How to get constructor as MethodInfo using Reflection
10,097
Solution 1
Type.GetConstructor. Note this returns a ConstructorInfo rather than a MethodInfo, but they both derive from MethodBase so have mostly the same members.
Solution 2
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
Author by
Admin
Updated on June 12, 2022Comments
-
Admin almost 2 years
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
returnsnull
).MethodInfo constructor = typeof(NameAndValue).GetMethod(".ctor", new[] { typeof(string), typeof(string) });
What am I doing wrong?