Dynamic Generics #7916
Replies: 4 comments 20 replies
-
If you don't like writing the reflection, then there are some options:
I can't say there has ever been a situation that I had control over in which I couldn't know the type at compile time. Consequently, since this is something you encounter often, can you provide a use case in which the type couldn't possibly be known at compile time? |
Beta Was this translation helpful? Give feedback.
-
I can understand what is requested, and I can even imagine situations where this would be needed. But:
|
Beta Was this translation helpful? Give feedback.
-
The infrastructure to "recover" from having a non-generic value already exists with dynamic - here's an example that shows how you can go from something held in a variable of type public void M()
{
var item = 9;
object obj = item;
dynamic dyn = item;
var listA = CreateList(item);
Console.WriteLine(listA.GetType().ToString());
var listB = CreateList(obj);
Console.WriteLine(listB.GetType().ToString());
var listC = CreateList(dyn);
Console.WriteLine(listC.GetType().ToString());
}
private IEnumerable<T> CreateList<T>(T item)
{
return new List<T>{ item };
} HOWEVER, the problem is that you can't do very much with the list once you have it. Sure, it's a I've needed code just like that in the original post. And the fix was to propagate the generic types out from that point, so that you don't end up with
Generic library code that I write never knows the type at compile type. That's literally why I write generic code, so that it works even though I don't know the actual type that will be used. |
Beta Was this translation helpful? Give feedback.
-
I think that your example in the OP doesn't really represent the problem, in my experience the problem many times is with library authors that are exposing only the generic version of a type or a method and it may be totally appropriate but then consumers needs to deal with it in their edge cases and it's certainly shouldn't be a common practice but if it is, you either trying to use the wrong tool for the job or picked the wrong language for you to begin with so I really don't think the language needs to change because of that and I don't think something like this would ever make it into the language not only because it shouldn't be common but the reliance on reflection is detestable; however, you can always write utility or extension methods to ease your pain. I'd love to have generic specialization, (a lot) more generic type inference, C++ concepts like feature for generics but dynamic? maybe in some other circumstances. Referencing your old issue #2802 from 2019. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I often wish generics in c# would work like this.
Often you can't use generics because you don't know the type at compile time. Who would like to have all the reflection code to make this work, generated for you at compile time?
Beta Was this translation helpful? Give feedback.
All reactions