【问题标题】:Find type of nullable properties via reflection通过反射查找可空属性的类型
【发布时间】:2011-04-13 04:54:32
【问题描述】:

我通过反射检查对象的属性并继续处理每个属性的数据类型。这是我的(简化的)来源:

private void ExamineObject(object o)
{
  Type type = default(Type);
  Type propertyType = default(Type);
  PropertyInfo[] propertyInfo = null;

  type = o.GetType();

  propertyInfo = type.GetProperties(BindingFlags.GetProperty |
                                    BindingFlags.Public |
                                    BindingFlags.NonPublic |
                                    BindingFlags.Instance);
  // Loop over all properties
  for (int propertyInfoIndex = 0; propertyInfoIndex <= propertyInfo.Length - 1; propertyInfoIndex++)
  {
    propertyType = propertyInfo[propertyInfoIndex].PropertyType;
  }
}

我的问题是,我最近需要处理可空属性,但我不知道如何获取可空属性的类型。

【问题讨论】:

  • 我找到了很好的答案here值得一试!!

标签: c# .net reflection nullable


【解决方案1】:

可能的解决方案:

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

【讨论】:

  • MSDN 上也提到了对 Nullables 的正确检查:msdn.microsoft.com/en-us/library/ms366789.aspx。如果需要,您可以在那里找到有关该主题的更多资源。
  • 一行就能搞定!:propertyType = Nullable.GetUnderlyingType(propertyType) ?? propertyType
  • propertyType.IsGenericType 在propertyType.GetGenericTypeDefinition() 之前确实是必需的,否则会引发异常。 +1
【解决方案2】:

Nullable.GetUnderlyingType(fi.FieldType) 将为您完成工作检查下面的代码以做您想做的事情

System.Reflection.FieldInfo[] fieldsInfos = typeof(NullWeAre).GetFields();

        foreach (System.Reflection.FieldInfo fi in fieldsInfos)
        {
            if (fi.FieldType.IsGenericType
                && fi.FieldType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
            {
                // We are dealing with a generic type that is nullable
                Console.WriteLine("Name: {0}, Type: {1}", fi.Name, Nullable.GetUnderlyingType(fi.FieldType));
            }

    }

【讨论】:

  • 我喜欢Nullable.GetUnderlyingType(type) 解决方案,因为它比type.GetGenericArguments()[0] 更明确,至少在这种情况下是这样。
  • 您不需要检查 IsGenericType 和 GetGenericTypeDefinition,Nullable.GetUnderlyingType 已经在本机执行此操作。当类型不是 Nullable 时,GetUnderlyingType 返回 null(来源:msdn.microsoft.com/en-US/library/…)
【解决方案3】:
foreach (var info in typeof(T).GetProperties())
{
  var type = info.PropertyType;
  var underlyingType = Nullable.GetUnderlyingType(type);
  var returnType = underlyingType ?? type;
}

【讨论】:

    【解决方案4】:

    正如Yves M. 所指出的那样简单如下。

    var properties = typeof(T).GetProperties();
    
      foreach (var prop in properties)
      {
         var propType = Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType;
         var dataType = propType.Name;
      }
    

    【讨论】:

      【解决方案5】:

      我正在使用循环遍历所有类属性以获取属性类型。我使用以下代码:

      public Dictionary<string, string> GetClassFields(TEntity obj)
      {
          Dictionary<string, string> dctClassFields = new Dictionary<string, string>();
      
          foreach (PropertyInfo property in obj.GetType().GetProperties())
          {
              if (property.PropertyType.IsGenericType && property.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>) && property.PropertyType.GetGenericArguments().Length > 0)
                  dctClassFields.Add(property.Name, property.PropertyType.GetGenericArguments()[0].FullName);
              else
                  dctClassFields.Add(property.Name, property.PropertyType.FullName);
          }
      
          return dctClassFields;
      }
      

      【讨论】:

        【解决方案6】:

        此方法简单、快捷、安全

        public static class PropertyInfoExtension {
            public static bool IsNullableProperty(this PropertyInfo propertyInfo)
                => propertyInfo.PropertyType.Name.IndexOf("Nullable`", StringComparison.Ordinal) > -1;
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-01-24
          • 2011-09-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-10-26
          相关资源
          最近更新 更多