你会如何处理这种设计?

时间:2022-12-05 12:55:52

I have ControlA which accepts an IInterfaceB which has a property of type List<unknownType>

我有ControlA,它接受一个I​​InterfaceB,它具有List 类型的属性

In an event of ControlA i need to add a new instance of unknownType to the List in IInterfaceB...

在ControlA的情况下,我需要向IInterfaceB中的List添加一个未知类型的新实例...

unknownType needs specific properties so i immediately thought it could be an interface, but quickly realised interfaces cannot be instantiated...

unknownType需要特定的属性,所以我立即认为它可能是一个接口,但很快就实现了接口无法实例化...

How would you design this system?

你会如何设计这个系统?

EDIT the current inheritance chain looks like this:

编辑当前的继承链如下所示:

topLevelClass -> baseObject -> IBaseObject (which is implemented in topLevelClass)

so if i added a new class to the chain it would need to do the inheriting and implementing which would be impossible (afaik)

所以,如果我在链中添加一个新类,则需要进行继承和实现,这是不可能的(afaik)

4 个解决方案

#1


If I'm interpreting this correctly, you could add a constraint on unknownType to be of some interface that contains the properties you need:

如果我正确地解释了这一点,你可以在unknownType上添加一个约束,使其成为包含你需要的属性的某个接口:

class ControlA
{
    void Frob<T>(IInterfaceB<T> something) where T : IHasSomeProperties, new()
    {
        something.ListOfT.Add(new T() { SomeProperty = 5 });
        something.ListOfT.Add(new T() { SomeProperty = 14 });
    }
}

#2


Would a restriction on the type work?

对类型的限制是否有效?

List<T> where T : IInterface

#3


Your specification isn't specific enough to answer well, but try putting constraints on T.

您的规范不够具体,无法很好地回答,但尝试对T进行约束。

public interface IInterfaceB<T>
    where T : new()
{
    List<T> Whatever { get; }
}

This allows you to do this:

这允许您这样做:

T iDoNotCare = new T();

#4


Why does List have to be an unknowntype? Why can't it be a real class, the has the real properties you need with a constructor for you to instantiate? If you need to extend that class, you can then inherit from it. Based on the information you're providing, if all you need is a few properties in the class that goes into this List, then a good old class should do the trick.

为什么List必须是unknowntype?为什么它不能成为一个真正的类,具有构造函数所需的真实属性供您实例化?如果需要扩展该类,则可以继承该类。根据您提供的信息,如果您需要的是进入此List的类中的一些属性,那么一个好的旧类应该可以做到。

#1


If I'm interpreting this correctly, you could add a constraint on unknownType to be of some interface that contains the properties you need:

如果我正确地解释了这一点,你可以在unknownType上添加一个约束,使其成为包含你需要的属性的某个接口:

class ControlA
{
    void Frob<T>(IInterfaceB<T> something) where T : IHasSomeProperties, new()
    {
        something.ListOfT.Add(new T() { SomeProperty = 5 });
        something.ListOfT.Add(new T() { SomeProperty = 14 });
    }
}

#2


Would a restriction on the type work?

对类型的限制是否有效?

List<T> where T : IInterface

#3


Your specification isn't specific enough to answer well, but try putting constraints on T.

您的规范不够具体,无法很好地回答,但尝试对T进行约束。

public interface IInterfaceB<T>
    where T : new()
{
    List<T> Whatever { get; }
}

This allows you to do this:

这允许您这样做:

T iDoNotCare = new T();

#4


Why does List have to be an unknowntype? Why can't it be a real class, the has the real properties you need with a constructor for you to instantiate? If you need to extend that class, you can then inherit from it. Based on the information you're providing, if all you need is a few properties in the class that goes into this List, then a good old class should do the trick.

为什么List必须是unknowntype?为什么它不能成为一个真正的类,具有构造函数所需的真实属性供您实例化?如果需要扩展该类,则可以继承该类。根据您提供的信息,如果您需要的是进入此List的类中的一些属性,那么一个好的旧类应该可以做到。