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.
The method I want to call is
public void Add(DBTable table, String sField, DBValue value, SQLConditionType type)
in the MP-TVSeries project
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开发者_JAVA技巧(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.
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. The arguments you pass are smelly, particularly filename and sqlConditionTypeEnumType.GetField("Equal").
精彩评论