public void OtherMethod()
    {
        IFooable nc = ContainingClass.CreateFooable();

        Console.WriteLine(nc.Foo);
        nc.Foo = 42;                            // Now it's an error :P
    }
Пример #2
0
    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();
            }
        }
    }