[英]How to get Constructor Generic parameter type using reflection c#
Hi All Thanks in advance. 大家好,谢谢。 I have a class and a parameterize constructor and i want to read Constructor parameters type. 我有一个类和一个参数化构造函数,我想读取构造函数参数类型。 Let say 说吧
Class A
{
public A(Helper help)
{
// code
}
}
In the above code we can read constructor using below code Code: 在上面的代码中,我们可以使用以下代码读取构造函数:
Type ClassType = typeof(A);
ConstructorInfo[] Constructors = ClassType.GetConstructors();
foreach (ConstructorInfo constInfo in Constructors)
{
if (constInfo.GetParameters().Count() > 0)
{
RefNames = new List<string>();
foreach (ParameterInfo parameters in constInfo.GetParameters())
{
Type newType = parameters.ParameterType;
}
}
}
So in newType i will get Type of "Helper" class but i want type from generic interface. 因此,在newType中,我将获得“ Helper”类的Type,但我希望从通用接口输入Type。 eg 例如
Class A
{
public A(IHelper<Helper> help)
{
// code
}
}
// where Helper class inheriting from IHelper interface Now i want type of IHerper interface ie Type of Helper How can i get it. //凡是从IHelper介面继承的Helper类别现在,我想输入IHerper介面的类型,例如Helper的类型。
Thanks in advance. 提前致谢。
不问你为什么这样做,但是可以通过访问ParameterInfo
GenericTypeArguments
属性来获取使用的通用类型:
Type newType = parameters.ParameterType.GenericTypeArguments.First();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.