PropertyInfo.GetValue()-如何使用C#中的反射对泛型参数进行索引?

PropertyInfo.GetValue()-如何使用C#中的反射对泛型参数进行索引?,c#,generics,reflection,C#,Generics,Reflection,这是(缩短的)代码 for(int i=0;i

这是(缩短的)代码

for(int i=0;i
。。正在引发“TargetParameterCountException:参数计数不匹配”异常

“propertyInfo”的基础类型是一些T的集合。“count”是集合中的项数。我需要遍历集合并在obj上执行一个操作


感谢您的建议。

反思一次只能在一个层面上起作用

你试图索引到属性中,这是错误的

相反,读取属性的值和返回的对象,即需要索引到的对象

下面是一个例子:

using System;
using System.Collections.Generic;
using System.Reflection;

namespace DemoApp
{
    public class TestClass
    {
        public List<Int32> Values { get; private set; }

        public TestClass()
        {
            Values = new List<Int32>();
            Values.Add(10);
        }
    }

    class Program
    {
        static void Main()
        {
            TestClass tc = new TestClass();

            PropertyInfo pi1 = tc.GetType().GetProperty("Values");
            Object collection = pi1.GetValue(tc, null);

            // note that there's no checking here that the object really
            // is a collection and thus really has the attribute
            String indexerName = ((DefaultMemberAttribute)collection.GetType()
                .GetCustomAttributes(typeof(DefaultMemberAttribute),
                 true)[0]).MemberName;
            PropertyInfo pi2 = collection.GetType().GetProperty(indexerName);
            Object value = pi2.GetValue(collection, new Object[] { 0 });

            Console.Out.WriteLine("tc.Values[0]: " + value);
            Console.In.ReadLine();
        }
    }
}
使用系统;
使用System.Collections.Generic;
运用系统反思;
命名空间DemoApp
{
公共类TestClass
{
公共列表值{get;private set;}
公共测试类()
{
值=新列表();
增加(10);
}
}
班级计划
{
静态void Main()
{
TestClass tc=新的TestClass();
PropertyInfo pi1=tc.GetType().GetProperty(“值”);
对象集合=pi1.GetValue(tc,null);
//注意,这里没有检查对象是否真的
//是一个集合,因此真正具有该属性
字符串索引器名称=((DefaultMemberAttribute)集合。GetType()
.GetCustomAttributes(typeof(DefaultMemberAttribute),
true)[0])。成员名称;
PropertyInfo pi2=collection.GetType().GetProperty(indexerName);
对象值=pi2.GetValue(集合,新对象[]{0});
Console.Out.WriteLine(“tc.Values[0]:”+value);
Console.In.ReadLine();
}
}
}

在我看到这张照片之前,我一直在那里,我之所以发布这张照片,是因为我在其他任何地方都没有看到它;键使用的是GetValue(collection,newobject[]{i});在循环中,而不是尝试使用GetValue(集合,新对象[i]);在循环之外。 (在我的示例中,您可能可以忽略“输出”)

私有静态字符串递归(对象o)
{ 
字符串输出=”;
类型t=o.GetType();
如果(t.GetProperty(“项”)!=null)
{
System.Reflection.PropertyInfo p=t.GetProperty(“项目”);
整数计数=-1;
如果(t.GetProperty(“Count”)!=null&&
t、 GetProperty(“Count”).PropertyType==typeof(System.Int32))
{
count=(int)t.GetProperty(“count”).GetValue(o,null);
}
如果(计数>0)
{
对象[]索引=新对象[计数];
for(int i=0;i
Assembly zip\u Assembly=Assembly.LoadFrom(@“C:\Ionic.zip.Reduced.dll”);
类型ZipFileType=zip_assembly.GetType(“Ionic.zip.ZipFile”);
类型ZipEntryType=zip_assembly.GetType(“Ionic.zip.ZipEntry”);
字符串local_zip_file=@“C:\zipfile.zip”;
object zip_file=ZipFileType.GetMethod(“Read”,新类型[]{typeof(string)}).Invoke(null,新对象[]{local_zip_file});
//条目是ICollection
IEnumerable entries=(IEnumerable)ZipFileType.GetProperty(“entries”).GetValue(zip\u文件,null);
foreach(条目中的对象条目)
{
字符串文件_name=(字符串)ZipEntryType.GetProperty(“文件名”).GetValue(条目,null);
Console.WriteLine(文件名);
}

好的,我理解,谢谢您的回复。它现在正在工作,但如果有人知道的话,他会有兴趣知道“物品”属性……找到了它,改变了答案。属性位于类上,而不是属性上。注意,没有索引器的类也没有属性。注意-我在代码中进一步检查集合。感谢您的更新:)
using System;
using System.Collections.Generic;
using System.Reflection;

namespace DemoApp
{
    public class TestClass
    {
        public List<Int32> Values { get; private set; }

        public TestClass()
        {
            Values = new List<Int32>();
            Values.Add(10);
        }
    }

    class Program
    {
        static void Main()
        {
            TestClass tc = new TestClass();

            PropertyInfo pi1 = tc.GetType().GetProperty("Values");
            Object collection = pi1.GetValue(tc, null);

            // note that there's no checking here that the object really
            // is a collection and thus really has the attribute
            String indexerName = ((DefaultMemberAttribute)collection.GetType()
                .GetCustomAttributes(typeof(DefaultMemberAttribute),
                 true)[0]).MemberName;
            PropertyInfo pi2 = collection.GetType().GetProperty(indexerName);
            Object value = pi2.GetValue(collection, new Object[] { 0 });

            Console.Out.WriteLine("tc.Values[0]: " + value);
            Console.In.ReadLine();
        }
    }
}
private static string Recursive(object o)
{ 
        string output="";
        Type t = o.GetType();
        if (t.GetProperty("Item") != null)
        {
            System.Reflection.PropertyInfo p = t.GetProperty("Item");
            int count = -1;
            if (t.GetProperty("Count") != null && 
                t.GetProperty("Count").PropertyType == typeof(System.Int32))
            {
                count = (int)t.GetProperty("Count").GetValue(o, null);
            }
            if (count > 0)
            {
                object[] index = new object[count];
                for (int i = 0; i < count; i++)
                {
                    object val = p.GetValue(o, new object[] { i });
                    output += RecursiveWorker(val, p, t);
                }
            }
       }
       return output;        
}
Assembly zip_assembly = Assembly.LoadFrom(@"C:\Ionic.Zip.Reduced.dll");
Type ZipFileType = zip_assembly.GetType("Ionic.Zip.ZipFile");
Type ZipEntryType = zip_assembly.GetType("Ionic.Zip.ZipEntry");
string local_zip_file = @"C:\zipfile.zip";
object zip_file = ZipFileType.GetMethod("Read", new Type[] { typeof(string) }).Invoke(null, new object[] { local_zip_file });

// Entries is ICollection<ZipEntry>
IEnumerable entries = (IEnumerable)ZipFileType.GetProperty("Entries").GetValue(zip_file, null);
foreach (object entry in entries)
{
    string file_name = (string)ZipEntryType.GetProperty("FileName").GetValue(entry, null);
    Console.WriteLine(file_name);
}