Skip to content

Instantly share code, notes, and snippets.

@mgroves
Created April 19, 2012 12:59
Show Gist options
  • Select an option

  • Save mgroves/2420809 to your computer and use it in GitHub Desktop.

Select an option

Save mgroves/2420809 to your computer and use it in GitHub Desktop.
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() {}
}
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() { }
}
// get all the attributes used on MyClass
System.Reflection.MemberInfo info = typeof(MyClass);
object[] attributes = info.GetCustomAttributes(true);
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