Sign in to follow this  

[.net] Getting Generics from a Type

This topic is 3194 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi How can I get a Generic from a Type ? Here's a short example :


public class ResourceManager
{
    public static List<string> GetAssets<T>();
}

//...

Type[] types = new Type[]{typeof(Texture), typeof(Font2D), typeof(TileSet)};
foreach(Type type in types)
{
     List<string> list = ResourceManager.GetAssets<type>(); // Error CS0246
   
}
//...


Share this post


Link to post
Share on other sites
Perhaps I am misunderstanding your problem here, but why not just make the function take the type as a parameter rather than a generic type e.g.

List<string> list = ResourceManager.GetAssets( type );

Share this post


Link to post
Share on other sites
MindGames is right. Otherwise there's a reflection method (on MethodInfo iirc) that allows you to make a non-generic method from a generic method and type args.

Share this post


Link to post
Share on other sites
Warning: useless advice of the day ahead... :)

You might as well expose both signatures


List<string> Get<T>() { return this.Get(typeof(T)); }
List<string> Get(type T) { [...]; }

Share this post


Link to post
Share on other sites

This topic is 3194 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this