Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / September 2007

Tip: Looking for answers? Try searching our database.

CodeDom - can I generate an anonymous methodmethodwi?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Niels Ull - 12 Sep 2007 12:12 GMT
Hi!

I'm using .Net 2.0 and C#, and I'm trying to generate code for build time
AOP. But I cannot find out how to generate an anonymous delegate.

E.g. generating code like this:

class MyClassWrapper
{
  public static void StaticMethod(int param1, int param2)
  {
      DbContext.DoInTransaction( delegate { MyClass.StaticMethod(param1,
param2} );
  }
}

Background: This is based on having MyClass

class MyClass
{
  [TransactionRequired]
  public static void StaticMethod(int param1, int param2) {...}
}

But apparently I cannot generate this with the CodeDOM?

My best approach so far is to do a C# roundtrip - e.g.

private CodeExpression GetAnonymousDelegateExpression(CodeStatement statementToWrap)
{
     CodeExpression delegateExpression;
     CSharpCodeProvider csc = new CSharpCodeProvider();
     StringWriter sw = new StringWriter();
     csc.GenerateCodeFromStatement(statementToWrap, sw, new CodeGeneratorOptions());
     delegateExpression = new CodeSnippetExpression("delegate {" + sw.ToString()
+ "}");
     return delegateExpression;
}

but that seems very clunky? Any ideas?
Having to generate my own randomly-named method will be a bit complex since
I have to handle capturing outer variables
Nicholas Paldino [.NET/C# MVP] - 12 Sep 2007 15:56 GMT
Neils,

   Unfortunately, I don't think that the CodeDom namespace has been updated
to accomidate anonymous methods.  It also doesn't seem like it's going to be
updated for lambda expressions either.  If your snippet works (I haven't
tested it), then I would say that's the way you have to go.

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

> Hi!
>
[quoted text clipped - 38 lines]
> randomly-named method will be a bit complex since I have to handle
> capturing outer variables.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.