C# 加载程序集调用方法返回自定义类型
希望有人能帮我。例如,基本上我必须动态加载2个程序集C# 加载程序集调用方法返回自定义类型,c#,reflection,return-value,usertype,C#,Reflection,Return Value,Usertype,希望有人能帮我。例如,基本上我必须动态加载2个程序集 ObjA= Assembly.LoadFrom(folder + module); //had the main methods of the integration ObjB = Assembly.LoadFrom(folder + module_decl); //has more interface declarations for the intigration Type intObj ; Type[] type2 = o
ObjA= Assembly.LoadFrom(folder + module); //had the main methods of the integration
ObjB = Assembly.LoadFrom(folder + module_decl); //has more interface declarations for the intigration
Type intObj ;
Type[] type2 = objB.GetTypes();
foreach (Type t in type2)
{
if ((intObj == null) && (t.FullName == "IIReturnInterfaceDecl"))
{
intObj = t;
}
}
现在,ObjB
在objA
中有一个方法的返回值声明。因此,我首先尝试找到这个返回值,并创建它的一种类型
ObjA= Assembly.LoadFrom(folder + module); //had the main methods of the integration
ObjB = Assembly.LoadFrom(folder + module_decl); //has more interface declarations for the intigration
Type intObj ;
Type[] type2 = objB.GetTypes();
foreach (Type t in type2)
{
if ((intObj == null) && (t.FullName == "IIReturnInterfaceDecl"))
{
intObj = t;
}
}
然后我去创建一个主类的实例并调用该方法
foreach (Type t in types)
{
mi = t.GetMethod("CreateTheObjectMethod");
if (mi != null)
{
string typeName = t.FullName;
object lateBoundObj = null;
lateBoundObj = Activator.CreateInstance(t);
intObj = lateBoundObj.GetType().InvokeMember("CreateTheObjectMethod",
//BindingFlags.Public | BindingFlags.InvokeMethod | BindingFlags.Instance,
BindingFlags.InvokeMethod | BindingFlags.GetProperty,
null,
lateBoundObj,
args);
if (intObj == null)
{
MessageBox.Show("oops");
}
else
{
MessageBox.Show("done");
}
//
// break;
}
//result
}
}
else
{
MessageBox.Show("DAMMIT");
}
但基本上我无法让它调用方法并以我想要的方式返回对象(上面的代码没有编译,因此会出现错误)
错误1无法将类型“object”隐式转换为“System.type”。错误
存在显式转换(是否缺少强制转换?)
但我不知道如何让它转换成正确的类型
请注意,我加载的外部程序集是第三方的,我没有标题或声明,我可以访问它们,但我没有为我声明的接口或类,以便于从中进行转换,我必须动态执行
非常感谢您的指导。我在“委托”上已经搜索了很多,看到了很多,但我看到的最多的是调用和传递值,而不是处理返回的动态用户类型值并使用它们
谢谢你的帮助
//编辑:if语句中有错误,对于那些已经响应的语句,很抱歉,必须是:if(intObj==null)而不是if(CreateTheObjectMethod==null)
这可能会有所帮助,这基本上是我调用的方法的声明
[System.Reflection.RuntimeMethodInfo] = {IIReturnInterfaceDecl CreateTheObjectMethod(System.String, System.String)}
你试过这样的吗
mi = t.GetMethod("CreateTheObjectMethod");
if (mi != null)
{
string typeName = t.FullName;
object lateBoundObj = null;
lateBoundObj = Activator.CreateInstance(t);
object returnValue = mi.Invoke(lateBoundObj,null);//pass your params instead of null if you need
//here you can check what return value is.
Type returnedType = returnValue.GetType();
if (CreateTheObjectMethod== null)
{
MessageBox.Show("oops");
}
else
{
MessageBox.Show("done");
}
//
// break;
}
intObj
属于System类型。键入
并尝试将InvokeMember
的返回值分配给它。但是返回值是object
。这就是错误消息告诉您的
您可以简单地将返回值强制转换为类型
,但我怀疑这是否正确。这将消除编译器错误,但如果调用的方法(CreateTheObjectMethod
)的返回值不是类型
,您需要将返回值赋给返回类型为
CreateTheObjectMethod
的变量,并相应地强制转换InvokeMember
的返回值。哪一行出现此错误?在创建类型的实例后,为什么要再次尝试获取类型?与要运行的方法相同。@Daniel Hilgarth-我明白了调用“intObj=lateBoundObj.GetType().InvokeMember”(“CreateToObject方法”)时出错,//BindingFlags.Public | BindingFlags.InvokeMethod | BindingFlags.Instance,BindingFlags.InvokeMethod | BindingFlags.GetProperty,null,lateBoundObj,args);“@Dilshod-更多的是在看类型(有一个messagebox在循环中为我弹出了该类型,对于该方法,我只是在找到一个包含该方法时才进一步使用它。。因此,在绝望之后的一天结束时会有更多的线索和错误代码。@Soner Gönül-感谢您修复我的帖子,对此仍然非常陌生。这是我在开始创建时尝试的“intObj=t;”我是为接口类型(t.FullName==“IIReturnInterfaceDecl”)执行此操作的。但是,我遇到了错误,不知道如何强制方法调用返回该类型(强制转换对象返回值)@Johannes:你混淆了类型type
的实例和类型iReturnInterfacedeCl
的实例。在这种情况下,intObj
只需要类型object
,因为我认为类型iReturnInterfacedeCl
在编译时是未知的。这是我的想法,但如果我进行正常调用的话对它进行调用并使其成为object类型的intObj,那么它总是返回null。这就是为什么我开始尝试这个方法。但是如果我在设计时将程序集拉入IDE,那么我可以毫无错误地执行这些调用,但动态地不行。不幸的是,我需要它们是动态的。@Johannes:但是返回null
的方法与此无关返回值分配给的类型…在您的代码中,您正在检查CreateTheObjectMethod
是否为null
,但您正在将返回值分配给intObj
。也许这就是问题所在?抱歉,我刚刚意识到,我已将其粘贴到那里,而不是intObj,我正在检查intObj是否为null,只是更改了名称对于post etc,不要泄露代码。我会尝试看看我是否能找到编辑原始post的方法,对此很抱歉。但基本上我会检查InObject是否为null(如果它得到了任何东西或没有)。sorryno不是真的,但它会自动创建该方法的对象吗?我会尝试。但不知道这部分“if(CreateTheObjectMethod==null)“@Johannes我不知道你到底想要什么。你说的“创建方法的对象”是什么意思?很抱歉,我现在意识到我在if语句中粘贴了错误的文本,它一定是if(intObj==null),而不是if(CreateTheObjectMethod==null),很抱歉,我将查看是否有办法编辑原始代码,但基本上我想调用方法“CreatObjectOfMethod”和I来使用其返回值。但我无法强制转换为其返回值类型,因为该类型是usertype(类)我在编译时没有,只有运行时。如果我只做object类型的返回值,我在整个过程中只得到null。这可能会有所帮助,这基本上是我调用的方法的声明[System.Reflection.RuntimeMethodInfo]={IIReturnInterfaceDecl CreateTheObject方法(System.String,System.String)}你试过我写的例子了吗?你调试过吗?它是否返回了你想要的对象?