代码之家  ›  专栏  ›  技术社区  ›  flesh

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

  •  13
  • flesh  · 技术社区  · 17 年前

    for (int i = 0; i < count; i++)
    {
        object obj = propertyInfo.GetValue(Tcurrent, new object[] { i });
    }
    

    .. 正在引发“TargetParameterCountException:参数计数不匹配”异常。

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

    谢谢你的建议。

    3 回复  |  直到 17 年前
        1
  •  19
  •   Lasse V. Karlsen    17 年前

    反射一次只能在一个级别上工作。

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

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

    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();
            }
        }
    }
    
        2
  •  2
  •   maxp    14 年前

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

    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;        
    }
    
        3
  •  0
  •   Adi Lester    13 年前
    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);
    }