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

发布于 2024-08-14 13:53:54 字数 554 浏览 1 评论 0原文

我正在尝试将此类属性重构为接口。

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 生成导致不正确编译情况的代码的少数情况之一。)

是否有直接的解决方案来提取此错误将一个属性放入接口而不在类上进行不同的设置和获取成员/方法?

This class property is what I'm trying to refactor into an interface.

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

Visual Studio 2008 refactoring tools extract the following interface

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

The C# compiler complains with the error:

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

(This is one of the few circumstances I have run into where Visual Studio generates code that causes an improper compile situation.)

Is there a direct solution to extract this one property into an interface without making distinct set and get members/methods on the class?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绝對不後悔。 2024-08-21 13:53:54

我测试了以下内容:

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

interface Ibla {
    int Blargh { get; }
}

并且效果很好。你正确实现了接口吗?

I tested with the following:

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

interface Ibla {
    int Blargh { get; }
}

and it works just fine. Did you implement the interface correctly?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文