c# - 从属性构造函数中抛出异常

Det*_*iel 7 c# exception custom-attributes

我找到了关于这个主题的这篇文章并尝试了以下内容:

public class FailerAttr : Attribute {
    public FailerAttr(string s) {
        throw new Exception("I should definitely fail!");
    }
}
Run Code Online (Sandbox Code Playgroud)

在单元测试项目中,我有以下内容:

using Microsoft.VisualStudio.TestTools.UnitTesting;

[TestClass]
public class Test {
    [TestMethod]
    public void GoFail() {
        // Make sure attribute will get initialized
        new Failer();
    }

    private class Failer {
        [FailerAttr("")]
        public int Prop { get; set; }
    }
}
Run Code Online (Sandbox Code Playgroud)

当我运行测试时,它会成功.所以,问题是:

  1. 为什么不失败?
  2. 从属性中抛出异常真是个坏主意吗?因为我认为我需要.

一些环境信息(以防万一相关):

  • 单元测试通过ReSharper的单元测试运行器(R#v8.2.0.2160)运行
  • Visual studio v11.0.61030.0

das*_*ght 11

由于属性是运行时可用的类定义的一部分(在geekspeak中也称为"元数据"),除非程序的某些部分要求,否则CLR不会实例化它们.这是有道理的:为什么要花费CPU周期来寻找无人无法访问的东西?

因此,除非您要求该属性,否则将永远不会执行构造函数的执行.

这是一种要求使程序失败的属性的方法:

var attr = Attribute.GetCustomAttribute(typeof(Failer).GetProperty("Prop"), typeof(FailerAttr));
Run Code Online (Sandbox Code Playgroud)

此代码使CLR实例化FailerAttr,从而触发异常.

在ideone上演示.

如果您不知道属性的类型,则可以使用此调用一次检索所有属性:

var allAttributes = Attribute.GetCustomAttributes(typeof(Failer).GetProperty("Prop"));
Run Code Online (Sandbox Code Playgroud)

这也会导致异常(演示).


ang*_*son 6

属性不会转换为可执行代码,而是转换为元数据.

在正常执行期间不使用这样的元数据,只有当您开始使用元数据(如通过反射)时,属性类型才会重新发挥作用.

编译期间不执行构造函数或属性中的任何代码.相反,构造函数的类型和参数被序列化为元数据,并且只有在使用反射检查时才会实际执行构造函数.

换句话说,如果你打算在编译时失败,那么你就不能.

尝试使用反射查找属性,具体取决于从元数据反序列化的属性对象,构造函数可能会或可能不会被调用,但它绝对不会通过将它应用于标识符来调用.