简体   繁体   English

具有“反射”参数的C#中的InvokeMember

[英]InvokeMember in c# with “reflected” parameters

I want to call a method of a DLL via reflection (can't be sure that the other dll is loaded) but have problems with the parameters. 我想通过反射调用DLL的方法(不能确保已加载其他dll),但是参数有问题。

The method I want to call is 我想调用的方法是

public void Add(DBTable table, String sField, DBValue value, SQLConditionType type)

in the MP-TVSeries project MP-TVSeries项目中

What I tried is this: 我试过的是:

        // WindowPlugins.GUITVSeries
        Assembly MPTVSeries = Assembly.Load("MP-TVSeries");
        Type sqlConditionType = MPTVSeries.GetType("WindowPlugins.GUITVSeries.SQLCondition");
        Type sqlConditionTypeEnumType = MPTVSeries.GetType("WindowPlugins.GUITVSeries.SQLConditionType");
        Type dbEpisode = MPTVSeries.GetType("WindowPlugins.GUITVSeries.DBEpisode");

        // SQLCondition sql = new SQLCondition();
        object sql = Activator.CreateInstance(sqlConditionType);

        // sql.Add(new DBEpisode(), DBEpisode.cFilename, filename, SQLConditionType.Equal);
        sqlConditionType.InvokeMember("Add",
        BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Public,
        null,
        sql,
        new object[] { 
            Activator.CreateInstance(dbEpisode),
            dbEpisode.GetField("cFilename"),
            filename,
            sqlConditionTypeEnumType.GetField("Equal")
        });

but this throws an exception with the message 但这会引发异常消息

The method "WindowPlugins.GUITVSeries.SQLCondition.Add" could not be found. 找不到方法“ WindowPlugins.GUITVSeries.SQLCondition.Add”。

My guess is that I am doing something wrong with the parameters, but as I am totally new to reflection I can't get my head around it. 我的猜测是我在参数上做错了,但是由于我是反射的新手,所以我无法理解。

Someone please help ;-) 有人请帮助;-)

You are going too fast. 你太快了。 Get sqlConditionType.GetMethod() working first to get a MethodInfo so you can be sure this is not a method overload resolution problem. 首先获取sqlConditionType.GetMethod()以获取MethodInfo,以便可以确定这不是方法重载解析问题。 The arguments you pass are smelly, particularly filename and sqlConditionTypeEnumType.GetField("Equal") . 您传递的参数很脏 ,尤其是filenamesqlConditionTypeEnumType.GetField(“ Equal”)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM