Groups | Blog | Home
all groups > dotnet clr > may 2006 >

dotnet clr : Extend compiler (pre compile)



eacsub
5/9/2006 9:46:01 PM
I would like to solve the problem of method parameter verfication in order to
avoid to writing repetitive code to validate method parameters, for example:

void method(object param)
{
if (param = null)
{
throw new ArgumentNullException("param");
}
}

and do it throw attibutes that decorate the mothods. but the I don't want
the preformance hit associated with reflection especially when it's
associated with each method invocation.

Is it possible to some how make the complire produce the code by reading the
attributes? Is it possible to extend the compiler some how? I remeber reading
something about extending the compiler in MSDN but i can't find it. is this
Greg Young
5/10/2006 1:00:02 AM
Is this what you were looking at from microsoft?
http://research.microsoft.com/research/pubs/view.aspx?msr_tr_id=MSR-TR-2003-32

There is also an extensible commercial compiler XC#
http://www.resolvecorp.com/ which might suit your needs; in fact they have
an example of almost exactly what you are trying to do here
http://www.resolvecorp.com/products.aspx

Cheers,

Greg Young
MVP - C#
[quoted text, click to view]

eacsub
5/10/2006 6:25:02 AM
Thank you Greg. Although, I don't think I wan't to substitute microsoft
compiler for a third party compiler and I don't want to create my own
compiler. Is there a middle way where i can intercept the compilation process
so that I read the attributes and convert them into code in the message body
and compile that code and produce the final assembly. maybe:

- compile into assembly with the attributes
- load the assembly, read the attributes and produce the code in the method
body
- compile the final code and generate the final assemble that has the
assertions.

[quoted text, click to view]
Greg Young
5/10/2006 3:22:24 PM
Not that I know of .. but there are many people around here who are alot
wiser than I.

Cheers,

Greg Young
MVP - C#
[quoted text, click to view]

Jay B. Harlow [MVP - Outlook]
5/10/2006 7:46:13 PM
eacsub,

Refactor! & other refactoring tools have a "Create Method Contract"
refactoring that adds the checks for you.

Refactor! does a return rather then throw an exception, haven't used it
enough to know if that is configurable or not. Using DXCore (the *free*
runtime that Refactor! is based on) would allow you to create your own
"add-in" for VS that injects the code you want...

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


[quoted text, click to view]
|I would like to solve the problem of method parameter verfication in order
to
| avoid to writing repetitive code to validate method parameters, for
example:
|
| void method(object param)
| {
| if (param = null)
| {
| throw new ArgumentNullException("param");
| }
| }
|
| and do it throw attibutes that decorate the mothods. but the I don't want
| the preformance hit associated with reflection especially when it's
| associated with each method invocation.
|
| Is it possible to some how make the complire produce the code by reading
the
| attributes? Is it possible to extend the compiler some how? I remeber
reading
| something about extending the compiler in MSDN but i can't find it. is
this
| doable?

AddThis Social Bookmark Button