I want using class ToDataTable()
to convert List to DataTable.
Problem is: class ToDataTable()
using static
method and it make error.
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
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:
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.
.
My code like this:
public static class MainForm : System.Windows.Forms.Form
{
}