views:

362

answers:

2

The constructor looks like this:

public NameAndValue(string name, string value)

I need to get it as a MethodInfo using Reflection. It tried the following, but it does not find the constructor (GetMethod returns null).

MethodInfo constructor = typeof(NameAndValue).GetMethod(".ctor", new[] { typeof(string), typeof(string) });

What am I doing wrong?

+2  A: 

Type.GetConstructor. Note this returns a ConstructorInfo rather than a MethodInfo, but they both derive from MethodBase so have mostly the same members.

Phil Devaney
+2  A: 
ConstructorInfo constructor = typeof(NameAndValue).GetConstructor
        (new Type[] { typeof(string), typeof(string) });

You should have the elements you need in the ConstructorInfo, I know of no way to get a MethodInfo for a constructor though.

Kindness,

Dan

Daniel Elliott
I already knew how to get the ConstructorInfo, but I thought I needed MethodInfo, while only MethodBase was required. Thanks for your answer though.
Hermann