this. __curious_geek
this. __curious_geek

Reputation: 43207

How to get System.Type instance of a class-type in Static Member?

I have a public static property in a class. The class has some custom attributes applied to it. I want to access the Attribute in a static property.

In a non-static member I can get the type of current class using this.GetType() but how do I do this in a static member of the class ?

Please note that..

EDIT

Here's my objective.

I have an abstract base class called EntityBase. All my entities derive from this class. Each entity also carries a custom attribute called TableMappingAttribute that lets me know the table it refers/maps to, during runtime. I already have a property in EntityBase that returns me the mapped TableName for the entity.

I will always need an instance of entity to access the TableName property. I wish to access this property statically sometime, like MyEntity.TableName. I have large amount entities in my project. I want this static property to be added in EntityBase class itself. So I must discover the type at runtime. How do I do this in EntityBase class itself ??

Thnaks.

Upvotes: 2

Views: 2923

Answers (6)

S M Kamran
S M Kamran

Reputation: 4503

A parent don't know how many childs it has. But a child know about its parent. The only way a parent should know about the child is through polymorphism which is not an attribute of static members.

What you are trying to do is to know about the child class in a public static property of parent. Why don't you consider sending your child class reference as a parameter in the static method in your base class and then in the base class have the reference of the child class by calling its GetType method...

public static string GetTableName(BaseClass childsObjectWrappedInBaseReference) {
   Type type = childsObjectWrappedInBaseReference.GetType();
   ....
   ....
}

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1499770

You can't, basically. typeof(...) is what you need to use.

Bear in mind that if you try to use:

Type type = MyDerivedType.SomeStaticProperty;

which is declared in MyBaseType, that will actually end up being compiled to

Type type = MyBaseType.SomeStaticProperty;

anyway. Static members basically aren't polymorphic. If you try to use them polymorphically, you'll run into problems like this.

EDIT: So from your edit, it looks like you're trying to do exactly the above type of thing, with

MyEntity.TableName

instead of

EntityBase.TableName

It just won't work. The compiler will emit code to fetch EntityBase.TableName. The runtime has no concept of "the current class". There's no context here.

Basically you need to change your design. If you want to use inheritance, you may want to have a parallel hierarchy - one for the metadata (things like table names) and one for the actual objects. So you'd have something like:

public class MyEntity : EntityBase<MyEntityType>

where MyEntityType derives from EntityType in the parallel hierarchy. Then you can use inheritance within the metadata hierarchy.

Alternatively, just making EntityBase generic anyway will let you get at the type of entity you're talking about:

public class MyEntity : EntityBase<MyEntity>

I know you said you didn't want to use generics, but as what you want to do just won't work, you should at least consider it...

Upvotes: 4

Philippe Leybaert
Philippe Leybaert

Reputation: 171734

If you don't want to use typeof(), then you're out of luck, because that's the only way to get the Type object of a static class (unless you want to find the type by calling Type.GetType() and look for it by name)

I don't see the problem with inheritance though.

Type type = typeof(YourStaticClass);

Attribute[] attributes = type.GetCustomAttributes(...);

Upvotes: 0

Fredrik M&#246;rk
Fredrik M&#246;rk

Reputation: 158289

You don't need to worry about inheritance if the property is static; it cannot be overridden, so it will always be declared in the base class anyway. Using typeof is the way to go.

Upvotes: 0

Marc Gravell
Marc Gravell

Reputation: 1062502

I do not want to use typeof(typename) due to inheritance issues.

static properties aren't inherited in the normal sense. Sure, they are in-scope, but that isn't the same. The only way to get what you want would be to look at the stack-frame, but that is ugly and hacky (and risky if optimisations are enabled).

I'd refactor for a solution that uses the instace... instance have a Type.

Upvotes: 2

Martin Liversage
Martin Liversage

Reputation: 106796

You can use the System.Diagnostics.StackFrame class in a static method like this:

StackFrame currentStackFrame = new StackFrame();
Type type = currentStackFrame.GetMethod().DeclaringType;

Upvotes: 0

Related Questions