473,407 Members | 2,598 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

CodeDOM Delegate

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?
May 3 '07 #1
2 2302
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?

May 3 '07 #2
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?


May 4 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Ron Bullman | last post by:
Hi, I havent been able to find any documentation about @ in the CodeDom context. The problem is when generating C# code from CodeDom it will associate a @-tag with all C# specific types. Why?...
2
by: Guillaume | last post by:
I don't think I'm the only one with this challenge so… I made code with the CodeDom classes and made code to create a new solution and a new project Now I want to get the codedom code in the new...
1
by: John Livermore | last post by:
Hello, We have an application that uses CodeDom for dynamic in-memory compilation of code. The same code (library) is used by both an ASP.net web site and a Windows Service. The code runs fine...
3
by: Alexey Lavnikov | last post by:
I'm playing with CodeDOM and CSharpCodeProvider. I've found the following possibilities: CodeDOM -> Assembly C# Source code -> Assembly I miss the C# Source code -> CodeDOM only. Is it there?...
9
by: lee.chappers | last post by:
How can I make the CodeDom generate the following C# method? protected sealed override void Test() { } I've tried using: domMethod.Attributes = MemberAttributes.Override |...
6
by: moondaddy | last post by:
will CodeDom create XAML windows or just regular windows forms? If so, how to I tell it to create a xaml window instead of a windows form? thanks -- moondaddy@noemail.noemail
2
by: CodeMonkey775 | last post by:
I'm having problems passing a variable to a method which is executed and compiled using CodeDom. The situation is I have a List<CellData> with cells, each containing a formula (like Excel). I am...
1
by: Niels Ull | last post by:
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...
1
by: =?Utf-8?B?QU1lcmNlcg==?= | last post by:
I may have painted myself into a corner with GenerateInMemory=true. My app need a custom user step. Users want to code (sort of - they are not programmers) some refinements to a search...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.