If you only allow user to enter regular expression, would it be better to generate RegEx on the fly and refactory your code to make use of RegEx class instead of creating IContentParser for each regular expression.
Main Topics
Browse All TopicsHello experts,
I am building a utility program to allow users to create their own classes that do certain regular expressions. I cannot allow users to code anything themselves, so what I would like to do is have users enter desired regular expression in to windows forms text box, enter name of regular expression (which will become the class name), then click "Add".
After clicking "Add" RegexClassWriter.cs is instanced and it uses a template to create a new .cs class that implements IContentParser. This new class is saved to local disk in folder called /ParsingStrategies.
To finish the scenario, the user will then start a main program. This main program is windows forms project that has a listBox. This listBox is populated with all types matching IContentParser that exist in assembly GURL.Parsing.Tools.dll. If you have been paying attention you will now say, "wait, this doesn't load the files in /ParsingStrategies that the user created with the utility program". And you are right! This is where I am stuck. I can use reflection to invoke classes already in assembly GURL.Parsing.Tools.dll. But how do I add the .cs files (the implement IContentParser) that the user saved to /ParsingStrategies into assembly GURL.Parsing.Tools.dll?
Once the classes are in the assembly I have no problem using reflection to instance them (and use them). In fact, I wrote a strategy factory that loads parsing strategies dynamically at runtime. In the factory constructor I call initialize():
private void initialize(Assembly asm)
{
Type[] types = asm.GetExportedTypes();
foreach (Type type in types)
{
Type[] iFaces = type.GetInterfaces();
foreach (Type face in iFaces)
{
if (face.Name == "IContentParser")
_parsers.Add((IContentPars
}
}
foreach (IContentParser parser in _parsers)
_parserToStrings.Add(parse
}
Which works great: it loads each class that implements IContentParser that has been built in to assembly GURL.Parsing.Tools.dll.
But back to my utility program. I would like to be able to generate a new class that implements IContentParser and load it in to GURL.Parsing.Tools.dll ***OR*** once user saves .cs file (that has not been loaded in to GURL.Parsing.Tools.dll) to /ParsingStrategies be able to change initialize() method above to load these new classes in to GURL.Parsing.Tools.dll and then use reflection to instance them (as I do above). Then (since GURL.Parsing.Tools.dll is dynamically invoked, with variable name myAssembly) I may call initialize(Assembly.Load(m
Anyone know how to do this? I might be off a bit in my explanation (I am new to this technique). Please feel free to ask questions to clarify.
Thanks,
sapbucket.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Thanks all for your help experts! I split the points among the two of you. I have gone with testn's suggestion. My solution involves storing Regex class in xml file. I then created a RegexXMLReader and RegexXMLWriter utility class. It works great, and it doesn't require reflection.
However, I am still very interested in this topic. If I ever do figure it out I will be sure to post my solution here.
Author's comment:
Thanks all for your help experts! I split the points among the two of you. I have gone with testn's suggestion. My solution involves storing Regex class in xml file. I then created a RegexXMLReader and RegexXMLWriter utility class. It works great, and it doesn't require reflection.
However, I am still very interested in this topic. If I ever do figure it out I will be sure to post my solution here.
--------------------------
You're welcome :)
I would always use the RegEx class if it is directly suitable, and storing the pattern to a file is a good solution for this problem. Of course it doesn't answer the original question which was specifically about code generation...
Business Accounts
Answer for Membership
by: SteveH_UKPosted on 2008-01-01 at 01:32:33ID: 20559151
The first problem you have is that your new class is not compiled. You have a few options here:
eProvider class to generate the code
mbly) to unload it.
1. Execute the csc tool to compile the code whenever you need it
2. Use the System.Reflection.Emit namespace to generate the corresponding code
3. Use the Microsoft.CSharp.CSharpCod
I recommend trying the 3rd option and using the CompileAssemblyFromFile method. I also recommend that you include all custom classes in the compilation, not just the new one. It will be difficult to add a class to an existing built assembly.
Now since you will be overwriting an existing linked assembly, you'll need to load the assembly manually and be able to unload it. To do this, you need to create a new AppDomain using AppDomain.Create and then use AppDomain.Load to load the assembly. I believe that if you use Assembly.LoadFrom then the assembly will load into the default or executing AppDomain which is not what you want. Later, you will need to use AppDomain.Unload(toolsAsse