Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Table Driven Menus
Message
De
20/11/2009 01:24:05
 
 
À
19/11/2009 23:21:51
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Divers
Thread ID:
01435073
Message ID:
01435752
Vues:
41
My other reply should help explain what has gone wrong here.

First, your classname needs the full namespace, so it should be
            string classname = "SimpleClassLibrary1.Class1";
I mentioned in my other reply about the use of interfaces. If you didn't use an interface that was common to a known set of dynamically instantiated objects, then you'd have to have a reference in your solution to Class1 ... which defeats the purpose of what we're trying to accomplish here.

So, say you want to have the ability to instantiate a bunch of different classes that all will implement the IShowMessage interface (which contains a method called showmessageX). So, both your SimpleClassLibrary and your other solution would both need to have that IShowMessage interface included in them.

Then your Class1 would be:
namespace SimpleClassLibrary1
{
    public class Class1 : IShowMessage
    {
        //method
        public bool showmessageX(string message)
        {
            MessageBox.Show(message);
            return true; 
        }
    }
}
And you'd instantiate and call the method like this:
            IShowMessage o = oReflection.InstantiateClass(ref message) as IShowMessage;
            if (o == null)
                MessageBox.Show(message);
            else
            {
                o.showmessageX(message);
                
            }
Does that help?

~~Bonnie




>Hi Bonnie
>
>After reading your blog this is what I did:
>
>1. I created a class which will be dynamically loaded and its methods called using reflection - SimpleClassLibrary1 and compiled it to a DLL and closed the project.
>
>namespace SimpleClassLibrary1
>{
>    public class Class1
>    {
>        //method
>        public bool showmessageX(string message)
>        {
>            MessageBox.Show(message);
>            return true; 
>        }
>    }
>}
>
>
>2. I then created a new solution and created your MyReflectionClass exactly as you showed in your blog. The only change is I called it bbReflection.
>
>3. I added a new windows forms project to the above solution and added a reference to the above assembly called bbReflection
>
>4. On the form I added a button and in its click added this code to test:
>
>private void button1_Click(object sender, EventArgs e)
>        {
>            string assembly = "C:\\VSX2008\\MyProjects\\bbReflection\\SimpleClassLibrary1\\bin\\Release\\SimpleClassLibrary1.dll";
>            string classname = "class1";
>            MyReflectionClass oReflection = new MyReflectionClass(assembly, classname);
>
>            string message = "";
>            object o = oReflection.InstantiateClass(ref message);
>            if (o == null)
>                MessageBox.Show(message);
>            else
>            {
>                // go on with your processing
>                
>            }
>        }
>
>
>Everything compiles ok (no errors) but when I click the button, a message box with nothing appears. On stepping through the code, it is because
>
>object o = oReflection.InstantiateClass(ref message);
>
>
>always returns null.
>
>What am I doing wrong here?
>
>All I want to do is in the part where you have
>
>// go on with your processing
>
>I want to call the method in bbReflection.Class1 - showmessageX to display the message, but it never runs that code because o is always null.
>
>and this is because of this - oAssembly is not null but the CreateInstance returns null always.
>
>if (oAssembly != null)
>       oClass = oAssembly.CreateInstance(this.m_ClassName);
>
>
>Also what would be the syntax for calling the showmessageX method passing it a message to display?
>
>Please help.
>
>Bernard
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform