Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can be C# generics be queried at run time?

Tags:

java

c#

generics


So I'm currently working on porting a project from Java to C#, and I'm trying to figure something out.

Currently, in Java, when we query something of the generic type, it erases the Generic type. And because of that, when we query something, we need to use Annotation so that we can get a collection and know what we get.

In C#, is this the same issue? Can C# generics be queried at run time? If we do, do we need to use the attributes to solve the issue?

like image 488
Michael Fender Avatar asked Dec 30 '25 23:12

Michael Fender


1 Answers

Can C# generics be queried at run time?

Yes. The generic types are not erased in C#, as they are part of the type information itself. You should not need to add attributes to your objects in C# for this purpose.

That being said, by using generic methods in addition to your generic types, you may not need to "query" the type, as you can construct the appropriate type up front.

like image 155
Reed Copsey Avatar answered Jan 01 '26 13:01

Reed Copsey