简体   繁体   English

如何在静态成员中获取类类型的System.Type实例?

[英]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 ? 在非静态成员中,我可以使用this.GetType()获得当前类的类型,但是如何在类的静态成员中实现呢?

Please note that.. 请注意..

  • I do not want to use typeof(typename) due to inheritance issues. 由于继承问题,我不想使用typeof(typename) [I will have this property inherited to derived classes.]. [我将将此属性继承到派生类。]

  • I also do not want to use Generics as well. 我也不想使用泛型

EDIT 编辑

Here's my objective. 这是我的目标。

I have an abstract base class called EntityBase. 我有一个称为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. 每个实体还带有一个名为TableMappingAttribute的自定义属性,该属性使我知道在运行时它引用/映射的表。 I already have a property in EntityBase that returns me the mapped TableName for the entity. 我已经在EntityBase中拥有一个属性,该属性可向我返回该实体的映射TableName。

I will always need an instance of entity to access the TableName property. 我将始终需要一个实体实例来访问TableName属性。 I wish to access this property statically sometime, like MyEntity.TableName. 我希望某个时候可以静态访问此属性,例如MyEntity.TableName。 I have large amount entities in my project. 我的项目中有大量实体。 I want this static property to be added in EntityBase class itself. 我希望将此静态属性添加到EntityBase类本身中。 So I must discover the type at runtime. 因此,我必须在运行时发现类型。 How do I do this in EntityBase class itself ?? 我如何在EntityBase类本身中执行此操作?

Thnaks. Thnaks。

You can't, basically. 基本上你不能。 typeof(...) is what you need to use. typeof(...) 您需要使用的。

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 MyBaseType 声明的,实际上最终将被编译为

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. 编译器将发出代码以获取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. 其中MyEntityType从并行层次结构中的EntityType派生。 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: 另外,只要使EntityBase通用,就可以让您了解正在谈论的实体的类型:

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... 我知道您说过您不想使用泛型,但是由于您想做的事情行不通,因此至少应该考虑一下……

I do not want to use typeof(typename) due to inheritance issues. 由于继承问题,我不想使用typeof(typename)。

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 . 我将重构一个使用instace ...实例的TypeType的解决方案。

You can use the System.Diagnostics.StackFrame class in a static method like this: 您可以在静态方法中使用System.Diagnostics.StackFrame类,如下所示:

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

You don't need to worry about inheritance if the property is static ; 如果属性是static ,则不必担心继承; it cannot be overridden, so it will always be declared in the base class anyway. 它不能被覆盖,因此无论如何它将始终在基类中声明。 Using typeof is the way to go. 使用typeoftypeof的方法。

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) 如果您不想使用typeof(),那么您就不走运了,因为这是获取静态类的Type对象的唯一方法(除非您想通过调用Type.GetType()和按名称查找)

I don't see the problem with inheritance though. 我看不到继承的问题。

Type type = typeof(YourStaticClass);

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

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... 为什么不考虑在基类的静态方法中将子类引用作为参数发送,然后在基类中通过调用其子类的GetType方法来获得子类的引用...

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM