Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Help needed with Reflection
Message
De
24/08/2007 11:04:19
 
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Divers
Thread ID:
01250136
Message ID:
01250223
Vues:
37
Hi Pete,

I do something similar in VB. I could not get CreateInstance to work either, but I was successful with CreateInstanceFrom().
Note that both those methods return a HANDLE that must be UnWrapped to get an object reference.
Public Function GetCustomScrubberDetails(ByVal iImportDefaultSeqNo As Integer) As ICustomScrubs

        Dim objDALClaimImport As New CommonParsingData
        Dim dsScrubberAssembly As New DataSet
        Dim hdlScrubber As System.Runtime.Remoting.ObjectHandle
        Dim ifCustomScrubs As ICustomScrubs

        Try
            Dim sLoc As String
            Dim sPath As String
            Dim sDll As String
            Dim sClass As String
            Dim asm As Reflection.Assembly

            dsScrubberAssembly = objDALClaimImport.RtnCustomScrubberDetails(iImportDefaultSeqNo)
            If dsScrubberAssembly.Tables.Count > 1 Then
                If dsScrubberAssembly.Tables("CustomScrubbers").Rows.Count > 0 Then
                    Dim drAssembly As DataRow
                    drAssembly = dsScrubberAssembly.Tables("CustomScrubbers").Rows(0)
                    sDll = drAssembly("Scrubber_Assembly_Name").ToString.Trim()
                    sClass = drAssembly("Class_Name").ToString.ToUpper.Trim()

                    asm = Reflection.Assembly.GetExecutingAssembly()
                    sLoc = asm.Location

                    sPath = Path.Combine(Path.GetPathRoot(sLoc), Path.GetDirectoryName(sLoc))
                    sDll = Path.Combine(sPath, sDll)

                    ' Note: The type parameter (sClass) of Activator.CreateInstanceFrom is CASE SENSITIVE!!!
                    hdlScrubber = Activator.CreateInstanceFrom(sDll, sClass)
                    ifCustomScrubs = DirectCast(hdlScrubber.Unwrap(), ICustomScrubs)

                End If

            End If

          Catch objEx As Exception

  
        Finally

            GetCustomScrubberDetails = ifCustomScrubs
 
        End Try

    End Function
>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
>         }
>    }
>}
>
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform