Using GetType() inside a generic?

  • Thread starter Thread starter Ympostor
  • Start date Start date
Y

Ympostor

Can't I do some syntactic sugar like this?:

MyClass MyObject = new MyClass();
MyStaticClass.MyMethod<MyObject.GetType()>();

Thanks in advance.

--
 
Ympostor said:
Can't I do some syntactic sugar like this?:

MyClass MyObject = new MyClass();
MyStaticClass.MyMethod<MyObject.GetType()>();

And what's wrong with...

MyClass MyObject = new MyClass();
MyStaticClass.MyMethod<MyClass>();

Remember that generic classes are built out when the code is JITed --
before it is run. If you don't know the type until runtime, you can't
use a generic.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top