简体   繁体   English

如何限制添加到派生类的项目类型?

[英]How do I restrict the type of items added to a derived class?

I have DerivedClass which inherits from BaseClass. 我有DerivedClass,它继承自BaseClass。 I want to take advantage of the BaseCollection class which overrides the InsertItem method so I defined DerivedCollection and inherited from BaseCollection. 我想利用覆盖InsertItem方法的BaseCollection类,因此我定义了DerivedCollection并继承自BaseCollection。 The problem is, DerivedCollection allows both DerivedClass and BaseClass types to be added. 问题是,DerivedCollection允许添加DerivedClass和BaseClass类型。 I only want to permit DerivedClass types to be added to DerivedCollection. 我只想允许DerivedClass类型添加到DerivedCollection。 What am I missing? 我错过了什么?

public class BaseClass
{
    public static string MyString;
}

public class BaseCollection<T> : Collection<BaseClass> where T : BaseClass
{
    protected override void InsertItem(int index, BaseClass item)
    {
        throw new UnauthorizedAccessException("I'm sorry Dave, I'm afraid I can't do that.");
    }
}

public class DerivedClass : BaseClass
{
    public static int MyInteger;
}

public class DerivedCollection : BaseCollection<DerivedClass>
{
}
  1. Change BaseCollection<T> to inherit from Collection<T> , not from Collection<BaseClass> . BaseCollection<T>更改为从Collection<T>继承,而不是从Collection<BaseClass>继承。
  2. Change arguments of InsertItem appropriately. 适当地更改InsertItem参数。
public class BaseCollection<T> : Collection<T> where T : BaseClass
{
    protected override void InsertItem (int index, T item)
    {
        throw new UnauthorizedAccessException("I'm sorry Dave, I'm afraid I can't do that.");
    }
}

You only want to allow T: 你只想允许T:

protected override void InsertItem(int index, T item)
{
    throw new UnauthorizedAccessException("I'm sorry Dave, I'm afraid I can't do that.");
}

You also have to inherit from Collection<T> instead of Collection<BaseClass> 您还必须继承Collection<T>而不是Collection<BaseClass>

You need to override InsertItem like so: 您需要像这样重写InsertItem:

public class DerivedCollection : BaseCollection<DerivedClass>
{
protected override void InsertItem(int index, DerivedClass item)
    {
        base.InsertItem(index, item);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何引用来自基础 class 的派生类型? - how do i refer to the derived type from the base class? 如何在抽象类中声明任何类型的数组(从给定类型派生)? - How do I declare an array of any type (derived from a given type) in an abstract class? C# 如何从基类中的派生类获取特定类型的所有字段? - C# How do I get all the fields of a specific type from a derived class within the base class? 如何在不事先知道类型的情况下使用XmlSerializer反序列化可能是基类或派生类的对象? - How do I use an XmlSerializer to deserialize an object that might be of a base or derived class without knowing the type beforehand? 是否可以将传递给.NET方法的变量类型限制为不是派生类? - Is it possible to restrict the type of variable passed to a .NET method to not be a derived class? 如何从基类调用派生类方法? - How do I call a derived class method from the base class? 在C#中,如何从派生类构造函数访问派生类的属性? - In C# how do I access the properties of a derived class from the derived class constructor? 限制派生类中的访问方法 - Restrict to access method in derived class 实体框架的MVC继承:如何构建派生类型? - MVC Inheritance with Entity Framework: How do I scaffold a derived type? 如何将派生类限制为仅具有某些类型的成员 - How to restrict derived class to only have members of certain types
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM