Jump to content
  • Advertisement
Sign in to follow this  
iliak

[.net] Getting Generics from a Type

This topic is 3414 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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!