10

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.

Fabio Milheiro
  • 8,100
  • 17
  • 57
  • 96
  • What do you want to do with the class? Instantiate an object? Call a static method? – Heinzi Sep 13 '10 at 17:39
  • 3
    Could you be more specific? A Type is the definition of the type, either a class (reference type) or struct (value type). A Type instance is usually retrieved from typeof(T) or obj.GetType(). – sisve Sep 13 '10 at 17:41
  • 2
    adding "opposite of typeof(Type)" for searching purposes. – NH. Sep 14 '18 at 03:19
  • "Converse of typeof" for the search purpose. – H2ONaCl Feb 08 '21 at 08:22
  • If you mean to help searchers in the future, then maybe you mean "Inverse operation of typeof"? Thanks for your input. It's always good to help other find the answers to their needs. – Fabio Milheiro Feb 08 '21 at 15:54

8 Answers8

7

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] { });
cichy
  • 10,464
  • 4
  • 26
  • 36
  • 1
    The only correct answer, not upvoted. I'd better fix that :) – Hans Passant Sep 13 '10 at 18:13
  • 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() where T is the class. This way, I am able to use it as it is mentioned in the new Edit. Thank you anyway. You were still helpful and, therefore, upvoted. – Fabio Milheiro Sep 14 '10 at 14:39
5

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:

Community
  • 1
  • 1
Heinzi
  • 167,459
  • 57
  • 363
  • 519
  • Although this doesn't do what I originally wanted, it was very helpful, so decided to mark it as an answer. Thanks Heinzi! – Fabio Milheiro Sep 16 '10 at 15:18
  • 1
    @Fabio: Thanks. I must admit that I do not fully understand the last edit you provided to your question. It might make sense to start an extra question on this (i.e., on how to do the repository thing). If your library design requires you to use reflection, it might be possible to improve the design to remove the need for reflection. – Heinzi Sep 16 '10 at 15:23
  • I had to use reflection so that our worker role updating method would work for anything we need in the future. The repository itself is another subject itself. I did not create it and I will not change it. Thanks again for your availability. – Fabio Milheiro Sep 17 '10 at 05:00
3

Use the "Activator" class:

Activator.CreateInstance<T>
Jerod Venema
  • 44,124
  • 5
  • 66
  • 109
2

I think you are looking for Activator.CreateInstance.

Jim Brissom
  • 31,821
  • 4
  • 39
  • 33
2

Here are a few options listed in order of my preference. I am assuming that T is the type parameter in your generic class or method.

new T(); // T must be constrained to support a default constructor.

or

Activator.CreateInstance(typeof(T), new object[] { });

or

typeof(T).GetConstructor(new Type[] { }).Invoke(null);

or

AppDomain.CurrentDomain.CreateInstanceAndUnwrap(typeof(T).Assembly.FullName, typeof(T).FullName);
Brian Gideon
  • 47,849
  • 13
  • 107
  • 150
1

Use the new() constraint.

public T Create<T>() where T : new() {
    return new T();
}
sisve
  • 19,501
  • 3
  • 53
  • 95
1

I must be missing something. The answers provided so far don't seem to match the questions. I would love more clarity.

Nevertheless I'll try to answer the question as I see it.

You say you're trying to do this:

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

Are you trying to do something more like this?

var repository = new GenericRepository<AzureBrightData>();

If so, then your generic repository class needs to be defined as such:

public class GenericRepository<T> where T : BaseEntity, new()
{
    ...
}

Then you can define your BaseEntity class as you have been, but the instantiation of your repository will give you the actual class - and I hope then the table - that you are looking for.

I hope I have understood your question.

Enigmativity
  • 113,464
  • 11
  • 89
  • 172
  • I wanted to convert convert a variable Type to the class itself. It has nothing to do with the "new AzureBrightData()" part... But you were right when you said no one answered the question :) I solved the problem another way. Thanks! – Fabio Milheiro Sep 16 '10 at 15:08
  • The converse of typeof(token) is the token. The token is the literal that identifies a class. Given an instance of the Type object, the converse is the literal that is the identifier of the class. I think that is what he wants. Where can you use this? The literal is what you need for a cast or to the right of "as". – H2ONaCl Feb 08 '21 at 08:42
0

I don't fully understand the OP question but I think this will certainly help some people searching for inverse of typeof which is how I got here myself.

I have a list of dynamic components in a lookup table.

class ImageComponent
{
    media: 'IMAGE';
}
  
class VideoComponent
{
    media: 'VIDEO';
}

// lookup table (runtime construct)    
const components = {

    'image': ImageComponent,
    'video': VideoComponent
}

So I want to take 'image' (the key in the table) and end up with IMAGE which is a property of ImageComponent.

Lookup the component from the table:

type imageComponent = typeof components['image'];    // typeof ImageComponent
   

If we actually had ImageComponent then we could do a type lookup on it.

ImageComponent['media']       // 'IMAGE'

But we have typeof ImageComponent instead which is useless for finding any properties on the class.

 type mediaType = imageComponent['media'];                // does not work

The actual answer...

So what we can do is get the 'prototype type' from something that is `typeof YourClassName.

  type media = imageComponent['prototype']['media'];      // 'IMAGE'  (const value)

or in other words what we're actually doing is:

  type media = (typeof ImageComponent)['prototype']['media'];

So for me this satisfied my search for 'inverse of typeof'.

Also note that 'IMAGE' and 'VIDEO' are literal types and not strings (except at runtime when they are just strings).

Simon_Weaver
  • 140,023
  • 84
  • 646
  • 689