Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to get c# to use method overload of most specific type rather than base type?

If you have a method which is overloaded with a derived type, the method called at run-time depends on the type of your variable, even if the underlying object is actually of the derived type:

class Program
{
    static void Main(string[] args)
    {
        BaseClass theBaseObject = new BaseClass
        {
            Foo = "FOO"
        };
        DerivedClass theDerivedObject = new DerivedClass
        {
            Foo = "FOO",
            Bar = "BAR"
        };

        Processor processor = new Processor();

        Console.WriteLine(processor.Compose(theBaseObject));
        Console.WriteLine(processor.Compose(theDerivedObject));
        Console.WriteLine(processor.Compose((BaseClass) theDerivedObject));
    }
}

public class Processor
{
    public string Compose(BaseClass item)
    {
        return item.Foo;
    }

    public string Compose(DerivedClass item)
    {
        return Compose((BaseClass)item) + item.Bar;
    }
}

public class BaseClass
{
    public string Foo { get; set; }
}

public class DerivedClass : BaseClass
{
    public string Bar { get; set; }
}

Actual Output:

FOO
FOOBAR
FOO

I would like to find a way to alter this behaviour such that the most specific method invoked for a given parameter.

Desired output:

FOO
FOOBAR
FOOBAR  // because theDerivedObject is an instance of DerivedClass 

This would allow specific processing for a bunch of items, all derived from a base.

Is this possible in c#?


Edit:

A clarification - in practice there would be no explicit cast, as the items would likely be in a list of collection of mixed types:

Example using a list without an explicit cast:

    foreach (BaseClass item in new []{ theBaseObject, theDerivedObject })
    {
        Console.WriteLine(processor.Compose(item));
    }

Actual output:

FOO
FOO

Desired output:

FOO
FOOBAR

Obviously the cast is still happening - but it's not easy to remove it.

like image 351
Kaine Avatar asked Mar 03 '16 11:03

Kaine


1 Answers

This code reminds me of the Haddocks' Eyes poem:

But I was thinking of a plan
 To dye one's whiskers green,
And always use so large a fan
 That they could not be seen.

First, your code creates a subclass, but then it casts the object back to the base class, so the compiler cannot see the actual type! The overload in your example is resolved at compile time, so the Compose(BaseClass item) will be called.

You could turn things around and have .NET resolve the overload dynamically for you by hiding all overloads, and exposing a single method that takes a base class and performs a dynamic cast:

public class Processor {
    public string Compose(BaseClass item) {
        return ComposeImpl((dynamic)item);
    }
    private string ComposeImpl(BaseClass item) {
        return item.Foo;
    }
    private string ComposeImpl(DerivedClass item) {
        return ComposeImpl((BaseClass)item) + item.Bar;
    }
}

The "magic" is on this line:

return ComposeImpl((dynamic)item);

the item is cast to dynamic, which tells the system that the actual overload of ComposeImpl must be picked based on the run-time type of the BaseClass object.

like image 200
Sergey Kalinichenko Avatar answered Nov 16 '22 02:11

Sergey Kalinichenko