内部返回类型的C#protected internal方法?

run*_*ack 0 c#

这是否意味着protected方法不能成为internal一种public类型?

internal class InternalReturnType
{
}

public class PublicTypeWithProtectedMethod
{
    //build succeeded when I remove `protected`
    internal protected InternalReturnType GetValue()
    {
        return new InternalReturnType();
    }
}

public sealed class PublicTypeWithPublicMethod : PublicTypeWithProtectedMethod
{
    public void Print()
    {
        var value = base.GetValue();
    }
}
Run Code Online (Sandbox Code Playgroud)

Tit*_*mir 5

internal protected表示可以从声明程序集或任何程序集中的任何派生类型访问该成员.由于InternalReturnType标记为internal只能从声明组件访问.这就是为什么编译器抱怨因为它不能遵守这两个限制,如果你可以GetValue从任何程序集中的任何派生类型访问,你应该能够InternalReturnType从任何程序集访问,但它被标记为internal不应该从任何集会.您可以protected从会员中删除或InternalReturnType公开.

注意根据您要执行的操作,您应该查看private protected(可在C#7.2上找到).这将允许仅在程序集内访问派生类型.