Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a typeof inverse operation?

Tags:

c#

reflection

I get the Type but that's not the same as the Class which is what I'm looking for.

Is there an inverse operation of typeof?

EDIT

I need the class in order to use a generic repository:

GenericRepository<BaseEntity> repository = new GenericRepository<BaseEntity>(new AzureBrightData());

I started by writing BaseEntity from which all entity class descend, but the problem is that the repository needs to know which table to search for.

For example, if we have a partition key and row key combination pair of (1,1) this doesn't allow me or the repository to know from which table to get the registry. It's not enough and that's why I believe I need the table.

like image 565
Fabio Milheiro Avatar asked Sep 13 '10 17:09

Fabio Milheiro


3 Answers

If i undestood answers under your question than maybe you are looking for something like this (instantiate Type):

     Assembly asmApp = Assembly.LoadFile("some.dll");
     Type tApp = asmApp.GetType("Namespace.SomeClass");
     object oApp = Activator.CreateInstance(tApp, new object[0] { });
like image 99
cichy Avatar answered Nov 09 '22 01:11

cichy


I'll base my answer on the clarification you provided in a comment:

I misunderstood what everyone said here or at least I did not make myself clear. I want to get the class as I would use it normally. For example, I have to pass the class like this: public void methodName<T>() where T is the class.

Short answer: No, you can't, because generic types are resolved at compile time.

Long answer: Yes, you can, but you need to use reflection. Here's how you do that:

  • StackOverflow: How to use reflection to call generic Method?
  • StackOverflow: What's the best way to instantiate a generic from its name?
like image 20
Heinzi Avatar answered Nov 09 '22 02:11

Heinzi


Use the "Activator" class:

Activator.CreateInstance<T>
like image 3
jvenema Avatar answered Nov 09 '22 01:11

jvenema