简体   繁体   English

如何在C#中将类与静态方法一起使用?

[英]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>因为因为IEnumerableIList更抽象,所以您可以保留扩展集合的扩展方法。

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.

相关问题 如何使用C#代码(而非python)将静态类(或静态方法)导入IronPython(或DLR)? - How to import static class (or static method) into IronPython (or DLR) using C# code(not python)? 如何使用 C# 反射调用静态泛型类的静态方法? - How to call static method of static generic class with C# Reflection? 在C#中使用表达式主体声明静态类或struct方法 - Declare static class or struct method using expression body in C# c#在单独的类中使用静态方法发送电子邮件 - c# emailing using static method in separate class C#:如何从派生类的静态方法调用基类的静态方法? - C#: How do I call a static method of a base class from a static method of a derived class? 如何在C#中的静态类中模拟普通方法? - How to mock a normal method inside a static class in C#? 如何使这个C#类和方法静态? - How can I make this C# class and method static? 如何在C#中使用静态工厂方法创建基类? - How to create a base class with static factory method in C#? 如何从C#中的静态方法引用当前类的类型 - How to refer to the type of the current class from a static method in C# 从 C# 中的静态方法实例化类 - Class instancing from a static method in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM