简体   繁体   English

属性构造函数在运行时的什么时间运行?

[英]At what time during runtime does an attribute constructor get run?

Trying to find some verbose reference on the intricacies of Attributes.试图找到一些关于属性复杂性的详细参考。 Any help would be appreciated.任何帮助,将不胜感激。

At this point, I'd specifically like to know what time during runtime does an attribute constructor get ran?在这一点上,我特别想知道属性构造函数在运行时的什么时间运行?

  • If it's over a class如果超过 class
  • If it's over a property如果它超过了一个属性
  • If it's over a method如果它结束了一个方法

Thanks.谢谢。

The constructor is invoked when you call GetCustomAttributes() on the type or MemberInfo.当您对类型或 MemberInfo 调用 GetCustomAttributes() 时,将调用构造函数。

Reading the norm (17.3.2 in the C# 2.0 version) it's unspecified.未指定阅读规范(C# 2.0 版本中的 17.3.2)。 Only the way to convert from the metatada to an instance is.只有从元数据转换为实例的方法是。

So you may need to test on different implementations, because if it isn't specified it's bound to be interpreted differently.因此,您可能需要对不同的实现进行测试,因为如果未指定它,它必然会被不同地解释。

The only thing that you can be sure is that it'll be called before is needed.唯一可以确定的是它会在需要之前被调用。 It's not defined the exact time the constructor will be called.没有定义调用构造函数的确切时间。

Anyway, the behaviour is unespecified, so you shouldn't rely on whenever the constructur gets called by the current implementation.无论如何,行为是未指定的,所以你不应该依赖当前实现调用构造函数的时候。

Attribute are decorations that stores metadata or informations about a type.属性是存储元数据或类型信息的装饰。 .Net framework utilizes heavily this kind of information to do additional processing when creating instances. .Net 框架在创建实例时大量利用此类信息进行额外处理。

The attribute is constructed only when asked by some other class, with Type.GetCustomAttributes() for example.该属性仅在其他一些 class 询问时构建,例如 Type.GetCustomAttributes()。 So, even you can create your own attributes and then asks for your custom attributes.因此,即使您可以创建自己的属性,然后询问您的自定义属性。

public class MyOwnAttribute: Attribute {}

/* at some point in another class */ /* 在另一个 class 的某个时刻 */

void CheckIfClassIsDecoratedWithMyOwnAttribute()
{
    var instance = new MyClass();
    if (instance.GetType().GetCustomAttributes(typeof(MyOwnAttribute)))
    {
       //do whatever you want
    }
}

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

相关问题 在运行时没有发生在对象上添加属性 - Adding attribute on object during run time not happening 获取运行时预编译的Azure Functions程序集的生成时间 - Get build time of Precompiled Azure Functions assembly during runtime 在运行时访问.NET ActiveX用户控件构造函数中的设计时属性 - Accessing Design time properties in .NET ActiveX User control constructor during runtime 装饰主方法的属性构造函数不会在发布版本中调用 - Constructor of attribute decorating the main method does not get called in release builds 获取声明的属性构造函数 - Get Attribute Constructor As It was Declared 属性可以发现在运行时应用于什么方法吗? - Can an attribute discover what method it is applied to at run time? 设置属性@运行时间 - Set Attribute @ run time 如何在运行时重新加载自定义属性? ASP.NET核心MVC - How to reload a Custom Attribute during run-time? ASP.NET Core MVC 当构造函数注入在配置和运行时已知参数时,从AutoFac获取类实例的最佳方法是什么 - What is the best way to get instance of a class from AutoFac when constructor injection has parameters known at configuration and runtime 我们可以在运行时在C#中有一个默认构造函数的主体吗? - Can we have a body for a default constructor in C# during runtime?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM