Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Help needed with Reflection
Message
De
25/08/2007 17:12:31
 
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Divers
Thread ID:
01250136
Message ID:
01250462
Vues:
22
Pete,

I successfully do this in a slightly different manner. I use the IsSubClassOf() method of Type. I ran into an issue using the "as" keyword and found that a regular cast, "(thetype)", works when the "as" doesn't. And also, I don't use "Activator", I use the assembly I just loaded, I don't know if that's where your problem lies or not, but try it this way:
private void frmPluginTest_Load(object sender, EventArgs e)
{
    string[] dllFilenames = Directory.GetFiles(@"P:\PJKPlugin\TabPagePlugins","TabPage*.dll");
    foreach (string filename in dllFilenames)
    {
        try
        {
            Assembly asm = Assembly.LoadFrom(filename);
            Type[] typesInAssembly = asm.GetTypes();
            foreach (Type type in typesInAssembly)
            {
                 if (type.IsSubClassOf.GetInterface(typeof(PJKPlugins.ICMSTabPage)))
                 {
                      PJKTabPage oTP = (PJKTabPage)asm.CreateInstance(type.Namespace + "." + type.Name);
                      this.PluginTabControl.Controls.Add(oTP);
                 }
             }
         }
         catch
         {
              // Not a valid assembly, move on
         }
    }
}
~~Bonnie





>Hi All, I'm trying to dynamically load some TabPages that are defined in seperate dlls using the code below, the problem I'm having is with the line : "PJKTabPage oTP = Activator.CreateInstance(type) as PJKTabPage;" where PJKTabPage is the name of the class, the class loads ok ( I've stepped through the code with the debugger ) but the variable oTP is always null, can't understand why ?
>
>
>
>private void frmPluginTest_Load(object sender, EventArgs e)
>{
>    string[] dllFilenames = Directory.GetFiles(@"P:\PJKPlugin\TabPagePlugins","TabPage*.dll");
>    foreach (string filename in dllFilenames)
>    {
>        try
>        {
>            Assembly asm = Assembly.LoadFrom(filename);
>            Type[] typesInAssembly = asm.GetTypes();
>            foreach (Type type in typesInAssembly)
>            {
>                 if (null != type.GetInterface(typeof(PJKPlugins.ICMSTabPage).FullName))
>                 {
>                      PJKTabPage oTP = Activator.CreateInstance(type) as PJKTabPage;
>                      this.PluginTabControl.Controls.Add(oTP);
>                 }
>             }
>         }
>         catch
>         {
>              // Not a valid assembly, move on
>         }
>    }
>}
>
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