Jump to content
Xtreme .Net Talk

System.TypeLoadException dynamically loading classes from DLLs


Recommended Posts

  • *Experts*
Posted (edited)

[PLAIN][resolved] System.TypeLoadException dynamically loading classes from DLLs[/PLAIN]

 

OK - I am writing an application that allows you to add plug-ins DLLs that implement a specific license.

 

So in the .config file I hagve something like:-

  <dataSchemaProvidors>
  <!-- The plug in providors that implement the DataSchemabase classes to write out
       the DDL for creating the database objects on the target database platform
       Key= The unique name that the providor is known by
       ImplementingAssembly = The assembly that holds the providor
       ImplementingClassType = The class that implements the providor
    -->
    <dataSchemaProvidor Key="SQL Server" 
                        ImplementingAssembly="SQLServerDataSchema.dll"
                        ImplementingClassType="SQLServerDatasource"/>
  </dataSchemaProvidors>

 

Then in a totally seperate project I create a DLL called "SQLServerDataSchema.dll" and it exports a public class "SQLServerDatasource". My container program is supposed to instantiate it by:-

 

#Region "Instance"
   Public ReadOnly Property Instance() As Object
       Get
           If _Instance Is Nothing Then
               _Instance = Activator.CreateInstanceFrom(_ImplementingAssembly, _ImplementingClassType)
           End If
           Return _Instance
       End Get
   End Property
#End Region

 

Where _ImplementingAssembly holds the DLL name and _ImplementingClassType the class name in that DLL.

 

However I am getting an exception:

Additional information: Could not load type SQLServerDatasource from assembly SQLServerDataSchema, Version=1.0.1700.16719, Culture=neutral, PublicKeyToken=null.

 

Resolution

 

Ity turns out that even though no namespace was specified the DLL name was used as a default namespace so the config file should have held:-

<dataSchemaProvidor Key="SQL Server" 
                        ImplementingAssembly="SQLServerDataSchema.dll"
                        ImplementingClassType="SQLServerDataSchema.SQLServerDatasource"/>

Edited by Merrion
Printer Monitor for .NET? - see Merrion Computing Ltd for details
Posted
Which value did you try for _ImplementingAssembly and _ImplementingClassType ?

"If someone say : "Die mortal !"... don't stay to see if he isn't." - Unknown

"Learning to program is like going out with a new girl friend. There's always something that wasn't mentioned in the documentation..." - Me

"A drunk girl is like an animal... it scream at everything like a cat and roll in the grass like a dog." - Me after seeing my girlfriend drunk and some of her drunk friend.

C# TO VB TRANSLATOR

  • *Experts*
Posted

There's no namespace and the values _ImplementingAssembly and _ImplementingClassType are being read from the .config file OK.

 

Strangely when I do this:-

#Region "Instance"
   Public ReadOnly Property Instance() As Object
       Get
           If _Instance Is Nothing Then
               Try
                   '
                   Dim hdlSample As ObjectHandle
                   hdlSample = Activator.CreateInstanceFrom(_ImplementingAssembly, _ImplementingClassType)
                   _Instance = hdlSample.Unwrap
               Catch ex1 As System.TypeLoadException
                   Console.Write(ex1.GetBaseException.ToString)
                   'Try to find the class yourself...
                   Dim assyPlugIn As System.Reflection.Assembly
                   Dim fiAssy As New System.IO.FileInfo(_ImplementingAssembly)
                   If fiAssy.Exists Then
                       Try
                           assyPlugIn = AppDomain.CurrentDomain.Load(_ImplementingAssembly)
                       Catch fnf As System.IO.FileNotFoundException
                           Console.WriteLine("Could not find " & fnf.FileName)
                           Exit Property
                       End Try
                   Else
                       Exit Property
                   End If
                   Dim typThis As Type
                   For Each typThis In assyPlugIn.GetTypes
                       Console.WriteLine(typThis.Name)
                       If Not typThis.GetInterface("IDataSource", True) Is Nothing Then
                           Console.WriteLine("Gotcha")
                       End If
                   Next
               End Try
           End If
           Return _Instance
       End Get
   End Property
#End Region

 

It gets to the line Console.WriteLine("Could not find " & fnf.FileName) which means that the FileInfo class sees the file but the CurrentDomain.Load() doesn't.

 

Very strange...

Printer Monitor for .NET? - see Merrion Computing Ltd for details
Posted
I knew that an "Expert" would resolve this by his own. :p

"If someone say : "Die mortal !"... don't stay to see if he isn't." - Unknown

"Learning to program is like going out with a new girl friend. There's always something that wasn't mentioned in the documentation..." - Me

"A drunk girl is like an animal... it scream at everything like a cat and roll in the grass like a dog." - Me after seeing my girlfriend drunk and some of her drunk friend.

C# TO VB TRANSLATOR

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...