c# - Throwing exceptions from attribute constructor
c#, custom-attributes, exception
Solution
Since attributes are part of class definition available to you at runtime (it's also called "metadata" in geekspeak) CLR does not instantiate them unless some part of your program asks for them. This makes sense: why bother spending CPU cycles for something that nobody wants to access?
Because of this, the execution of the constructor will never happen unless you ask for that attribute.
Here is one way to ask for an attribute that would make your program fail:
var attr = Attribute.GetCustomAttribute(typeof(Failer).GetProperty("Prop"), typeof(FailerAttr));
This code makes CLR instantiate the `FailerAttr`, which triggers the exception.
Demo on ideone.
If you do not know the type of the attribute, you can retrieve all attributes at once with this call:
var allAttributes = Attribute.GetCustomAttributes(typeof(Failer).GetProperty("Prop"));
This causes an exception as well (demo).
Problem
I found this article on the subject and tried the following: ``` public class FailerAttr : Attribute { public FailerAttr(string s) { throw new Exception("I should definitely fail!"); } } ``` And in unit test project I have the following: ``` 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; } } } ``` When I run the test, it succeeds. So, the questions are: - Why it does not fail? - Is it really a bad idea to throw exceptions from attributes? Because I think I need to. Some environment info (just in case it's relevant): - Unit tests are run via ReSharper's unit test runner (R# v8.2.0.2160) - Visual studio v11.0.61030.0