属性名称为变量

时间:2011-10-18 17:55:36

标签: c# reflection

我需要在数据库中找到每个字符串列的最大大小,作为设计另一个数据库的信息之一。我对源数据库的唯一访问是通过Web服务。我可以为很多列中的每一列做这个,以找到最大的尺寸,但我希望它是通用的,所以我可以在以后使用它。

我写了这个非常简化的版本,以使其易于理解。最后两行已经发明了语法,这是我需要帮助的地方。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    public class myClass
    {
        private string s;
        public string S
        {
            get { return s; }
            set { s = value; }
        }
        private int i;
        public int I
        {
            get { return i; }
            set { i = value; }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Type myClassType = typeof(myClass);
            System.Reflection.PropertyInfo[] propertyInfo = myClassType.GetProperties();
            Dictionary<string, int> property = new Dictionary<string, int>();

            foreach (System.Reflection.PropertyInfo info in propertyInfo)
               if (info.PropertyType == typeof(System.String))
                    property.Add(info.Name, -1);

            myClass[] myPa = new myClass[2];
            myPa[0] = new myClass();
            myPa[0].S = "1";
            myPa[0].I = 0;
            myPa[1] = new myClass();
            myPa[1].S = "12";
            myPa[1].I = 1;

这是我需要帮助的地方。我发明了c[pair.key]。如何引用我知道名称的属性?

            foreach (myClass c in myPa)
                foreach (KeyValuePair<string, int> pair in property)
                    if (c[pair.Key].Length > pair.Value)
                        property[pair.Key] = c[pair.Key].Length;

            foreach (KeyValuePair<string, int> pair in property)
                Console.WriteLine("Property: {0}, Biggest Size: {1}", pair.Key, pair.Value);
        }
    }
}

输出应该是:

Property: S Biggest Size: 2

2 个答案:

答案 0 :(得分:2)

以下内容应该足够了:

static void Main()
{
    // Demo data
    myClass[] myPa = new myClass[2];
    myPa[0] = new myClass();
    myPa[0].S = "1";
    myPa[0].I = 0;
    myPa[1] = new myClass();
    myPa[1].S = "12";
    myPa[1].I = 1;

    PrintMaxLengthsOfStringProperties(myPa);
}

public static void PrintMaxLengthsOfStringProperties<T>(IEnumerable<T> items)
{
    var t = typeof(T);
    t.GetProperties().Where(p => p.PropertyType == typeof(string)) // TODO: Add other filters
                        .SelectMany(p => items.Select(o => (string)p.GetValue(o, null)).Select(v => new { Property = p, Value = v }))
                        .GroupBy(u => u.Property)
                        .Select(gu => new { Property = gu.Key, MaxLength = gu.Max(u => u.Value != null ? u.Value.Length : 0) })
                        .ToList()
                        .ForEach(u2 => Console.WriteLine("Property: {0}, Biggest Size: {1}", u2.Property.Name, u2.MaxLength))
                        ;
}

虽然你可能会在“GetProperties”结果集上添加一些额外的过滤器(例如取出静态的,或索引属性等等。

它使用了几个Linq扩展函数,即“Where”,“GroupBy”,“SelectMany”,“Select”和“Max”以及匿名类型。

答案 1 :(得分:0)

不确定我是否理解正确,但是:

如果只在您的类中使用索引属性,这个[propertyname]里面将返回指定属性的值,如对象。

    public class myClass
    {
        ....
        ....
        public object this[string propertyname]
        {
            get { /*use reflection to return property value, so Object*/ }

        }
        ....
        ....
    }

这意味着,我担心,您不能只写c[pair.key].Length,因为Object没有Length属性。您需要将其强制转换为所需的类型(在您的情况下为string)并且仅在使用Length属性之后。

如果不是您的要求,请重新提问。