public void Foo(object o) { if (o == null) { throw new NullRefferenceException("Null reference", "o"); } IFooable f = o as IFooable; if (f != null) { f.Foo(); } else { throw new ArgumentException("Unexpected type: " + o.GetType()); } }
public void ProcessFooBars(IList <IFooBar> foobars) { foreach (var foobar in foobars) { // feature detection to see which interfaces the instance implements IBarrable bar = foobar as IBarrable; if (bar != null) { bar.Bar(); } IFooable foo = foobar as IFooable; if (foo != null) { foo.Foo(); } } }