开发者

Any way to use a class extension method to support an interface method in C#?

Console app below compiles, but the interface cast fails at run time. Is there an easy way to make this work?

namespace ConsoleApplication1
{
    class Monkey
    {
        public string Shock { get { return "Monkey has been shocked."; } }
    }

    static class MonkeyExtensionToSupportIWombat
    {
        public static string ShockTheMonkey( this Monkey m )
        {
            return m.Shock;
        }
    }

    interface IWombat
    {
        string ShockTheMonkey();
    }

    class Program
    {

        static void Main( string[] args )
        {
            var monkey = new Monkey();
            Console.WriteLine( "Shock the monkey without the interface: {0}", monkey.Shock );
            IWombat wombat = monkey as IWombat;
            Console.WriteLine( "Shock the monkey with the interfac开发者_JAVA技巧e: {0}", wombat.ShockTheMonkey() );
            Console.ReadLine();
        }
    }
}


A Monkey is not derived from IWombat, so I'm not sure why you'd expect that to work (outside of using Reflection or a dynamic call, but that'd be spackling over a basic deficiency in the class design). If you wanted this to work, you'd have to have Monkey implement IWombat or have another class that inherits from Monkey that implements IWombat (as seen below).

class Monkey
{
    public string Shock { get { return "Monkey has been shocked."; } }
}

static class MonkeyExtensionToSupportIWombat
{
    public static string ShockTheMonkey(this Monkey m)
    {
        return m.Shock;
    }
}

interface IWombat
{
    string ShockTheMonkey();
}

class MonkeyBat : Monkey, IWombat
{
    #region IWombat Members

    public string ShockTheMonkey()
    {
        return this.Shock;
    }

    #endregion
}

class Program
{

    static void Main(string[] args)
    {
        var monkey = new Monkey();
        Console.WriteLine("Shock the monkey without the interface: {0}", monkey.Shock);

        var monkeyBat = new MonkeyBat();
        Console.WriteLine("Shock the monkey with the interface: {0}", monkeyBat.ShockTheMonkey());

        Console.ReadLine();
    }
}


The cast will always fail as Monkey does not implement IWombat. But that has nohting to do with extension methods. What are you trying to do?


I'm no expert on extension methods, but I think what you're trying to do is the programming equivalent of mixing metaphors. Extension methods basically were introduced so that someone w/out access to the source of a class could expand the functionality of the class. By saying something implements an interface, you're basically saying that you do have access to the source, and so there really should be no need for the extension method. If as the designer of a class, you want the consumer of that class to specify functionality, perhaps having Monkey.ShockTheMonkey call a delegate would be more appropriate?

Also, a Wombat is not a Monkey and a Monkey is not a Wombat. Your knowledge of zoology definitely needs some help ;).


The cast fails because Monkey does not implement IWombat. The cast will always fail.

It looks like he is trying to be able to use the methods on the interface with out actually implementing the interface. In the purest sense, this is not possible. They may be some hackery to get it work though but I dont think it is worth it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜