Skip to content

Instantly share code, notes, and snippets.

@stofte
Created October 7, 2013 19:00
Show Gist options
  • Save stofte/6873121 to your computer and use it in GitHub Desktop.
Save stofte/6873121 to your computer and use it in GitHub Desktop.
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