簡體   English   中英

如何在C#中將類與靜態方法一起使用?

[英]How to using class with static method in C#?

我想使用類ToDataTable()將List轉換為DataTable。 問題是:類ToDataTable()使用static方法,它使錯誤。 我知道此錯誤,但我不知道如何解決。

錯誤代碼為: Extension method must be defined in a non-generic static class

我使用的代碼是:

var proxyInfos = proxyL
            .Where(l => l.Contains(" US "))
            .Select(l => l.Split(' '))
            .Select(tokens => new
            {
                IP = tokens[0],
                Port = tokens[1]
            })
            .ToList();
        dtProxy = ToDataTable(proxyInfos);

並將List轉換為DataTable的類:

public static DataTable ToDataTable<T>(this IList<T> data)
{
    PropertyDescriptorCollection properties =
        TypeDescriptor.GetProperties(typeof(T));
    DataTable table = new DataTable();
    foreach (PropertyDescriptor prop in properties)
        table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
    foreach (T item in data)
    {
        DataRow row = table.NewRow();
        foreach (PropertyDescriptor prop in properties)
            row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
        table.Rows.Add(row);
    }
    return table;
}

我當時正在互聯網上進行研究。 像,將我的班級改為靜態。 但我更改為靜態,錯誤繼續出現: Static class 'MyClass.MainForm' cannot derive from type 'System.Windows.Forms.Form'. Static classes must derive from object. Static class 'MyClass.MainForm' cannot derive from type 'System.Windows.Forms.Form'. Static classes must derive from object.

我的代碼是這樣的:

public static class MainForm : System.Windows.Forms.Form
{
}

只要刪除this

public static DataTable ToDataTable<T>(IList<T> data)

並用作簡單的靜態方法

或將此函數移到單獨的靜態

喜歡

public static class UnilityExtensions
{
    public static DataTable ToDataTable<T>(this IList<T> data)
    { ... }
}

不,您不能將表單定義為static類,因為必須對其進行實例化。 要擁有擴展方法,您需要一個靜態類,但不一定需要更改表單類。 創建一個新的靜態類,並將擴展方法放在此處。 請在此處查看MSDN文檔

擴展方法定義為靜態方法,但使用實例方法語法調用。

public static class MyExtensions
{
    public static DataTable ToDataTable<T>(this IEnumerable<T> str)
    {
        PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T));
        DataTable table = new DataTable();

        foreach (PropertyDescriptor prop in properties)
            table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);

        foreach (T item in data)
        {
            DataRow row = table.NewRow();
            foreach (PropertyDescriptor prop in properties)
                row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
            table.Rows.Add(row);
        }

        return table;
    }
}   

另一個建議是,將IList<T>更改為IEnumerable<T>因為因為IEnumerableIList更抽象,所以您可以保留擴展集合的擴展方法。

您不能執行public static class MainForm : System.Windows.Forms.Form因為您不能從其他類派生靜態類。

盡管可以這樣做:

using SomeNamespace
{
    public static class FormExtension
    {
        public static DataTable ToDataTable<T>(this IList<T> data)
        {
           ...
        }
    }
}

確保需要訪問擴展的位置包括包含擴展方法的名稱空間。 因此,任何IList<T>都可以訪問此ToDataTable<T>方法。

將您的方法移到靜態類中,並按照以下給出的方法將其擴展,

 public static class Extension
{
    public static DataTable ToDataTable<T>(this IList<T> data)
    {
        PropertyDescriptorCollection properties =
            TypeDescriptor.GetProperties(typeof(T));
        DataTable table = new DataTable();
        foreach (PropertyDescriptor prop in properties)
            table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
        foreach (T item in data)
        {
            DataRow row = table.NewRow();
            foreach (PropertyDescriptor prop in properties)
                row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
            table.Rows.Add(row);
        }
        return table;
    }
}

並更換你的線,

dtProxy = ToDataTable(proxyInfos);

與,

dtProxy = proxyInfos.ToDataTable();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM