[英]How to using class with static method in C#?
I want using class ToDataTable()
to convert List to DataTable. 我想使用类ToDataTable()
将List转换为DataTable。 Problem is: class ToDataTable()
using static
method and it make error. 问题是:类ToDataTable()
使用static
方法,它使错误。 I know this error but I don't know how to resolve it. 我知道此错误,但我不知道如何解决。
The error code is: Extension method must be defined in a non-generic static class
错误代码为: Extension method must be defined in a non-generic static class
My code using is: 我使用的代码是:
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);
And class to convert List to DataTable: 并将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;
}
I was research on the internet. 我当时正在互联网上进行研究。 Like, change my class to static. 像,将我的班级改为静态。 But I change to static and error continues to appear: 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.
Static class 'MyClass.MainForm' cannot derive from type 'System.Windows.Forms.Form'. Static classes must derive from object.
. 。
My code like this: 我的代码是这样的:
public static class MainForm : System.Windows.Forms.Form
{
}
Just remove this
只要删除this
public static DataTable ToDataTable<T>(IList<T> data)
and use as simple static method 并用作简单的静态方法
Or move this function to separated static class 或将此函数移到单独的静态类
like 喜欢
public static class UnilityExtensions
{
public static DataTable ToDataTable<T>(this IList<T> data)
{ ... }
}
No, you cannot define a form as a static
class because it have to be instanced. 不,您不能将表单定义为static
类,因为必须对其进行实例化。 TO have an extension method you need a static class, but not necessarily you need to chnage your form class. 要拥有扩展方法,您需要一个静态类,但不一定需要更改表单类。 Create a new static class and put your extensions methods there. 创建一个新的静态类,并将扩展方法放在此处。 See the MSDN docs here . 请在此处查看MSDN文档 。
Extension methods are defined as static methods but are called by using instance method syntax. 扩展方法定义为静态方法,但使用实例方法语法调用。
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;
}
}
Another suggestion, change you IList<T>
to IEnumerable<T>
because you can keep an extension method for more collections because the IEnumerable
is more abstract than IList
. 另一个建议是,将IList<T>
更改为IEnumerable<T>
因为因为IEnumerable
比IList
更抽象,所以您可以保留扩展集合的扩展方法。
You cant do public static class MainForm : System.Windows.Forms.Form
because you cant derive static class from another class. 您不能执行public static class MainForm : System.Windows.Forms.Form
因为您不能从其他类派生静态类。
Although you can do this: 尽管可以这样做:
using SomeNamespace
{
public static class FormExtension
{
public static DataTable ToDataTable<T>(this IList<T> data)
{
...
}
}
}
Make sure the place where you need to access the extension includes the namespace containing the Extension method. 确保需要访问扩展的位置包括包含扩展方法的名称空间。 So any IList<T>
will have access to this ToDataTable<T>
method. 因此,任何IList<T>
都可以访问此ToDataTable<T>
方法。
Move your method into static class, and make it extension method as per below given, 将您的方法移到静态类中,并按照以下给出的方法将其扩展,
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;
}
}
and replace your line, 并更换你的线,
dtProxy = ToDataTable(proxyInfos);
with, 与,
dtProxy = proxyInfos.ToDataTable();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.