Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
375 views
in Technique[技术] by (71.8m points)

inheritance - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...