Hello 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
er) Activator.CreateInstance(t
ype));
}
}
foreach (IContentParser parser in _parsers)
_parserToStrings.Add(parse
r.ToString
());
}
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
yAssembly)
) and the parsing factory will be updated with the new class. That is certainly a mouthful and I hope you are still with me!
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.
Start Free Trial