Injecting code into methods based on UDA's

TheFlyingFiddle theflyingfiddle at gmail.com
Sat Oct 19 16:16:38 PDT 2013


Is it possible to inject code into a method with UDA's?
For example if i wanted a method to do some logging when it's
called.

Something like the following.

void logMsg(string file, string msg)
{
   //Log the message to the file.
}

struct Log
{
    string logFile;
}

class Foo
{
   void bar() { /* do something */ }
   @Log("fooInfo.txt")
   int baz()
   {
     //Does some calculation and returns some value.
   }
}


would expand the class Foo to (at compile time)

class Foo
{
	void bar() { } //unchanged
	int baz()
	{
	   logMsg("fooInfo.txt", "Method Foo.baz was called.");

	   //Let baz do it's thing
	}
}

I know that this can be done by using a wrapper template that 
creates
a subclass but it would be nice if the code could just be injected
with the UDA's somehow.



More information about the Digitalmars-d-learn mailing list