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
AlanPosted Jun 29, 2007, 5:49 AM
You're in the realms here of 'declarative pre and post conditions', Stewart, which are not currently supported in C# (including, AFAIK, the forthcoming version 3.0) but are often proposed as a possible candidate for future inclusion in the language.
What you may have seen is a proposal as to how these might work or, possibly, a discussion of one of the C# variants, such as XC# or the research language Spec#, which do already support them:
http://www.resolvecorp.com/products.aspx
http://research.microsoft.com/specsharp/