构造函数如下所示:
public NameAndValue(string name, string value)
我需要使用反射将其作为 MethodInfo 获取。它尝试了以下操作,但没有找到构造函数(GetMethod
返回 null
)。
MethodInfo constructor = typeof(NameAndValue).GetMethod(".ctor", new[] { typeof(string), typeof(string) });
我做错了什么?
最佳答案
Type.GetConstructor .请注意,这会返回一个 ConstructorInfo 而不是 MethodInfo,但它们都派生自 MethodBase,因此具有大部分相同的成员。
关于c# - 如何使用反射获取构造函数作为 MethodInfo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1378020/