1
Answer

Using Attributes to inject code

Stewart Johnson

Stewart Johnson

17y
2.8k
1
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)