C# - 使用泛型进行反射:IList 嵌套集合的问题

发布于 2024-07-18 03:35:56 字数 431 浏览 5 评论 0原文

我希望能够打印对象属性,但当我遇到 iList 的嵌套集合时遇到了障碍。

foreach (PropertyInformation p in properties)
            {
                //Ensure IList type, then perform recursive call
                if (p.PropertyType.IsGenericType)
                {
                         //  recursive call to  PrintListProperties<p.type?>((IList)p,"       ");
                }

有人可以提供一些帮助吗?

干杯

KA

I would like to be able to print object properties, and I've hit a snag when I hit nested collections of the iLists.

foreach (PropertyInformation p in properties)
            {
                //Ensure IList type, then perform recursive call
                if (p.PropertyType.IsGenericType)
                {
                         //  recursive call to  PrintListProperties<p.type?>((IList)p,"       ");
                }

Can anyone please offer some help?

Cheers

KA

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

秉烛思 2024-07-25 03:35:57
foreach (PropertyInfo p in props)
    {
        // We need to distinguish between indexed properties and parameterless properties
        if (p.GetIndexParameters().Length == 0)
        {
            // This is the value of the property
            Object v = p.GetValue(t, null);
            // If it implements IList<> ...
            if (v != null && v.GetType().GetInterface("IList`1") != null)
            {
                // ... then make the recursive call:
                typeof(YourDeclaringClassHere).GetMethod("PrintListProperties").MakeGenericMethod(v.GetType().GetInterface("IList`1").GetGenericArguments()).Invoke(null, new object[] { v, indent + "  " });
            }
            Console.WriteLine(indent + "  {0} = {1}", p.Name, v);
        }
        else
        {
            Console.WriteLine(indent + "  {0} = <indexed property>", p.Name);
        }
    }    
foreach (PropertyInfo p in props)
    {
        // We need to distinguish between indexed properties and parameterless properties
        if (p.GetIndexParameters().Length == 0)
        {
            // This is the value of the property
            Object v = p.GetValue(t, null);
            // If it implements IList<> ...
            if (v != null && v.GetType().GetInterface("IList`1") != null)
            {
                // ... then make the recursive call:
                typeof(YourDeclaringClassHere).GetMethod("PrintListProperties").MakeGenericMethod(v.GetType().GetInterface("IList`1").GetGenericArguments()).Invoke(null, new object[] { v, indent + "  " });
            }
            Console.WriteLine(indent + "  {0} = {1}", p.Name, v);
        }
        else
        {
            Console.WriteLine(indent + "  {0} = <indexed property>", p.Name);
        }
    }    
失眠症患者 2024-07-25 03:35:57
p.PropertyType.GetGenericArguments()

会给你一个类型参数的数组。 (在这种情况下,只有一个元素,即 IList 中的 T)

p.PropertyType.GetGenericArguments()

will give you an array of the type arguments. (in ths case, with just one element, the T in IList<T>)

握住我的手 2024-07-25 03:35:57
var dataType = myInstance.GetType();
var allProperties = dataType.GetProperties(BindingFlags.Public | BindingFlags.Instance);
var listProperties =
  allProperties.
    Where(prop => prop.PropertyType.GetInterfaces().
      Any(i => i == typeof(IList)));
var dataType = myInstance.GetType();
var allProperties = dataType.GetProperties(BindingFlags.Public | BindingFlags.Instance);
var listProperties =
  allProperties.
    Where(prop => prop.PropertyType.GetInterfaces().
      Any(i => i == typeof(IList)));
瞳孔里扚悲伤 2024-07-25 03:35:56

我只是在这里大声思考。 也许您可以有一个非通用的 PrintListProperties 方法,看起来像这样:

private void PrintListProperties(IList list, Type type)
{
   //reflect over type and use that when enumerating the list
}

然后,当您遇到嵌套列表时,执行如下操作:

if (p.PropertyType.IsGenericType)
{
   PringListProperties((Ilist)p,p.PropertyType.GetGenericArguments()[0]);
}

再说一次,还没有测试过这个,但试一试......

I'm just thinking aloud here. Maybe you can have a non generic PrintListProperties method that looks something like this:

private void PrintListProperties(IList list, Type type)
{
   //reflect over type and use that when enumerating the list
}

Then, when you come across a nested list, do something like this:

if (p.PropertyType.IsGenericType)
{
   PringListProperties((Ilist)p,p.PropertyType.GetGenericArguments()[0]);
}

Again, haven't tested this, but give it a whirl...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文