I have an application that I would like third party developers to be
able to create Plug-ins that will be dynamically loaded into our
application to extend functionality.
I have utilized the "Let Users Add Functionality to Your .NET
Applications with Macros and Plug-Ins" article at MSDN for the dynamic
loading of DLLs
http://msdn.microsoft.com/msdnmag/is...s/default.aspx
We have an interface defined and everything was working fine. Users
would implement the interface and we would load the object dynamically
and our client application would properly implement the functionality.
I then went and create a Library class that would contain objects that
the third party developers need for utilization with their application
development. We have a reference to this Library class in our
Interface, and users need to implement properties that our part of the
library class. Upon adding this, and implementing the interface, our
dynamic loading of DLLs no longer functioned. I get a
ReflectionTypeL oadException error
"One or more of the types in the assembly unable to load."
System.SystemEx ception: {"One or more of the types in the assembly
unable to load."}
_classes: {Length=2}
_exceptions: {Length=1}
LoaderException s: {Length=1}
Types: {Length=2}
and if I look at the LoaderException s the message I get is
{"Com.Corp.Corp Image.CorpImage "}
System.SystemEx ception: {"Method get_CorpImageFi elds in type
Com.Corp.CorpIm age.CorpImage from assembly CorpImage,
Version=1.0.174 1.17159, Culture=neutral , PublicKeyToken= null does not
have an implementation. "}
AssemblyName: "CorpImage, Version=1.0.174 1.17159, Culture=neutral ,
PublicKeyToken= null"
ClassName: "CorpImage"
Message: "Method get_CorpImageFi elds in type
Com.Corp.CorpIm age.CorpImage from assembly CorpImage,
Version=1.0.174 1.17159, Culture=neutral , PublicKeyToken= null does not
have an implementation. "
MessageArg: "get_CorpImageF ields"
ResourceId: 6012
TypeName: "Com.Corp.CorpI mage.CorpImage"
I can get by the loading issue by skipping the loading of the Library
class when querying the types. While it's a work around, I am not
sure if it will enable everything to work down the road. I am also
unsure what I did :) Why does skipping trying to iterate the getTypes
of the Library dll enable things to work?
Again, the code is pretty similar to the one mentioned in the above
MSDN article. The change to the code I made to skip loading is as
follows...
private String[] DiscoverPluginA ssembliesHelper (
String path, PluginCriteria criteria, Type criteriaType)
{
String[] assemblies;
// Get .dll names
path = Path.Combine(Ap pDomain.Current Domain.BaseDire ctory, path);
assemblies = Directory.GetFi les(path, "*.dll");
try
{
if(assemblies.L ength != 0)
{
ArrayList assembliesInclu ded = new ArrayList();
foreach(String s in assemblies)
{
// Load the assembly (it it's not the control library)
if (s.ToLower().In dexOf("controll ibrary") < 0)
{
Assembly assembly = Assembly.LoadFr om(s);
// Find matching type?
Type[] types = assembly.GetTyp es();
foreach(Type t in types)
{
if(IncludeType( t, criteria, criteriaType))
{
assembliesInclu ded.Add(s);
break; // match found, move on
}
}
}
}
// Get array of matching assemblies
assemblies = (String[])assembliesIncl uded.ToArray(
typeof(String)) ;
}
}
catch (ReflectionType LoadException oEx)
{
throw new Exception("Erro r discovering plugin assemblies",oEx );
}
catch (Exception oEx)
{
throw new Exception("Erro r discovering plugin assemblies",oEx );
}
return assemblies;
}
Any insights greatly appreciated
Endymion Keats