c# attribute
何时自定义属性的构造函数运行? (3)
什么时候运行? 它是针对我应用它的每个对象运行的,还是只运行一次? 它能做什么,或者它的行为受到限制?
每次调用GetCustomAttributes
,或者每当其他代码直接调用构造函数时,都会运行构造函数(不是说有充分理由这样做,但这也不是不可能)。
请注意,至少在.NET 4.0中,属性实例不会被缓存 ; 每次调用GetCustomAttributes
都会构造一个新实例:
[Test]
class Program
{
public static int SomeValue;
[Test]
public static void Main(string[] args)
{
var method = typeof(Program).GetMethod("Main");
var type = typeof(Program);
SomeValue = 1;
Console.WriteLine(method.GetCustomAttributes(false)
.OfType<TestAttribute>().First().SomeValue);
// prints "1"
SomeValue = 2;
Console.WriteLine(method.GetCustomAttributes(false)
.OfType<TestAttribute>().First().SomeValue);
// prints "2"
SomeValue = 3;
Console.WriteLine(type.GetCustomAttributes(false)
.OfType<TestAttribute>().First().SomeValue);
// prints "3"
SomeValue = 4;
Console.WriteLine(type.GetCustomAttributes(false)
.OfType<TestAttribute>().First().SomeValue);
// prints "4"
Console.ReadLine();
}
}
[AttributeUsage(AttributeTargets.All)]
class TestAttribute : Attribute
{
public int SomeValue { get; private set; }
public TestAttribute()
{
SomeValue = Program.SomeValue;
}
}
当然,让属性表现得不是最好的主意。 至少,请注意GetCustomAttributes
没有记录,表现得像这样; 事实上,上述程序中发生的事情未在文档中指定。
可执行文件或DLL中的元数据存储:
- 指示要调用的构造函数的元数据标记
- 争论
当我到达CLI实现的那一部分时,我计划在第一次为ICustomAttributeProvider
调用GetCustomAttributes()
时调用构造ICustomAttributeProvider
。 如果请求了特定的属性类型,我将只构造返回该类型所需的属性类型。
构造函数何时运行? 尝试一下样品:
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Creating MyClass instance");
MyClass mc = new MyClass();
Console.WriteLine("Setting value in MyClass instance");
mc.Value = 1;
Console.WriteLine("Getting attributes for MyClass type");
object[] attributes = typeof(MyClass).GetCustomAttributes(true);
}
}
[AttributeUsage(AttributeTargets.All)]
public class MyAttribute : Attribute
{
public MyAttribute()
{
Console.WriteLine("Running constructor");
}
}
[MyAttribute]
class MyClass
{
public int Value { get; set; }
}
什么是输出?
Creating MyClass instance
Setting value in MyClass instance
Getting attributes for MyClass type
Running constructor
因此,当我们开始检查属性时,将运行属性构造函数。 请注意,属性是从类型中获取的,而不是类型的实例。