简体   繁体   English

当该子项作为其父项存储在列表中时,如何访问Child类的接口属性?

[英]How can I access a Child class's interface property when that child is stored in a list as its Parent?

I think the title of this post might not explain my issue very well, so here is some code that should hopefully explain it a little better: 我认为这篇文章的标题可能无法很好地解释我的问题,所以这里有一些代码应该希望能更好地解释一下:

interface IFoo {
    IFoo Bar {get; set;}
}

class Parent {

}

class Child1 : Parent, IFoo {

}

class Child2 : Parent, IFoo {

}

class Manager {
    List<Parent> Parents = new List<Parent>();

    Parents.Add(new Child1());
    Parents.Add(new Child2());

    // I want to do something like this:
    Parents[0].Bar = Parents[1];
}

I believe (Parents[0] is IFoo) will evaluate as true , and yet I can't use Parents[0].Bar because Parent does not contain a definition for Bar . 我相信(Parents[0] is IFoo)将评估为true ,但我不能使用Parents[0].Bar因为Parent不包含Bar的定义。 Can you explain why this is? 你能解释一下这是为什么吗? Also, how can I effectively do the same thing that (at least in my head) Parents[0].Bar = Parents[1] should do? 另外,我怎么能有效地做同样的事情(至少在我脑海里) Parents[0].Bar = Parents[1]应该做什么?

Hopefully this makes sense. 希望这是有道理的。 I'll try to clarify things if needed. 如果需要,我会尽力澄清事情。 Thanks in advance for taking a look! 在此先感谢您的光临!

Use type casting: 使用类型转换:

((IFoo)Parents[0]).Bar

But actually it is bad design in your case if you depend on inplementation. 但实际上,如果你依赖于实体,那么你的设计就是糟糕的设计。

If you want to do the assignment, you need to let your code have the same information that you have yourself, and also the same information that the Manager class has. 如果要进行分配,则需要让代码具有与自己相同的信息,以及与Manager类相同的信息。 That means that you must tell the class Parent that it knows about IFoo, and that means: 这意味着你必须告诉类Parent它知道IFoo,这意味着:

 abstract class Parent : IFoo
{
    public abstract IFoo Bar { get; set; }
}

It now also states that you can't instantiate a Parent alone. 它现在还声明您无法单独实例化Parent。 From your comments, it seems to make sense (you saying Parent should have no implementation of Bar, only know about it). 从您的评论中,它似乎有意义(您说父母应该没有Bar的实现,只知道它)。 If that makes sense it is abstract, but if not remove the abstract and add the implementation of Bar to Parent, or if your real implementation differs for each child, make it virtual. 如果这是有意义的,那么它是抽象的,但如果不删除摘要并将Bar的实现添加到Parent,或者如果每个子项的实际实现不同,则将其设置为虚拟。

暂无
暂无

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

相关问题 子类可以实现与其父类相同的接口吗? - Can a child class implement the same interface as its parent? 定义抽象类时,是否可以强制子类定义父接口中定义的属性/方法? - When defining an abstract class, can you force a child to define a property/method defined in a parent interface? 如何按子类属性为父类列表排序? - How to do order by child class property for a list of parent class? 我可以要求孩子初始化在其父类中声明的变量吗? - Can I require that a child initialize variables declared in its parent class? 如何使子对象知道在其父对象上为其分配的属性名称? - How can I make a child object aware of the property name to which it is assigned on its parent? 如何将子实例反序列化为父对象而不丢失其特定属性? - How can I deserialize a child instance as a parent object without losing its specific property? 子对象如何访问其父对象的属性? - How can a child object access the properties of its parent? XAML - 如何从 ControlTemplate 中的子控件访问父控件的 DataTemplate 属性? - XAML - How do I access a parent control's DataTemplate property from a child in a ControlTemplate? 在接口方法中使用父类的子类时接口的实现 - Implementation of interface when using child class of a parent class in method of interface 如何让子页面操作其父页面的属性、UI 元素和方法? - How can I have a child Page manipulate its parent Page's properties, UI elements, and methods?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM