I want to use a class name sting to new a class
NameSpace is Mcs.ControlMaster
Class Name is HostTransportCommand
after check some posts here. I use Activator
var msg = Activator.CreateInstance(Type.GetType(“Mcs.ControlMaster.HostTransportCommand”, true));
got exception
System.TypeLoadException
HResult=0x80131522
Message=Could not load type 'Mcs.ControlMaster.HostTransportCommand' from assembly 'Mcs.ControlMaster.UT, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
Source=<Cannot evaluate the exception source>
StackTrace:
<Cannot evaluate the exception stack trace>
OKAY, the execution assembly is Mcs.ControlMaster.UT now 。and this class is in Mcs.ControlMaster. then
string fileToLoad = @"Mcs.ControlMaster.exe";
AssemblyName assamblyName = AssemblyName.GetAssemblyName(fileToLoad);
// assamblyName={Mcs.ControlMaster, Version=3.0.19.320, Culture=neutral, PublicKeyToken=null}
AppDomain myDomain = AppDomain.CreateDomain("MyDomain");
//myDomain={System.Runtime.Remoting.Proxies.__TransparentProxy}
Assembly myAssambly = myDomain.Load(assamblyName);
//myAssambly={Mcs.ControlMaster, Version=3.0.19.320, Culture=neutral, PublicKeyToken=null}
var myFunc = myAssambly.CreateInstance("HostTransportCommand");
// myFunc = null
if use
Type.GetType(“Mcs.ControlMaster.UT, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null”, true));
will get
System.IO.FileLoadException: 'The given assembly name or codebase was invalid.
How to solve this issue?
Aucun commentaire:
Enregistrer un commentaire