如何通过接口描述具有不同访问级别的自动实现的属性?

时间:2009-12-09 19:01:11

标签: c# interface automatic-properties

这个类属性正是我试图重构为接口的。

public class Stuff : IStuff {
    public int Number {
        get;
        protected internal set;
    }
}

Visual Studio 2008重构工具提取以下界面

// Visual Studio 2008's attempt is:
public interface IStuff {
    int Number { get; }
}

C#编译器抱怨错误:

'Stuff.Number.set' adds an accessor not found in interface member 'IStuff.DataOperations'

(这是我遇到的少数情况之一,Visual Studio生成的代码导致编译情况不正确。)

是否有直接的解决方案将这一个属性提取到接口中而不在类上创建不同的set和get成员/方法?

1 个答案:

答案 0 :(得分:2)

我测试了以下内容:

class bla : Ibla {
    public int Blargh { get; protected internal set; }
}

interface Ibla {
    int Blargh { get; }
}

它工作得很好。您是否正确实现了界面?

相关问题