如何使用反射将构造函数作为MethodInfo获取 [英] How to get constructor as MethodInfo using Reflection

查看:41
本文介绍了如何使用反射将构造函数作为MethodInfo获取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

构造函数如下:

public NameAndValue(string name, string value)

我需要使用Reflection将其作为MethodInfo进行获取.它尝试了以下操作,但是没有找到构造函数( GetMethod 返回 null ).

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) });

我在做什么错了?

推荐答案

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

这篇关于如何使用反射将构造函数作为MethodInfo获取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆