开发者

How can I disable external inheritance (without classes) in C#?

开发者 https://www.devze.com 2023-01-13 20:23 出处:网络
How can I disable external inheritance without using classes? I know it can be done with classes like this:

How can I disable external inheritance without using classes? I know it can be done with classes like this:

public abstract class NoInherit
{
    internal NoInherit() { }
}

public sealed class MyType : NoInherit { /* ... */ }

But not with interfaces. I don't want to use classes because I want to inherit MyType from another class and C# doesn't support multiple inheritance.

Perhaps I should explain better what I'm开发者_开发技巧 trying to do. I'm attempting to create an object-like class but that only accepts some types.

List<NoInherit> x = new List<NoInherit>();
x.Add(new MyType()); // OK
x.Add(new MyType2()); // OK
x.Add(123); // ERROR
0

精彩评论

暂无评论...
验证码 换一张
取 消