简体   繁体   English

无法使用VS2010中的重构为我的类创建接口

[英]Unable to create an interface for my class using refactor in VS2010

I have the following class and I am trying to create an interface for this. 我有以下类,我正在尝试为此创建一个接口。 However when I try refactor in VS2010. 但是当我在VS2010中尝试重构时。 I get a message that: Could not extract interface. 我收到一条消息:无法提取界面。 The type does not contain any members that could be extracted to an interface. 该类型不包含可以提取到接口的任何成员。

Is this related to my defining the class and/or method as static? 这与我将类和/或方法定义为静态有关吗? What I do need is to be able to get this data without having to create an instance so that's why I made it all static. 我需要的是能够在不必创建实例的情况下获取此数据,这就是为什么我将其全部静态化。

public static class DataSourceService
{

    public static IEnumerable<DataSource> GetDataSources()
    {
        return new[]
            {
                new DataSource { Value = "0001", Text = "Development"  },
                new DataSource { Value = "0002", Text = "Production" }
            };
    }

} }

You cannot have a static class with an interface, that's why the refactor tool cannot extract one. 你不能拥有一个带接口的静态类,这就是重构工具无法提取的一个原因。 You would need to turn it into an instance class with instance members in order to extract an interface. 您需要将其转换为具有实例成员的实例类,以便提取接口。

You don't have any methods that could be extracted. 您没有任何可以提取的方法。 static methods can't belong to an interface. static方法不能属于接口。

Google for static interface method and you'll get some interesting articles such as http://discuss.joelonsoftware.com/default.asp?dotnet.12.305680.12 谷歌的static interface method ,你会得到一些有趣的文章,如http://discuss.joelonsoftware.com/default.asp?dotnet.12.305680.12

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM