has anyone been able to use Linqbridge on a .Net 2.0 Website?
I have no problem using it in a normal .Net 2.0 console, but when I use the methods in the website,
I get
Feature 'extension method' cannot be used because it is not part of the ISO-2 C# language specification
I think the error message is pretty clear. Extension methods aren't supported in 2.0. If you want to use an extension method in 2.0, you'd need to modify it by removing the this and call it explicitly.
If you had:
public static class ExtensionMethods {
public static bool IsOdd(this int x) {
return x % 2 != 0;
}
}
Then ExtensionMethods and code like number.IsOdd() won't compile.
You'd need to remove the this in the IsOdd method signature and call it as ExtensionMethods.IsOdd(number) to get it to work under 2.0.
If I recall correctly, that's the approach the authors of LinqBridge used.
Hope that helps.
Maybe you're confusing .NET and C# versions. LINQBridge supports .NET 2.0, but you still need C# 3.0 or later (i.e. VS2008 or later) to compile code with extension method or LINQ syntax sugar. Once compiled, the assembly runs without issue on .NET 2.0 runtimes. That's the benefit of LINQBridge.
Related
When I use float.IsFinite(float f) in a C# shell, I have no problems. However, when I use it in the context of Unity, I get an error alleging that it is not defined:
Here is the relevent code:
public static bool IsFinite(Unit u) => float.IsFinite(u.blocks);
Unit is a struct in which I wrote the above line.
My question is: why does this error appear and how can I fix it?
float.IsFinite exists in .NET 5, .NET Standard 2.1 and .NET Core 2.1. However Unity supports neither of those frameworks, just .NET Standard 2.0. This is why you cannot use the function here.
However it´s easy to create a workaround for the lack of that function:
public static bool IsFinite(Unit u) => !float.IsInfinity(u.blocks)
There´s an angoing discussion at Unitity support forum about .NET 5
I need to create a covariant interface with a method that takes a delegate with covariant generic parameter. Here's the code sample under question:
interface IExample<out T1>
{
void ExampleMethod(Action<T1> someAction);
}
On Mono/ .NET 4 profile it compiles OK (tested it in Xamarin studio). However, on .NET 2 (which I'm forced to use since I'm using Unity game engine), I get the following error:
error CS1961: The covariant type parameter 'T1' must be invariantly valid on `CovarianceExample.IExample.ExampleMethod(System.Action)'
Why does this error occur in early .Net versions? How can I fix it?
Why does this error occur in early .Net versions?
Because Action wasn't contravariant in .NET 2.0 (or 3.5).
How can I fix it?
Don't use .NET 2.0 :) I thought that modern versions of Unity were based on more recent versions of Mono anyway - perhaps an upgrade is available?
Alternatively, you could declare your own ContravariantAction delegate:
public delegate void ContravariantAction<in T>(T value);
I haven't tried doing so against .NET 2.0, but I believe the appropriate attributes were already present, and at least the MS .NET implementation supported generic variance - it just wasn't exposed in C# or used in the BCL.
There's a method in the .NET Framework that has both generic and a non-generic version/overload. I want to force the compiler to generate my code using the non-generic version even if it can resolve the generic type at build time.
The specific method I want to use is Marshal::GetFunctionPointerForDelegate which, as of .NET 4.5.1, has a generic overload. I want to use the original one (non-generic).
Also, if you could provide the solution (if any) in C++/CLI, it'd be much appreciated.
UPDATE: There reason for this is that, if my program is ran in a computer that does not have .NET 4.5.1, my program will crash.
It should be clear from the source code snippet posted by #elgonzo that the generic version is calling the non-generic version, otherwise you would have infinite recursion.
Therefore, you too can call the non-generic version. The trick is making the non-generic version a perfect match.
C#:
Marshal.GetFunctionPointerForDelegate( (Delegate)(object) mydel );
C++/CLI:
Marshal::GetFunctionPointerForDelegate( dynamic_cast<Delegate^>(safe_cast<Object^>(mydel)) );
It does not really matter whether you use the generic or the non-generic variant of the Marshall.GetFunctionPointerForDelegate method.
The only thing Marshall.GetFunctionPointerForDelegate<TDelegate>(...) does is calling the non-generic Marshal.GetFunctionPointerForDelegate(...) method.
As can be seen in the .NET framework reference source code, the implementation of GetFunctionPointerForDelegate<TDelegate> is just:
public static IntPtr GetFunctionPointerForDelegate<TDelegate>(TDelegate d)
{
return GetFunctionPointerForDelegate((Delegate)(object)d);
}
Regarding your update
UPDATE: There reason for this is that, if my program is ran in a computer that does not > have .NET 4.5.1, my program will crash.
Set the target framework of your project to a .NET version prior to 4.5.1 (for example, if your project is required to run on .NET 3.5, then set the target framework of your project to .NET 3.5.)
I have recently upgraded from VS2005 to VS2010. In my .Net 2.0 code I referenced the Microsoft.VisualBasic.dll and used the StringType.StrLike method to perform glob string comparison. I just noticed that according to MSDN, StringType is deprecated. Is there a replacement for the Like operator in VS2010/.Net 4.0?
Try using the LikeOperator instead.
using Microsoft.VisualBasic.CompilerServices;
...
if (LikeOperator.LikeString(left, right, CompareMethod.Text)) {
...
}
Your link says StrLike is only for compiler infrastructure and you should just use the Like operator, which makes no mention of deprecation. Are you using Like or are you actually using StrLike?
What are some of the new features that can be used in .NET 2.0 that are specific to C# 3.0/3.5 after upgrading to Visual Studio 2008? Also, what are some of the features that aren't available?
Available
Lambdas
Extension methods (by declaring an empty System.Runtime.CompilerServices.ExtensionAttribute)
Automatic properties
Object initializers
Collection Initializers
LINQ to Objects (by implementing IEnumerable extension methods, see LinqBridge)
Not Available
Expression trees
WPF/Silverlight Libraries
You can use any new C# 3.0 feature that is handled by the compiler by emitting 2.0-compatible IL and doesn't reference any of the new 3.5 assemblies:
Lambdas (used as Func<..>, not Expression<Func<..>> )
Extension methods (by declaring an empty System.Runtime.CompilerServices.ExtensionAttribute)
Automatic properties
Object Initializers
Collection Initializers
LINQ to Objects (by implementing IEnumerable<T> extension methods, see LinqBridge)
Pretty much everything! Daniel Moth covers this here and here. That only leaves runtime support: LINQ-to-Objects is provided by LINQBridge - which leaves just bigger APIs like Expression support, and tools like LINQ-to-SQL. These are too big to be reasonably ported back to .NET 2.0, so I'd use .NET 3.5 for these.
I cover this in an article on my site.
Almost all C# 3.0 features are available when targeting .NET 2.0. For extension methods, you need to define an extra attribute. Expression trees aren't available at all. Query expression support is based on a translation followed by "normal" C# rules, so you'll need something to provide the Select, Where etc methods. LINQBridge is the de facto standard "LINQ to Objects in .NET 2.0" implementation. You may well want to declare the delegates in the Func and Action delegate families to make it easier to work with lambda expressions - and then remove them if/when you move to .NET 3.5
To define extension methods, you'll need to supply the following class if you're targeting .NET 2.0:
namespace System.Runtime.CompilerServices {
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class | AttributeTargets.Assembly)]
sealed class ExtensionAttribute : Attribute { }
}
There was a previous discussion about something similar you may also want to read too:
Targeting .NET Framework 3.5, Using .NET 2.0 Runtime. Caveats?
You can use Mono's version of the System.Core which fully supports LINQ & Expression Trees.
I compiled its source against .net 2.0, and now I can use it in my .net2.0 projects.
This is great for projects that needs to be deployed on win2k, where .net3.5 is not available.
Lambdas & Extension methods are handled purely by the compiler and can be used with the .Net 2.0 framework.