Using Attributes to inject code

Jun 29 2007 3:16 AM
Hey all -

I was wondering how it's possible to use custom attributes in C# to add code to my classes.

An example I've seen quoted:

class Foo
{
  public void MyMethod([NotNull] MyType myparam)
  { ... }
}

Where the [NotNull] attribute causes this code:

class Foo
{
  public void MyMethod( MyType myparam)
  {
    if (myparam==null) throw new NullReferenceException();
    ...
  }
}

I've been trying to figure out what the definition of NotNullAttribute might look like in order to accomplish this.

Can anybody give me some pointers?

Thanks, Stewart


Answers (1)