Hey! Im doing a project, where i have to manage a bunch of different appliences.

I´ve made a custom generic collection class that has a few different search methods. Now the collection class has to hold all of the different appliances (so they all inherits from the same superclass: Appliance). I can't see how the search methods can return anything but an object of the superclass, so when they return that i would like to be able to cast it to the actual appliance. The only way i can see how that is possible is to make an if-else chain that checks on the object returneds type and then casts it from there.

Initially i thought you could use Type to actually create an object and cast, but that isn't the case. You could do it with generic method, but then that would return an object that i dont know the type of.

Basically the code where im having the problem looks like this:

//the searching method in my generic class, the _wrappedList is the private list used to store the data
public List<Appliance> FindPriceRange(double min, double max){
            List<Appliance> tmp = new List<Appliance>();

            foreach (Appliance app in _wrappedList)
                if (app.Price > min && app.Price < max)

            return tmp;
//this is the code where i call the method and would like to cast the Appliance returned, to their actual sub-type
foreach (Appliance app in Appliances.FindPriceRange(750, 840)) { 
    Console.WriteLine(app.Name + " " + app.Price + "\n");
    //conevert appliance to its sub-type
    //basically; typeof(app) MySubClass =(typeof(app)) app;
    //which i know doesnt work because you cant creat objects like that, but is there a way to do the same thing?

Thanks a bunch in advance. :)

5 Years
Discussion Span
Last Post by Ketsuekiame

You need to understand C#'s implementation of the Reflection pattern. There are API's that should allow you to create and object via type name. I had to do this (create the class factory in order to create an object by name) about 20 years ago for C++. That way, when we needed to create an object using its name in a serialized image (TCL or XML wire format), we could do so.

Anyway, I've read articles on this about C#, and have read the code to do it with Java, but since I haven't don't more than simple C# programming I cannot tell you specifically how to do it. Here is a link to the MS documentation on Reflection: http://msdn.microsoft.com/en-us/library/ms173183(v=vs.80).aspx

What version of Visual Studio are you using? There have been a few changes between VS2005 and VS2012. The link above (including the v=vs.80 part) is for 2005.

Edited by rubberman


Awsome that helped a tonne! I still couldnt quite figure out how to actually cast the object, as far i can understand from looking around for Reflection you need to create a class factory to do this(and class facotries is outside this years curriculum so ill elegantly skip that ^_^)? But with System.Reflection.PropertyInfo i can get specific property values, which is fine for what i wanted to do.


Let me ask you, why do you need to convert it to the sub-type, if you don't already know what sub-type you need to deal with?

The reason I ask is that, if you don't know what type you're dealing with, how can you call anything that is specific to that class?

If you know what sub-class you're dealing with, then you can easily make the cast.

If you're worried about mistakenly trying to cast objects in your list to the wrong type, there are a couple of methods you can use:

myList.Where(myObj => myObj is MySubType);
myList.Where(myObj => (myObj as MySubType) != null);

Both of those will return a list of objects of the correct type you need to deal with.

If you really don't know what type you're dealing with, then there's no point casting it.

When you cast down (back to a base object) you don't lose any information, so don't worry about passing around base types or interfaces (An interface might be better here actually)


Another search method i have only returns types that implement a certain interface, but since that interface isn't needed on every class the superclass doesnt implement it. So there methods/properties i know is there that i would like to use.

But the Reflection solves most of the problem in the sense that i can access the methods/properties induvidually.


That's pretty bad design though, you should re-think how this part of the software works.

What is it you're trying to achieve by instantiating disparate objects to their respective types (and hence call different functions)?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.