PC Review


Reply
Thread Tools Rate Thread

CodeDOM Delegate

 
 
Chuck B
Guest
Posts: n/a
 
      3rd May 2007
I have an object factory that creates customized objects with a method that
is dependant on the name of the object. Much of the method is similar but
some parameters within the method will be added from an XML dataset that
dependents on the name of the object. This method will be duplicated about
900 times with minor differences in the body.

Is it possible to create a string with a code snippet and compile it to an
executable method to be assigned to the object as a delegate at runtime?


 
Reply With Quote
 
 
 
 
Nicholas Paldino [.NET/C# MVP]
Guest
Posts: n/a
 
      3rd May 2007
Chuck,

You could always take the parts that are similar and have them as
methods in another class. Then, in the codegen'ed class you are creating,
just make the calls to the methods on the other class.


--
- Nicholas Paldino [.NET/C# MVP]
- (E-Mail Removed)

"Chuck B" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>I have an object factory that creates customized objects with a method that
>is dependant on the name of the object. Much of the method is similar but
>some parameters within the method will be added from an XML dataset that
>dependents on the name of the object. This method will be duplicated about
>900 times with minor differences in the body.
>
> Is it possible to create a string with a code snippet and compile it to an
> executable method to be assigned to the object as a delegate at runtime?
>



 
Reply With Quote
 
Chuck B
Guest
Posts: n/a
 
      4th May 2007
Thanks for the reply Nicholas.

I'm not actually generating an entire class. I'm using a base class as a
template to generate new objects each with a custom method. The method is
void with no calling parameters but its internal logic is controlled by the
contents of an XML file.

Essentially the entire project is a Model-View-Controller pattern with the
XML file being the user interface to the Controller. The Model is an
external application that is controlled thru automation. The base object is
treated identically thruout the application but must react differently
depending on it's Name property and the state that the Model is currently
in.

Internally it's a little complex but the idea is to simplify the external
interface to the point where anybody can control the Model with limited
training. For this I need to generate a custom method that wraps around the
XML commands. Something along the lines of:

public delegate void OnReportSetup();

class MyClass
{
private OnReportSetup OnSetup;
// stuff
}


// MyClass Object Factory

string topSnippet = "private void MyMethod()" + @"{\n" + @"// do stuff\n//
insert XML here";
string bottomSnippet = "}";
string xmlCode = ParseCommands();

CSharpCodeProvider codeProvider = new CSharpCodeProvider();
ICodeGenerator codeGenerator = codeProvider.CreateGenerator();

CodeSnippetCompileUnit compileUnit = new CodeSnippetCompileUnit( topSnippet
+ xmlCode + bottomSnippet );

// codeGenerator.GenerateCodeFromCompileUnit(compileUnit);

I'm not sure where to go from here. Essentially I want to assign the
compiled snippet to the OnSetup() delegate on the fly but don't quite how to
accomplish that.



"Nicholas Paldino [.NET/C# MVP]" <(E-Mail Removed)> wrote in
message news:%(E-Mail Removed)...
> Chuck,
>
> You could always take the parts that are similar and have them as
> methods in another class. Then, in the codegen'ed class you are creating,
> just make the calls to the methods on the other class.
>
>
> --
> - Nicholas Paldino [.NET/C# MVP]
> - (E-Mail Removed)
>
> "Chuck B" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>>I have an object factory that creates customized objects with a method
>>that is dependant on the name of the object. Much of the method is similar
>>but some parameters within the method will be added from an XML dataset
>>that dependents on the name of the object. This method will be duplicated
>>about 900 times with minor differences in the body.
>>
>> Is it possible to create a string with a code snippet and compile it to
>> an executable method to be assigned to the object as a delegate at
>> runtime?
>>

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to remove a given delegate from the delegate invocation list mehdi Microsoft C# .NET 2 18th Feb 2007 11:02 PM
Delegate issue - meetings only show up after delegate opened the request nils.pommerien@gmail.com Microsoft Outlook Calendar 0 21st Jul 2006 06:16 PM
Outlook 2002 - Delegate updates in delegate calendar? =?Utf-8?B?V2F6emE=?= Microsoft Outlook Calendar 0 1st Feb 2006 11:20 AM
delegate access - meetings appear in delegate calendar! =?Utf-8?B?TWljaGFlbCBZb3JrZQ==?= Microsoft Outlook Calendar 0 13th Jan 2006 04:46 PM
Delegate Gurus: Can I create a delegate for a property (as opposed to a method)? Bill Davidson Microsoft C# .NET 6 3rd Aug 2005 04:19 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:32 AM.