C# 如何获取可空类型的字符串类型

C# 如何获取可空类型的字符串类型,c#,reflection,C#,Reflection,我正在使用从dbml文件生成的数据类的属性生成T4模板。要获取我使用的类的属性类型item.PropertyType.Name,问题是对于可为null的类型,它返回nullable`1,是否有一种方法可以获取nullable,例如,或int?`int?=可为null。他们是一体的。如果您想知道可为null的类型,那么可以使用null.getUnderlineType(typeof(int?)方法获取类型(在本例中为int) 是您想要的方法 if (item.PropertyType.IsGene

我正在使用从dbml文件生成的数据类的属性生成T4模板。要获取我使用的类的属性类型
item.PropertyType.Name
,问题是对于可为null的类型,它返回
nullable
`1
,是否有一种方法可以获取
nullable
,例如,或
int?`

int?
=
可为null
。他们是一体的。如果您想知道可为null的类型,那么可以使用
null.getUnderlineType(typeof(int?)
方法获取类型(在本例中为
int

是您想要的方法

if (item.PropertyType.IsGenericType) {
    if (item.PropertyType.GetGenericType() == typeof(Nullable<>)) {
        var valueType = item.PropertyType.GetGenericArguments()[0];
    }
}
if(item.PropertyType.IsGenericType){
if(item.PropertyType.GetGenericType()==typeof(可为空)){
var valueType=item.PropertyType.GetGenericArguments()[0];
}
}
不过仔细想想,在这种情况下,Darren的答案要简单得多,因为当您传入一个不可为null的类型时,它将返回null。

请看一看。MarcGravell的答案将告诉您如何获得泛型参数的类型