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

发布于 2024-07-27 18:21:31 字数 705 浏览 4 评论 0原文

我在类中有一个公共静态属性。 该类应用了一些自定义属性。 我想访问静态属性中的属性。

在非静态成员中,我可以使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

美羊羊 2024-08-03 18:21:31

基本上你不能。 typeof(...) 是您需要使用的。

请记住,如果您尝试使用:

Type type = MyDerivedType.SomeStaticProperty;

MyBaseType声明,那么实际上最终会被编译为

Type type = MyBaseType.SomeStaticProperty;

无论如何。 静态成员基本上不是多态的。 如果您尝试以多态方式使用它们,您将遇到这样的问题。

编辑:所以从你的编辑来看,你似乎正在尝试做上述类型的事情,而

MyEntity.TableName

不是

EntityBase.TableName

它只是行不通。 编译器将发出代码来获取 EntityBase.TableName。 运行时没有“当前类”的概念。 这里没有上下文。

基本上你需要改变你的设计。 如果您想使用继承,您可能需要一个并行的层次结构 - 一个用于元数据(例如表名称),另一个用于实际对象。 所以你会得到类似的东西:

public class MyEntity : EntityBase<MyEntityType>

其中 MyEntityType 派生自并行层次结构中的 EntityType。 然后您可以在元数据层次结构中使用继承。

或者,无论如何,只要使 EntityBase 通用就可以让您了解您正在谈论的实体类型:

public class MyEntity : EntityBase<MyEntity>

我知道您说过您不想使用泛型,但由于您想做的事情是行不通的,所以您应该至少考虑一下...

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...

极致的悲 2024-08-03 18:21:31

由于继承问题,我不想使用 typeof(typename)。

在正常意义上,静态属性不会被继承。 当然,它们在范围内,但这并不相同。 获得你想要的东西的唯一方法是查看堆栈框架,但这是丑陋和黑客的(如果启用优化的话会有风险)。

我会重构一个使用 instace... 实例具有 Type 的解决方案。

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.

泪眸﹌ 2024-08-03 18:21:31

您可以在静态方法中使用 System.Diagnostics.StackFrame 类,如下所示:

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

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

StackFrame currentStackFrame = new StackFrame();
Type type = currentStackFrame.GetMethod().DeclaringType;
暮光沉寂 2024-08-03 18:21:31

如果属性是静态的,则无需担心继承问题; 它不能被重写,因此无论如何它总是在基类中声明。 使用 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. Using typeof is the way to go.

输什么也不输骨气 2024-08-03 18:21:31

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

但我没有看到继承的问题。

Type type = typeof(YourStaticClass);

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

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(...);
撩起发的微风 2024-08-03 18:21:31

父母不知道自己有多少个孩子。 但孩子了解其父母。 父级应该了解子级的唯一方法是通过多态性,这不是静态成员的属性。

您想要做的是了解父类的公共静态属性中的子类。 为什么不考虑将子类引用作为基类静态方法中的参数发送,然后在基类中通过调用子类的 GetType 方法来获取子类的引用...

public static string GetTableName(BaseClass childsObjectWrappedInBaseReference) {
   Type type = childsObjectWrappedInBaseReference.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...

public static string GetTableName(BaseClass childsObjectWrappedInBaseReference) {
   Type type = childsObjectWrappedInBaseReference.GetType();
   ....
   ....
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文