繁体   English   中英

反射GetProperty应该排除某些数据类型上不需要的子级属性

[英]Reflection GetProperties should exclude unwanted children properties on certain datatype

我肯定会遗漏明显的东西。我正在构建一个内部应用程序,并正在反思我们的一些内部dll并以树状视图显示它们

Treeview是按需加载的,对于展开后的每个属性,我都会得到子级(如果有)。 当孩子是datetime,string,decimal等时。然后再次进行扩展,我不应该获取string或datetime的所有内部属性,等等。 它不应该返回任何东西。我尝试了很少的bindingFlags,但是我们没有成功。

我正在使用以下方法,但还不够好。

  public static PropertyInfo[] GetPropertiesByType(this Type t)
    {
        if (!t.IsPrimitive
            || t != typeof (System.Decimal)
            || t != typeof (System.String)
              || t != typeof(System.DateTime)
            || t != typeof (System.DateTime?))
        {

            return t.GetProperties(BindingFlags.Public | BindingFlags.Instance)
                .OrderBy(p => p.Name).ToArray();
        }
        return new PropertyInfo[0];
    }

我想要的是,在获取属性时,应排除所有不相关的内部属性。

EG客户有订单,订单有OrderedDate。使用树状视图时,单击客户,得到订单,单击订单,单击orderdate时得到OrderDate,我应该没有属性。我得到“ HasValue和value”并扩展值并得到所有日期时间的东西。

当属性是字符串时也是如此,我不应该看到字符和长度。

有什么建议么

您应该手动检查基本类型并返回一个空数组。 您可以在这里找到wuestion的答案, 以检查类型是否为原始类型

if (t.IsPrimitive || t == typeof(Decimal) || t == typeof(String) || ... )
{
    // Is Primitive, or Decimal, or String
}

所以基本上你可以做

public static PropertyInfo[] GetPropertiesByType(this Type t)
    {
        if (t.IsPrimitive || t == typeof(Decimal) || t == typeof(String) || ... )
    {
        return //empty array
    }
        return t.GetProperties(BindingFlags.Public | BindingFlags.Instance)
                .OrderBy(p => p.Name).ToArray();
    }

-Update

public static PropertyInfo[] GetPropertiesByType(this Type t)
    {
        if (!t.IsPrimitive
            || t != typeof (System.Decimal)
            || t != typeof (System.String)
            || t != typeof(System.DateTime)
            || t != typeof (System.DateTime?))
        {

            return t.GetProperties(BindingFlags.Public | BindingFlags.Instance)
                .OrderBy(p => p.Name).ToArray();
        }
        else
        return new PropertyInfo[0];
    }

扩展之前,应检查Type.FullName属性,如果类型不在您的命名空间中,则不要扩展。

为了详细说明,MSDN的以下代码说明了FullName的作用:

Type t = typeof(Array);
Console.WriteLine("The full name of the Array type is {0}.", t.FullName);

/* This example produces the following output:

The full name of the Array type is System.Array.
*/

例如,您可以测试FullName是否以“ System”开头。 或“微软”。 并且不要扩展这些属性。

也许:

    public static PropertyInfo[] GetPropertiesByType(this Type t)
    {
        if (!typeof (MyBasePropertyClass).IsAssignableFrom(t))
            return new PropertyInfo[0];

        return t.GetProperties(BindingFlags.Public | BindingFlags.Instance)
                .OrderBy(p => p.Name)
                .ToArray();
    }

其中MyBasePropertyClass是您的实体的基类。

if (!t.IsPrimitive
    || t != typeof (System.Decimal)
    || t != typeof (System.String)
    || t != typeof(System.DateTime)
    || t != typeof (System.DateTime?))

应该

if (!t.IsPrimitive
    && t != typeof (System.Decimal)
    && t != typeof (System.String)
    && t != typeof(System.DateTime)
    && t != typeof (System.DateTime?))

System.String由于所有类型不是System.StringSystem.Decimal并且您将它们与OR运算符组合在一起,因此您的条件将始终为true。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM