Advertisement

12.31.2007 at 04:32PM PST, ID: 23051538
[x]
Attachment Details

Reflection: how to add a class to assembly?

Asked by sapbucket in C# Programming Language, .NET

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((IContentParser) Activator.CreateInstance(type));
                }
            }

            foreach (IContentParser parser in _parsers)
                _parserToStrings.Add(parser.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(myAssembly)) 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
[+][-]01.01.2008 at 01:32AM PST, ID: 20559151

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]01.01.2008 at 07:59AM PST, ID: 20560322

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: C# Programming Language, .NET
Sign Up Now!
Solution Provided By: testn
Participating Experts: 2
Solution Grade: A
 
 
[+][-]01.05.2008 at 08:42PM PST, ID: 20592533

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628