Passing a delegate as a type parameter and using it causes error CS0314

I am trying to pass a delegate type as a type parameter so that I can use it later as a type parameter in the code, for example:

// Definition private static class Register { public static FunctionObject Create<T>(CSharp.Context c, T func) { return new IronJS.HostFunction<T>(c.Environment, func, null); } } // Usage Register.Create<Func<string, IronJS.CommonObject>>(c, this.Require); 

However, the C # compiler complains:

 The type 'T' cannot be used as type parameter 'a' in the generic type or method 'IronJS.HostFunction<a>'. There is no boxing conversion or type parameter conversion from 'T' to 'System.Delegate'." 

I tried to fix this by adding the "where T: System.Delegate" function to the function, however you cannot use System.Delegate as a restriction on type parameters:

 Constraint cannot be special class 'System.Delegate' 

Does anyone know how to resolve this conflict?

DOES NOT WORK (information of type Argument and return type is lost during translation):

 Delegate d = (Delegate)(object)(T)func; return new IronJS.HostFunction<Delegate>(c.Environment, d, null); 
+7
source share
3 answers

If you look at https://github.com/fholm/IronJS/blob/master/Src/IronJS/Runtime.fs , you will see:

 and [<AllowNullLiteral>] HostFunction<'a when 'a :> Delegate> = inherit FO val mutable Delegate : 'a new (env:Env, delegateFunction, metaData) = { inherit FO(env, metaData, env.Maps.Function) Delegate = delegateFunction } 

In other words, you cannot use C # or VB to write your function, because for this you need to use System.Delegate as a type constraint. I recommend either writing your function in F # or using reflection, for example:

 public static FunctionObject Create<T>(CSharp.Context c, T func) { // return new IronJS.HostFunction<T>(c.Environment, func, null); return (FunctionObject) Activator.CreateInstance( typeof(IronJS.Api.HostFunction<>).MakeGenericType(T), c.Environment, func, null); } 
+6
source

@Gabe is absolutely right, it is related to the restriction type of the class HostFunction <'a>, which is valid only in F # (not C # or VB).

Have you checked the functions in Native.Utils ? This is what we use inside the runtime to create functions from delegates. Especially the let CreateFunction (env:Env) (length:Nullable<int>) (func:'a when 'a :> Delegate) = function should do exactly what you need.

If CreateFunction does not fulfill what you need, open a ticket http://github.com/fholm/IronJS/issues with what you are missing and how you can see how it is implemented and we will get it.

0
source

As an update for anyone reading this after May 2018:

Starting with C # 7.3 (.Net Framework 4.7.2), you can now use where T: System.Delegate as a constraint on the generic declaration, which means that the original author will now be able to do what she is trying to do in C # - without the need to resort to creating a class with another .Net language.

System.Enum (another extremely missed limitation in earlier versions of C #) is now also available. There are also a couple of other add-ons.

https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/generics/constraints-on-type-parameters#delegate-constraints

0
source

All Articles