2016-03-21 32 views
0

我目前一直在學習和研究Generic的C#內部,但我一直在努力實際使用一旦創建的方法。通用方法無法訪問

我曾嘗試:

public class myTestClass 
{ 
    class example 
    { 
     public static DataTable LINQtoDataTable<T>(IEnumerable<T> data) 
     { 
      DataTable dt = new DataTable(); 
      PropertyInfo[] objectProps = null; // Reflection 

      if (data == null) return null; 
      foreach (T record in data) 
      { 
       if (objectProps == null) objectProps = ((Type)data.GetType()).GetProperties(); 
       foreach (PropertyInfo pi in objectProps) 
       { 
        Type collumnType = pi.PropertyType; 
        if ((collumnType.IsGenericType) && (collumnType.GetGenericTypeDefinition() == typeof(Nullable<>))) collumnType = collumnType.GetGenericArguments()[0]; 
        dt.Columns.Add(new DataColumn(pi.Name, collumnType)); 
       } 
      } 
      return dt; 
     } 

    } 

    example ex { get; set; } 

    public myTestClass() 
    { 
     this.ex = new example(); 
    } 
} 

但是,當我這樣做(在C#形式):

// Namespace area 
myTestClass test; 
// Main Method 
test = new myTestClass(); 

test.LINQtoDataTable()不上來或存在。任何人都可以請幫我嗎?我很困惑,爲什麼,因爲我public'd方法和實例化類是內部:(

大大提前瞭解&感謝,這將不會出現。

+2

'LINQtoDataTable'是一個私有嵌套類中的靜態方法,所以'test.LINQtoDataTable'將不起作用。 – rbm

+2

你也需要它作爲一個靜態類(其中數據參數需要設置爲此),你可以研究擴展方法:) – Icepickle

回答

3

extension-method存在於靜態公共類中,而您的當前代碼在私有類中只有靜態方法。因此,你需要這樣的:

public static class MyTestClass { 
    public static DataTable LINQtoDataTable<T>(this IEnumerable<T> data) { ... } 
} 

而且你需要在你想成爲該擴展法被綁定到帕拉姆的this -keyword。

最後一個擴展方法不能停留在一個嵌套類中,而這顯然根本不需要。刪除嵌套類並使MyTestClass(也考慮naming-conventions for classes)publc和static並將該方法放在那裏。因此你不需要這個類的任何實例。 Simpy致電myEnumerable.LINQtoDataTable()

+0

更改我似乎無法現在創建一個靜態類的實例:/ – KDOT

+1

@ KyleE4K不,你應該把它提取到一個「擴展」類,它不需要包含在你的示例類中,如果這是你想用來啓動對象的類。重要的部分是,擴展方法定義的類是靜態的,並且你的方法的第一個參數包含this關鍵字(如果你想要擴展方法) – Icepickle

+0

好點@HimBromBeere,讚賞 –

2

becuse的LINQtoDataTable metode靜態,不需要一個example實例。這LOCAT在ex屬性格式,並通過它的名稱訪問。

myTestClass.example.LINQtoDataTable(...) 
3

您要創建擴展方法,併爲擴展方法有一些前提條件的方法有,它應該是靜態的,在靜態類中,你是一個靜態類再缺少的是this關鍵字,您的類不是在它開始靜態

public static DataTable LINQtoDataTable<T>(this IEnumerable<T> data) 
{ 
    DataTable dt = new DataTable(); 
    PropertyInfo[] objectProps = null; // Reflection 

    if (data == null) return null; 
    foreach (T record in data) 
    { 
     if (objectProps == null) objectProps = ((Type)data.GetType()).GetProperties(); 
     foreach (PropertyInfo pi in objectProps) 
     { 
      Type collumnType = pi.PropertyType; 
      if ((collumnType.IsGenericType) && (collumnType.GetGenericTypeDefinition() == typeof(Nullable<>))) collumnType = collumnType.GetGenericArguments()[0]; 
       dt.Columns.Add(new DataColumn(pi.Name, collumnType)); 
     } 
    } 
    return dt; 
} 

,你仍然會看到現在它在智能感知,因爲你正在創建IEnumerable<T>擴展方法,而你試圖把它僅在T

對於能夠調用它,你必須創建一個List<T>

List<myTestClass> listTestClass = new List<myTestClass>(); 
listTestClass.Add(new myTestClass()); 
listTestClass.LINQtoDataTable(); 

我曾經寫過的擴展方法專題的博客文章,你可能想用一個簡單的例子來了解擴展方法here

+0

讚賞,我做了這些方法 – KDOT