Here is my code that loads a dll file and writes info about the file in xml:
MyOptions ass = MyOptionsList[MyOptionsList.Count - 1];
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
StringWriter sw = new StringWriter();
XmlSerializer serializer = new XmlSerializer(ass.GetType(),
new Type[] { typeof(MyOptions) });
serializer.Serialize(sw, ass);
//more code writing in an xml file
AppDomain.CurrentDomain.AssemblyResolve -= CurrentDomain_AssemblyResolve;
Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
return Assembly.LoadFrom(correctPath + ".dll");
}
The problem happens in the line serializer.Serialize(sw, ass);. The inner exception says that I can’t cast from one dll to another. Both dll files have the same name. One is on the correctPath, and the second is on some other path. How to resolve this?
MyOptions is a class library project from which the dll I am loading is extending.
I just tried the below with no problems, I think your problem must lie elsewhere.
Note that the event code isn’t called.