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(compileU nit);
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]" <mv*@spam.guard.caspershouse.comwrote in
message news:%2****************@TK2MSFTNGP05.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com
"Chuck B" <ch****@shc1.comwrote in message
news:ux**************@TK2MSFTNGP03.phx.gbl...
>>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?