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

appsecguy

I have an application that displays several types of widgets. I pass the view the base abstract class, 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

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. I want to make sure, in my razor view, which is accepting an IEnumerable, that I can properly display each item.

Andrew

You can use the "is" operator to test if your GraphWidgets are of a certain derived class:

    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:

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how can I obtain HWND of a class derived from QMainWindow

From Dev

Check if type is derived from abstract generic class

From Dev

How can I create an instance of a derived class from an instance of a base class and include private fields?

From Dev

How can I retrieve a list of workitems from TFS in C#?

From Dev

How to use a covariant return to retrieve a derived class from a base class?

From Dev

How can I retrieve a list of objects from a class relation?

From Dev

Get list of classes derived from given Class

From Dev

How can I implement a Singleton class that can be derived from in WPF?

From Dev

An abstract class which can be derived by database classes

From Dev

polymorphism c++, trying to access member functions of classes that are derived from abstract base class

From Dev

How do I declare an array of any type (derived from a given type) in an abstract class?

From Dev

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

From Dev

In JavaScript how can I retrieve class from it's string name?

From Dev

C++: Use pointer from abstract class for accessing method of derived classes; derived class apears to be abstract as well

From Dev

Mocking an abstract class derived from an abstract class

From Dev

Why I must declare field as "public" in abstract class to access it from derived classes?

From Dev

how can I obtain HWND of a class derived from QMainWindow

From Dev

how to access derived class object from abstract class in boost python

From Dev

How can I define asp.net Htmlhelper extensions for derived classes of an abstract base class?

From Dev

How would I validate additions in a class derived from List<T>?

From Dev

How can I get name of interface from abstract class in PHP

From Dev

How can I maintain a compile-time list of all types derived from a base class?

From Dev

How to annotate an instance variable derived from an abstract Class?

From Dev

How can I avoid dynamically generated classes derived from metaclass not to end up as the same class?

From Dev

How can I implement a Singleton class that can be derived from in WPF?

From Dev

How i can design these with abstract class

From Dev

How can I call virtual function definition of base class that have definitions in both abstract base class and derived class in C++?

From Dev

Derived Class Constructor from an Abstract Class

From Dev

New derived class from abstract classes

Related Related

  1. 1

    how can I obtain HWND of a class derived from QMainWindow

  2. 2

    Check if type is derived from abstract generic class

  3. 3

    How can I create an instance of a derived class from an instance of a base class and include private fields?

  4. 4

    How can I retrieve a list of workitems from TFS in C#?

  5. 5

    How to use a covariant return to retrieve a derived class from a base class?

  6. 6

    How can I retrieve a list of objects from a class relation?

  7. 7

    Get list of classes derived from given Class

  8. 8

    How can I implement a Singleton class that can be derived from in WPF?

  9. 9

    An abstract class which can be derived by database classes

  10. 10

    polymorphism c++, trying to access member functions of classes that are derived from abstract base class

  11. 11

    How do I declare an array of any type (derived from a given type) in an abstract class?

  12. 12

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

  13. 13

    In JavaScript how can I retrieve class from it's string name?

  14. 14

    C++: Use pointer from abstract class for accessing method of derived classes; derived class apears to be abstract as well

  15. 15

    Mocking an abstract class derived from an abstract class

  16. 16

    Why I must declare field as "public" in abstract class to access it from derived classes?

  17. 17

    how can I obtain HWND of a class derived from QMainWindow

  18. 18

    how to access derived class object from abstract class in boost python

  19. 19

    How can I define asp.net Htmlhelper extensions for derived classes of an abstract base class?

  20. 20

    How would I validate additions in a class derived from List<T>?

  21. 21

    How can I get name of interface from abstract class in PHP

  22. 22

    How can I maintain a compile-time list of all types derived from a base class?

  23. 23

    How to annotate an instance variable derived from an abstract Class?

  24. 24

    How can I avoid dynamically generated classes derived from metaclass not to end up as the same class?

  25. 25

    How can I implement a Singleton class that can be derived from in WPF?

  26. 26

    How i can design these with abstract class

  27. 27

    How can I call virtual function definition of base class that have definitions in both abstract base class and derived class in C++?

  28. 28

    Derived Class Constructor from an Abstract Class

  29. 29

    New derived class from abstract classes

HotTag

Archive