【问题标题】:How this where generic type constraint work? [duplicate]泛型类型约束如何工作? [复制]
【发布时间】:2019-07-14 21:51:53
【问题描述】:

我正在研究通用 where 约束,但我遇到了这个并卡住了。我尝试通过我自己的类复制代码,但我得到“不一致的不可访问性”。

//this is the reference study class
public class MainView<T> : System.Windows.Window where T : INotifyPropertyChanged, new(){}

//my own
interface ITest{}

class B : ITest
{
    public B()
    {

    }
}

public class MyClass<T> : B where T : ITest, new()
{

}

我想不通。参考学习班有什么?如何使用自己定义的类和接口进行相同的签名?

【问题讨论】:

  • 感谢您指出这一点。我不知道我的问题与访问修饰符有关。

标签: c# generics types


【解决方案1】:

注意ITest 接口和B 类没有显式访问修饰符;如果未提供默认值是internal(对于顶级类型),并且您在publicMyClass 类中实现它们(比internal 更易于访问,因此,编译器错误)。解决方案是在这种情况下将您的类型的访问修饰符更改为 public,或者将您的 MyClass 类设为 internal 而不是 public

 //my own
public interface ITest{}

public class B : ITest
{
    public B()
    {

    }
}

public class MyClass<T> : B where T : ITest, new()
{

}

更多关于访问修饰符的信息可以在这里找到: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/accessibility-levels

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-24
    • 1970-01-01
    • 1970-01-01
    • 2017-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多