![](/img/trans.png)
[英]Could not load type 'System.Reflection.IntrospectionExtensions' from assembly 'mscorlib
[英]Get type from assembly load using reflection
project1具有class1和interface1。 Class1實現interface1。 我有另一個項目,它將使用interface1測試此類class1方法。 現在的問題是我必須動態加載project1.dll並使用接口方法來調用class1方法。 為此,我正在使用反射加載project1.dll。 現在,我從接口獲取methodInfo,並且在調用此方法之前,我應該創建將調用該方法的類的實例。 要使用activator.createInstance創建類的實例,我需要知道構造函數的參數。 現在,這些構造函數參數為自定義類型。 如前所述,我必須動態加載dll。 那么有沒有辦法從組裝負載中獲得類型呢? 或任何其他方法來實現上述想法? 下面是我的代碼。
Assembly assembly = Assembly.LoadFrom(@"D:\Project1.dll");
Type[] typeArray = assembly.GetTypes();
object obj;
//First create the instance of the class
foreach (Type type in typeArray)
{
if (type.Name == "Class1")
{
Type[] types = new Type[4];
//I am not able to get the below customParams from the loaded assembly.
//Is there a way to do this. Can this be done without adding reference?
types[0] = typeof(CustompParam1);
types[1] = typeof(CustompParam2);
types[2] = typeof(CustompParam3);
types[3] = typeof(CustompParam4);
obj = Activator.CreateInstance(types);
}
}
//use the instance of the class to invoke the method from the interface
foreach (Type type in typeArray)
{
if (type.Name == "Interface1")
{
MethodInfo[] mInfo = type.GetMethods();
foreach (MethodInfo mi in mInfo)
{
mi.Invoke(obj, null);
}
}
}
您可以像創建類實例一樣獲取構造函數參數的實例
通過類型名稱查找它們,然后調用Activator.CreateInstance作為參數類型,然后將其放入原始類的Activator.CreateInstance中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.