简体   繁体   English

如何从抽象类列表中检索派生类?

[英]How can I retrieve the derived class from a list of abstract classes?

I have an application that displays several types of widgets. 我有一个显示几种类型的小部件的应用程序。 I pass the view the base abstract class, GraphWidget. 我将基本的抽象类GraphWidget传递给视图。 The goal is to loop through those, and create the appropriate display for each type of graph. 目标是遍历这些图形,并为每种图形创建适当的显示。

How would I go about looping through that collection and obtaining the appropriate information from the derived class so my view knows how to render each widget? 我将如何遍历该集合并从派生类中获取适当的信息,以便我的视图知道如何呈现每个小部件?

EDIT This is a .NET MVC5 application 编辑这是一个.NET MVC5应用程序

Sample code: 样例代码:

public abstract class GraphWidget {
    public abstract string Display();
    public string Title {get; set;}
    public GraphWidget(string title){
        this.Title = title;
    }
}

public class BarGraph : GraphWidget{
     public override string Display(){
          return stuff...
     }
}

So for example, if I have a mix of bar graphs and pie graphs, their Display() function will be different. 因此,例如,如果我混合使用条形图和饼图,则它们的Display()函数将有所不同。 I want to make sure, in my razor view, which is accepting an IEnumerable, that I can properly display each item. 我想在接受IEnumerable的剃须刀视图中确保可以正确显示每个项目。

You can use the "is" operator to test if your GraphWidgets are of a certain derived class: 您可以使用“ is”运算符来测试您的GraphWidgets是否属于某个派生类:

    var graphs = new List<GraphWidget>{new BarGraph("Bar"), new PieGraph("Pie")};
    foreach(var graph in graphs)
    {
       if (graph is BarGraph)
          { // it's a BarGraph 
          }
       else if (graph is PieGraph)
          { // it's a PieGraph 
          }
    }

If you need use them as the derived class, then you can use the "as" operator: 如果需要将它们用作派生类,则可以使用“ as”运算符:

    /* Note: if graph is actually of type PieGraph, 
             then barGraph would be null */
    var barGraph = graph as BarGraph; 

As I understand you, you can use LINQ method OfType : 据我了解,您可以使用LINQ方法OfType

var list = new List<GraphWidget>();
var badList = list.OfType<BarGraph>().ToList();

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

相关问题 如何在非抽象基类的派生类中强制重写? - How to force overrides in a derived class from non abstract base classes? 如何在从抽象类派生的类中创建新方法? - How to create new methods in classes derived from an abstract class? 一个抽象类,可以由数据库类派生 - An abstract class which can be derived by database classes 如何遍历包含从该抽象 class 派生的非抽象类型的抽象类型列表? - How could I iterate through list of abstract type, containing non-abstract types derived from that abstract class? 如何从另一个派生 class 访问其他派生类? - How can I access other derived classes from another derived class? Array中抽象类的派生类 - Derived classes of abstract class in an Array 为什么我必须在抽象类中将字段声明为“公共”才能从派生类访问它? - Why I must declare field as “public” in abstract class to access it from derived classes? 如何防止具有公共派生类的抽象类在其他程序集中被继承? - How to prevent an abstract class with public derived classes from being inherited in other assemblies? C#:如何从包含抽象类的 IReadOnlyList 中获取派生 class 的值 - C#: How to get the value of derived class from an IReadOnlyList containing abstract classes 如何强制所有派生类实现抽象方法或属性? - How can I force all derived classes to implement an abstract method or property?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM