How to intercept, parse and compile?
Posted
by epitka
on Stack Overflow
See other posts from Stack Overflow
or by epitka
Published on 2010-04-07T12:49:34Z
Indexed on
2010/04/07
12:53 UTC
Read the original article
Hit count: 319
This is a problem I've been struggling to solve for a while. I need a way to either replace code in the method with a parsed code from the template at compile time (PostSharp comes to mind) or to create a dynamic proxy (Linfu or Castle). So given a source code like this
[Template]
private string GetSomething()
{
var template = [%=Customer.Name%]
}
I need it to be compiled into this
private string GetSomething()
{
MemoryStream mStream = new MemoryStream();
StreamWriter writer = new StreamWriter(mStream,System.Text.Encoding.UTF8);
writer.Write(@"" );
writer.Write(Customer.Name);
StreamReader sr = new StreamReader(mStream);
writer.Flush();
mStream.Position = 0;
return sr.ReadToEnd();
}
It is not important what technology is used. I tried with PostSharp's ImplementMethodAspect but got nowhere (due to lack of experience with it). I also looked into Linfu framework. Can somebody suggest some other approach or way to do this, I would really appreciate. My whole project depends on this.
© Stack Overflow or respective owner