Click here to Skip to main content
12,752,634 members (34,581 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# Inheritance Parsing
Hey i was here to day playing around with interfaces to try get better to them (cos i never use anything but abstract classes cos i never feel interfaces is much of use) but i have made the following code:

class Program
        static void Main(string[] args)
            List<maintestclass> tl = new List<maintestclass>() { new testclass(), new testclass(), new testclass2()};
            List<testinterface> ti = new List<testinterface>();


    public class testclass : maintestclass, testinterface
        public void testmetode()
            Console.WriteLine("interface call");

    public class testclass2 : maintestclass

    public abstract class maintestclass


    public interface testinterface
        void testmetode();

and what i really wanna do is to parse every object in "tl" that can inherit from testinterface to ti but it seems i cant make it to work, i've tried to use "where" and get type but cant find a usefull way to do it...

if anyone fast could help me i would be greatfull. (sample codes would help alot)

- Jackie
Posted 16-May-12 8:36am
Updated 16-May-12 8:37am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

If I understood correctly, you can cast an object instance to your interface like:

testinterface someVar = originalVar as testinterface;

after that someVar should contain the variable as your interface if the original instance implemented the interface. If it didn't, someVar is null.
Jackssen 16-May-12 13:53pm
Thanks is there a way to do this by using .where on a list?
lewax00 16-May-12 14:16pm
Something like this?
Where(x => (x as testinterface) != null)
Mika Wendelius 16-May-12 14:28pm
Yep, just like that :)
Jackssen 16-May-12 14:39pm
keep getting "Error: Cannot convert type 'System.Collections.Generic.List<interfacetest.maintestclass>' to 'System.Collections.Generic.List<interfacetest.testinterface>'"

this code is the problem: ti = (List<testinterface>)tl.Where(x => (x as testinterface) != null).ToList();

Thanks alot for helping :)
Mika Wendelius 16-May-12 14:44pm
How are ti and tl defined?
Jackssen 16-May-12 14:46pm
as following: (see source above for details):
List(maintestclass) tl = new List(maintestclass)() { new testclass("test"), new testclass("test"), new testclass2("test") };

List(testinterface) ti = new List(testinterface)();

those greater/lesser thing wont work it takes it as HTML...
lewax00 16-May-12 14:52pm
Why are you converting a list into a list?

ti = tl.Where(x => (x as testinterface) != null).ToList();

Should work fine...
Jackssen 16-May-12 14:55pm
almost same error as above with that code :)

can not implicitly convert bla bla...
Mika Wendelius 16-May-12 14:56pm
Ok, what if you try something like this:

System.Collections.Generic.List<object> tl = new System.Collections.Generic.List<object>();

tl.Add(new testclass("test"));

tl.Add(new testclass("test"));

tl.Add(new testclass2("test"));
System.Collections.Generic.List<testinterface> ti = new System.Collections.Generic.List<testinterface>();

ti = tl.Where(x => (x as testinterface) != null).OfType<testinterface>().ToList();
Jackssen 16-May-12 15:02pm
The problem with this is that tl MOST be list of maintestclass, sorry...
Jackssen 16-May-12 15:05pm
WOW! made it work just using the "oftype" thanks a lot :)
Mika Wendelius 16-May-12 15:10pm
Glad it got solved :)
Jackssen 16-May-12 15:14pm
its silly you cant cast it as i tried... but lists of generic types have always been a pain... i wished i've seen oftype earlier haha :D anyway thanks again +5 from here for great help chat and support have a nice day/evening :)
Mika Wendelius 16-May-12 15:16pm
Thanks and the same to you :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170217.1 | Last Updated 16 May 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100