[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

9.2

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.
 
Related Solutions
Keywords: Reflection: how to add a class to ass…
 
Loading Advertisement...
 
[+][-]01/01/08 01:32 AM, ID: 20559151Assisted Solution

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

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

 
[+][-]01/01/08 07:59 AM, ID: 20560322Accepted Solution

View this solution now by starting your 30-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/08 08:42 PM, ID: 20592533Author Comment

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 30-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091021-EE-VQP-81 / EE_QW_2_20070628