C# 属性名作为变量

C# 属性名作为变量,c#,reflection,C#,Reflection,我需要找到数据库中每个字符串列的最大大小,作为设计另一个数据库的信息之一。我只能通过web服务访问源数据库。我可以对许多列中的每一列进行搜索,以找到最大的大小,但我希望它是通用的,以便以后使用 我写这个非常简单的版本是为了让它简单易懂。最后两行已经发明了语法,这就是我需要帮助的地方 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleAppl

我需要找到数据库中每个字符串列的最大大小,作为设计另一个数据库的信息之一。我只能通过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;
输出应为:

Property: S Biggest Size: 2

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

如果只在类中使用索引属性[propertyname],它将返回指定属性的值,如对象

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

        }
        ....
        ....
    }
这意味着,恐怕不能只编写
c[pair.key].Length
,因为
Object
没有
Length
属性。只有在使用
Length
属性之后,才需要将其强制转换为所需的类型(
string


如果不是你想要的,请重新提问。

以下内容就足够了:

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))
                        ;
}
static void Main()
{
//演示数据
myClass[]myPa=新的myClass[2];
myPa[0]=新的myClass();
myPa[0].S=“1”;
myPa[0].I=0;
myPa[1]=新的myClass();
myPa[1].S=“12”;
myPa[1].I=1;
PrintMaxLengthsOfStringProperties(myPa);
}
公共静态void PrintMaxLengthsOfStringProperties(IEnumerable项)
{
var t=类型(t);
t、 GetProperties().Where(p=>p.PropertyType==typeof(string))//TODO:添加其他筛选器
.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)})
托利斯先生()
.ForEach(u2=>Console.WriteLine(“属性:{0},最大大小:{1}”,u2.Property.Name,u2.MaxLength))
;
}
尽管您可能会在“GetProperties”结果集上添加一些额外的过滤器(例如删除静态过滤器,或索引属性等)


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

这很有效,我接受了它。但在另一个问题中,我将坚持使用不依赖Linq的解决方案,尽管我怀疑没有Linq。该类只是一种简化。我无法控制web服务返回的每一行都是实例的真实类。
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))
                        ;
}