How to get constructor as MethodInfo using Reflection

user65199 picture user65199 · Sep 4, 2009 · Viewed 9.7k times · Source

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?

Answer

Phil Devaney picture Phil Devaney · Sep 4, 2009

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