Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Code injection at compil time

Status
Not open for further replies.

BKQc

Programmer
Jul 26, 2002
118
CA
Anyone knows of a way to inject some code before the compiler does its job?
My goal is to be able to add attributes to function parameters to generate testing code so a function like

Code:
function MyFunction(<notNothing()>test as object)
end function
would become
Code:
function MyFunction(test as object)
  if test is nothing then
    throw new ArgumentNullException("test")
  end if
end function

I wouldn't mind if the code was automatically generated at runtime by I don't know more how to do it.

Any help would be quite appreciated, thank you all ;)
 
why do you need to dynamically gen/produce code like that, you can just write that.

You may be wanting some code gen tools for design time automation, but that is different than compile time behavior changes.

-Sometimes the answer to your question is the hack that works
 
This was a simple example but the goal is to easily implement complex validation or even stats gathering, counters, trace, ... name it. The best would be to have a way to automatically execute code each time the function is called but I think this is even harder.

The reasons to have it done at compil time and not at design time is first to have it centralized, manageable and updatable but there's also an interest in having the code easier to read by removing the clutter made by generic validations. Design time tools will still add code that makes it harder to read and get a quick idea of what it is doing.
 
I am not really understanding your end goal. Perhaps someone else can help you better.

-Sometimes the answer to your question is the hack that works
 
Yes it can easily (;-)) be done. look up attributes for .net and design by contract (roy osherove) has one that uses atributes. They only work in 2.0 and above (I think).

Christiaan Baes
Belgium

My Blog
 
OK, so it seems it is possible... But anyone know how?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top