Created
October 7, 2013 19:00
-
-
Save stofte/6873121 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace ConsoleApplication1 | |
{ | |
public interface IFoo | |
{ | |
int Value { get; } | |
} | |
public interface IBar : IFoo | |
{ | |
int Stuff(); | |
} | |
public class Foo : IFoo | |
{ | |
public int Value { get; set; } | |
} | |
public class Bar : IBar | |
{ | |
public int Value { get; set; } | |
public int Stuff() | |
{ | |
return Value; | |
} | |
} | |
public class Container | |
{ | |
private IList<IFoo> _list = new List<IFoo>(); | |
public void Add<T>(T o) where T : IFoo | |
{ | |
_list.Add(o); | |
} | |
public IEnumerable<IFoo> GetList<T>() where T : IFoo | |
{ | |
return _list.Where(e => typeof(T).IsAssignableFrom(e.GetType())); | |
} | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var c = new Container(); | |
c.Add(new Foo { Value = 1 }); | |
c.Add(new Bar { Value = 42 }); | |
var l = c.GetList<IBar>(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment