Created
April 19, 2012 12:59
-
-
Save mgroves/2420809 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class MyAttribute : Attribute | |
| { | |
| public MyAttribute(string something) { } | |
| public MyAttribute(string[] something) { } | |
| } | |
| public class AttributeUsage | |
| { | |
| [MyAttribute("this works")] | |
| public void Method1() {} | |
| [MyAttribute(new [] { "this", "also", "works"})] | |
| public void Method2() {} | |
| static string _thisDoesnt = "this doesnt"; | |
| [MyAttribute(_thisDoesnt)] | |
| public void Method3() {} | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class MyAttributeWithType1<T> : Attribute // the compile error occurs here | |
| { | |
| public MyAttributeWithType1() {} | |
| } | |
| public class MyAttributeWithType2 : Attribute | |
| { | |
| public MyAttributeWithType2(Type t) {} | |
| } | |
| public class AttributeWithTypeUsage | |
| { | |
| [MyAttributeWithType1<DateTime>] | |
| public void Method1() { } | |
| [MyAttributeWithType2(typeof(DateTime))] | |
| public void Method2() { } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // get all the attributes used on MyClass | |
| System.Reflection.MemberInfo info = typeof(MyClass); | |
| object[] attributes = info.GetCustomAttributes(true); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class MyController : Controller | |
| { | |
| [HttpPost] | |
| public ActionFilter ActionOnlyForPostRequests() | |
| { | |
| // action method body... | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment