Dynamic Generics Proposal #2802
Replies: 9 comments
-
I imagine the Language Keepers will declare this too niche to consider, but the more I work with libraries, the more I find myself doing this same reflection-dance (and sort of hating it each time). However, I review and maintain a lot of code written by others, and I'm not sold on a easily-overlooked indicator like var value = obj.Foo<dynamic type1>(); |
Beta Was this translation helpful? Give feedback.
-
Over the years I've wanted to supply a type to a generic type/method via a variable on a number of occasions and haven't been able to. Invariably I've therefore resorted to reflection and/or passing it via a |
Beta Was this translation helpful? Give feedback.
-
This might be the first step towards first-class types. |
Beta Was this translation helpful? Give feedback.
-
It is not possible to check constraints in this case, for example. It will not be obvious how the call will be made: through reflection, through the dynamic, or somehow else. |
Beta Was this translation helpful? Give feedback.
-
The dynamic language runtime can't do this today, so I don't see the ambiguity: I read the proposal as a request to specifically create syntactic sugar around the reflection approach, and Hmm, did you mean my suggestion to overload We already have two divergent meanings for the |
Beta Was this translation helpful? Give feedback.
-
Thank you. The resulting code just looks like some analogue of var instance = new Demo();
var argumentType = typeof(Bar);
var value = instance.Foo<@argumentType>(); // What the type of the "value"? What if internal class Demo
{
public T Foo<T>() => default(T);
} It looks like, after applying |
Beta Was this translation helpful? Give feedback.
-
You can't use @. It already is a valid character before an identifier. |
Beta Was this translation helpful? Give feedback.
-
@ViIvanov since the proposal is to wrap the longer reflection-based code, class Foo
{
public T Baz<T>() where T : new() => new T();
}
class Bar
{ }
var obj = new Foo();
var genericType = typeof(Bar);
MethodInfo methodInfo = obj.GetType().GetMethod("Baz");
MethodInfo genericMethod = methodInfo.MakeGenericMethod(genericType);
object value = genericMethod.Invoke(obj, new object[] { });
Bar cast = value as Bar;
Console.WriteLine(cast.GetType()); @LouisFr81 good catch, it seems the rest of us forgot the reserved-keyword / identifier-name disambiguation prefix. Hopefully there isn't much code out there using variable names like |
Beta Was this translation helpful? Give feedback.
-
I should have thought of this earlier. A simple extension method is really all it takes: public static class InvokeGenericExtension
{
public static object InvokeGeneric(this object target, Type genericType, string methodName, params object[] methodParameters)
{
MethodInfo methodInfo = target.GetType().GetMethod("Baz");
MethodInfo genericMethod = methodInfo.MakeGenericMethod(genericType);
return genericMethod.Invoke(target, methodParameters);
}
}
var value = new Foo().InvokeGeneric(typeof(Bar), "Baz"); |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
In writing code that uses generics I always find it tedious and error prone to call generic methods and create generic classes when you don't know the type at compile time. I propose in a future version of C# that there is some syntactic sugar to make this easier.
For a class that looks like this
public class demo { string Foo<T>(); }
Currently you have to do something like this
`Example
I would like to see it easier to do like this:
` Example
`
using the @ sign in front of the Type you could dynamically call generic methods or create Generic Classes while C# generates that messy, easy to get wrong reflection code for you (properly).
Beta Was this translation helpful? Give feedback.
All reactions