2011-10-18 146 views
0

我需要查找數據庫中每個字符串列的最大大小,作爲設計另一個數據庫的信息之一。我對源數據庫的唯一訪問權限是通過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 
+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」以及使用匿名類型。

+0

這個作品,我接受了。但我會堅持一個解決方案,而不會依賴Linq,儘管我懷疑沒有一個。 –

+0

你完全可以在沒有LINQ的情況下編寫它。它會花更長的時間... – Reddog

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屬性後。

如果這不是你要求的,請重新填寫你的問題。

+0

那個類是隻是一種簡化,我無法控制從哪個eac的真實課程由Web服務返回的h行是一個實例。 –