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.
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.
First of all the reason you are getting the result you are getting the result you are getting is because of you specifically declared theDerivedObject as DerivedClass
therefor it will go DerivedClass
as parameter and with (BaseClass) theDerivedObject)
you bassically just told the compiler (correct me if im wrong with compiler) or program to take the method with the parameter BaseClass
What you want to do is one of the following options
Number 1:
public class Processor
{
public string Compose(BaseClass item)
{
if (item is DerivedClass)
return Compose((DerivedClass) item);
return item.Foo;
}
public string Compose(DerivedClass item)
{
return item.Foo + item.Bar;
}
}
Number 2
public class Processor
{
public string Compose(BaseClass item)
{
if (item is DerivedClass)
{
var derived = (DerivedClass) item;
return derived.Foo + derived.Bar;
}
return item.Foo;
}
}
Or you might want to overide ToString()
public class Processor
{
public string Compose(BaseClass item)
{
var @class = item as DerivedClass;
return @class?.ToString() ?? item.ToString();
}
}
public class BaseClass
{
public string Foo { get; set; }
public override string ToString()
{
return Foo;
}
}
public class DerivedClass : BaseClass
{
public string Bar { get; set; }
public override string ToString()
{
return Foo + Bar;
}
}
If you are not using C#6.0 then Processor will be the following
public class Processor
{
public string Compose(BaseClass item)
{
var @class = item as DerivedClass;
if (@class != null)
{
return @class.ToString();
}
return item.ToString();
}
}
public class Processor
{
public string Compose(BaseClass item)
{
return item.Compose();
}
}
public class BaseClass
{
public string Foo { get; set; }
public virtual string Compose()
{
return Foo;
}
}
public class DerivedClass : BaseClass
{
public string Bar { get; set; }
public override string Compose()
{
return base.Compose() + Bar;
}
}
No wonder you are getting what you get. You overload Compose
method. You declare two signatures one for base class, another for derived. Then you call the method several times:
processor.Compose(theBaseObject)
calls Compose(BaseClass item)
processor.Compose(theDerivedObject)
calls Compose(DerivedClass item)
. It happens here that several methods might be suitable for the call. Compiler selects a method having closest base class in the hierarchy which is itself DerivedClass
.
processor.Compose((BaseClass) theDerivedObject))
calls Compose(BaseClass item)
because you've told him to do it. It has no options to chose from.
You've might be interrested in class virtual member overriding. See
public class BaseClass
{
public string Foo { get; set; }
public virtual string GetComposeResult() { return Foo; }
}
public class DerivedClass : BaseClass
{
public string Bar { get; set; }
public override string GetComposeResult() { return Foo + Bar; }
}
In this case using following code would do what you expect.
public class Processor
{
public string Compose(BaseClass item) { return item.GetComposeResult(); }
}