0
var leftCurrent = leftArray.GetValue(i);
var rightCurrent = rightArray.GetValue(i);

var mi = typeof (PropertyCompare).GetMethod("NotEqualProperties");
mi.MakeGenericMethod(leftCurrent.GetType());

var notEqualProps = mi.Invoke(null,new []{leftCurrent, rightCurrent});

if(notEqualProps != null) 
    result.Add(new ArraysDiffResult(i, notEqualProps as List<string>));

Why does this code throws InvalidOperationException ( Late bound operations cannot be performed on types or methods for which ContainsGenericParameters is true.) ?

NotEqualProperties is static generic method..

UPD : I've already found solution. Just forgot to assign new MethodInfo...(Epic Fail..)

But how about performance?

illegal-immigrant
  • 8,089
  • 9
  • 51
  • 84

3 Answers3

1

MakeGenericMethod returns a new MethodInfo instance. (MethodInfo is immutable)

Your code creates this new instance, throws it away, then continues using the open (non-parameterized) MethodInfo.

You need to use the new instance, like this:

mi = mi.MakeGenericMethod(leftCurrent.GetType());

Yes; reflection is much slower than normal method calls.
However, unless you're calling it in a tight loop, it's not necessarily an issue.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
1

You didn't assign the result of

mi.MakeGenericMethod(leftCurrent.GetType());

to anything. Note that MakeGenericMethod does not mutate the invoking instance.

P.S Is this code much slower than calling method directly (without mi.Invoke) ?

Much? I don't know. The only way to know is to set performance benchmarks and to profile.

jason
  • 236,483
  • 35
  • 423
  • 525
  • Could you please share some links to VS profiler reviews/tutorials/samples ? – illegal-immigrant Dec 13 '10 at 16:44
  • @taras.roshko: http://www.google.com/search?sourceid=chrome&ie=UTF-8&q=nant+profiler+tutorial#sclient=psy&hl=en&q=ants+profiler+tutorial+review&aq=f&aqi=&aql=&oq=&gs_rfai=&pbx=1&fp=1 (I'm not trying to be a jerk here, I just don't know any specific reviews/tutorials/samples that are good. I can tell you that ANTS is very easy to use.) – jason Dec 13 '10 at 16:46
1

Oh, I'm stupid...It should be :

mi = mi.MakeGenericMethod(leftCurrent.GetType());

(Facepalm...). But how about performance?

illegal-immigrant
  • 8,089
  • 9
  • 51
  • 84