如何获取静态成员中类类型的 System.Type 实例?
我在类中有一个公共静态属性。 该类应用了一些自定义属性。 我想访问静态属性中的属性。
在非静态成员中,我可以使用 this.GetType() 获取当前类的类型,但是如何在类的静态成员中执行此操作?
请注意..
我不想使用typeof(typename),因为 到继承问题。 [我会有 该属性继承到派生 类。]。
我也不想使用泛型作为 好吧。
编辑
这是我的目标。
我有一个名为 EntityBase 的抽象基类。 我所有的实体都源自这个类。 每个实体还带有一个名为 TableMappingAttribute 的自定义属性,它让我知道它在运行时引用/映射到的表。 我已经在 EntityBase 中有一个属性,它返回实体的映射 TableName。
我始终需要一个实体实例来访问 TableName 属性。 我希望有时静态访问此属性,例如 MyEntity.TableName。 我的项目中有大量实体。 我希望将此静态属性添加到 EntityBase 类本身中。 所以我必须在运行时发现类型。 我如何在 EntityBase 类本身中执行此操作?
谢谢。
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..
I do not want to use typeof(typename) due
to inheritance issues. [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. 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
基本上你不能。 typeof(...) 是您需要使用的。
请记住,如果您尝试使用:
在
MyBaseType
中声明,那么实际上最终会被编译为无论如何。 静态成员基本上不是多态的。 如果您尝试以多态方式使用它们,您将遇到这样的问题。
编辑:所以从你的编辑来看,你似乎正在尝试做上述类型的事情,而
不是
它只是行不通。 编译器将发出代码来获取 EntityBase.TableName。 运行时没有“当前类”的概念。 这里没有上下文。
基本上你需要改变你的设计。 如果您想使用继承,您可能需要一个并行的层次结构 - 一个用于元数据(例如表名称),另一个用于实际对象。 所以你会得到类似的东西:
其中 MyEntityType 派生自并行层次结构中的 EntityType。 然后您可以在元数据层次结构中使用继承。
或者,无论如何,只要使 EntityBase 通用就可以让您了解您正在谈论的实体类型:
我知道您说过您不想使用泛型,但由于您想做的事情是行不通的,所以您应该至少考虑一下...
You can't, basically. typeof(...) is what you need to use.
Bear in mind that if you try to use:
which is declared in
MyBaseType
, that will actually end up being compiled toanyway. 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
instead of
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:
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:
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...
在正常意义上,静态属性不会被继承。 当然,它们在范围内,但这并不相同。 获得你想要的东西的唯一方法是查看堆栈框架,但这是丑陋和黑客的(如果启用优化的话会有风险)。
我会重构一个使用 instace... 实例具有
Type
的解决方案。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
.您可以在静态方法中使用 System.Diagnostics.StackFrame 类,如下所示:
You can use the
System.Diagnostics.StackFrame
class in a static method like this:如果属性是静态的,则无需担心继承问题; 它不能被重写,因此无论如何它总是在基类中声明。 使用
typeof
是正确的方法。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. Usingtypeof
is the way to go.如果您不想使用 typeof(),那么您就不走运了,因为这是获取静态类的 Type 对象的唯一方法(除非您想通过调用 Type.GetType() 和按名称查找)
但我没有看到继承的问题。
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.
父母不知道自己有多少个孩子。 但孩子了解其父母。 父级应该了解子级的唯一方法是通过多态性,这不是静态成员的属性。
您想要做的是了解父类的公共静态属性中的子类。 为什么不考虑将子类引用作为基类静态方法中的参数发送,然后在基类中通过调用子类的 GetType 方法来获取子类的引用...
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...