2

I am getting a

Method 'MyNameSpace.MyClass.MyMethod' not found.

when I changed a parameter of MyMethod from Hashtable to Dictionary<string, string>.

The invoke call is

return = t.InvokeMember("MyMethod", (BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod), null, instance, params);

When I do

Type type = a.GetType(String.Concat(DLLName, ".MyClass"));
var methods = t.GetMethods();

methods contains MyMethod() so it is there.

Can anyone shed any light?

The params are

Object[] params = new Object[11];
...
params[5] = foo.myHashtable.Cast<DictionaryEntry>().ToDictionary(d => d.Key, d => d.Value);   
...

The MyMethod signature is

public MyMethodReturn MyMethod(Byte[] m, Hashtable d, Mutex mut, FileStream logFile, Hashtable t, Dictionary<string, Byte[]> fields, bool e, byte[] k, int hashCode, bool h, Byte[] mm)
Sam Leach
  • 12,746
  • 9
  • 45
  • 73
  • `foo.myHashtable.Cast().ToDictionary(d => d.Key, d => d.Value);` doesn't actually put the dictionary anywhere - it just drops it on the floor for the garbage collector to deal with; did you assign this to a variable or anything? Better: can you show the *actual* code that *actually* calls `InvokeMember` with your inputs? And preferably the *actual* signature of `MyMethod` (because I have concerns about what may be lurking in the `...`) – Marc Gravell Jul 01 '13 at 09:47
  • I didn't copy & paste correctly. Edited. Okay. – Sam Leach Jul 01 '13 at 09:50

2 Answers2

1

You have:

params[5] = foo.myHashtable.Cast<DictionaryEntry>()
               .ToDictionary(d => d.Key, d => d.Value); 

This creates a Dictionary<object,object>, which does not match the signature. This is because a DictionaryEntry has object Key {get;} and object Value {get;}, and the compiler is using those to infer the types for the dictionary (generic type inference).

Try:

params[5] = foo.myHashtable.Cast<DictionaryEntry>()
               .ToDictionary(d => (string)d.Key, d => (byte[])d.Value); 

This will create a Dictionary<string,byte[]>, which should match.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
0

Reflection can not find the method because the method expects a hash table not a dictinary, dictionary doesn't inherit from hashtable so you can not use dictionary instead of hashtable. Method signiture should match before reflection can invoke a method.

Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
Alexandr Mihalciuc
  • 2,537
  • 15
  • 12