Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从可空类型获取反射中的PropertyType.Name_C#_Reflection_Nullable - Fatal编程技术网

C# 从可空类型获取反射中的PropertyType.Name

C# 从可空类型获取反射中的PropertyType.Name,c#,reflection,nullable,C#,Reflection,Nullable,我想使用反射来获取属性类型。 这是我的密码 var properties = type.GetProperties(); foreach (var propertyInfo in properties) { model.ModelProperties.Add( new KeyValuePair<Type, string>

我想使用反射来获取属性类型。 这是我的密码

var properties = type.GetProperties();
foreach (var propertyInfo in properties)
{
     model.ModelProperties.Add(
                               new KeyValuePair<Type, string>
                                               (propertyInfo.PropertyType.Name,
                                                propertyInfo.Name)
                              );
}
var properties=type.GetProperties();
foreach(属性中的var propertyInfo)
{
model.ModelProperties.Add(
新的KeyValuePair
(propertyInfo.PropertyType.Name,
propertyInfo.Name)
);
}

此代码
propertyInfo.PropertyType.Name
可以,但是如果我的属性类型是
Nullable
我会得到这个
Nullable'1
字符串,如果write
FullName
得到这个stirng
System.Nullable1[[System.DateTime,mscorlib,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b77a561934e089]]

更改代码以查找可为null的类型,在这种情况下,将PropertyType作为第一个泛型参数:

var propertyType = propertyInfo.PropertyType;

if (propertyType.IsGenericType &&
        propertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
    {
      propertyType = propertyType.GetGenericArguments()[0];
    }

model.ModelProperties.Add(new KeyValuePair<Type, string>
                        (propertyType.Name,propertyInfo.Name));
var-propertyType=propertyInfo.propertyType;
如果(propertyType.IsGenericType&&
propertyType.GetGenericTypeDefinition()==typeof(可为null))
{
propertyType=propertyType.GetGenericArguments()[0];
}
model.ModelProperties.Add(新的KeyValuePair
(propertyType.Name,propertyInfo.Name));

这是一个老问题,但我也遇到了这个问题。我喜欢@Igoy的答案,但如果类型是可空类型的数组,那么它就不起作用。这是我处理nullable/generic和array的任意组合的扩展方法。希望它对有同样问题的人有用

public static string GetDisplayName(this Type t)
{
    if(t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>))
        return string.Format("{0}?", GetDisplayName(t.GetGenericArguments()[0]));
    if(t.IsGenericType)
        return string.Format("{0}<{1}>",
                             t.Name.Remove(t.Name.IndexOf('`')), 
                             string.Join(",",t.GetGenericArguments().Select(at => at.GetDisplayName())));
    if(t.IsArray)
        return string.Format("{0}[{1}]", 
                             GetDisplayName(t.GetElementType()),
                             new string(',', t.GetArrayRank()-1));
    return t.Name;
}
公共静态字符串GetDisplayName(此类型为t)
{
if(t.IsGenericType&&t.GetGenericTypeDefinition()==typeof(可为空))
返回string.Format(“{0}?”,GetDisplayName(t.GetGenericArguments()[0]);
if(t.IsGenericType)
返回string.Format(“{0}”,
t、 Name.Remove(t.Name.IndexOf(“`”),
string.Join(“,”,t.GetGenericArguments().Select(at=>at.GetDisplayName());
如果(t.IsArray)
返回string.Format(“{0}[{1}]”,
GetDisplayName(t.GetElementType()),
新字符串(',',t.GetArrayRank()-1));
返回t.Name;
}
这将处理如此复杂的案件:

typeof(Dictionary<int[,,],bool?[][]>).GetDisplayName()
typeof(字典).GetDisplayName()
返回:

Dictionary<Int32[,,],Boolean?[][]>
字典

它是可为空的吗?您希望获取的字符串是哪一个?看起来您必须使用PropertyType上的属性/方法来访问该类型的泛型参数。请参阅。它为泛型和非泛型类型创建一个可读的名称(例如,
System.Nullable
)。