我需要查找数据库中每个字符串列的最大大小,作为设计另一个数据库的信息之一。我对源数据库的唯一访问权限是通过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);
}
}
}
输出768,16是:
Property: S Biggest Size: 2
我不明白。 –