如何在 C# 中使用带有静态方法的类
本文关键字:静态方法 | 更新日期: 2023-09-27 18:35:14
我想使用类ToDataTable()
将列表转换为数据表。问题是:类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);
以及将列表转换为数据表的类:
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.
.
我的代码是这样的:
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>
,因为您可以为更多集合保留扩展方法,因为IEnumerable
比IList
更抽象。
做public static class MainForm : System.Windows.Forms.Form
,因为你不能从另一个类派生静态类。
虽然你可以这样做:
using SomeNamespace
{
public static class FormExtension
{
public static DataTable ToDataTable<T>(this IList<T> data)
{
...
}
}
}
确保需要访问扩展的位置包括包含 Extension 方法的命名空间。 因此,任何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();