1

I am dealing with an interface as follows:

public interface ISomething { ...many auto-props, void SetValues(ISomething thing) } 

Now, I don't own this interface, but I'd like to extend it with a couple more properties:

public interface ISomethingMoreSpecific : ISomething { ...existing + my props, void SetValues(ISomething thing) } 

In the class the implements ISomethingMoreSpecific I've implemented an overload that takes the derived interface and handles both my props and the base interface properties.

public void SetValues(ISomethingMoreSpecific specificThing) { ...set my props and base props } 

The calling code does the following:

myThing.SetValues((ISomethingMoreSpecific)otherThing); 

With or without the cast, the method will not dispatch to my overload even though otherThing and myThing are concrete types implementing ISomethingMoreSpecific. I am guessing I'm overlooking something simple, but what is it?

2 Answers 2

2

Include

void SetValues(ISomethingMoreSpecific specificThing); 

into ISomethingMoreSpecific.

Sign up to request clarification or add additional context in comments.

Comments

0

You have void SetValues(ISomething thing) again in ISomethingMoreSpecific. Do you intend to hide that, then use the new keyword. If you do not want to hide that you need to change void SetValues(ISomething thing) to void SetValues(ISomethingMoreSpecific) in ISomethingMoreSpecific. Below is the code when you intend to hide and it does work with casting. Even if you don't hide it, i.e. don't use the new keyword. it works.

public class Program { public void Main(string[] args) { MyThing a = new MyThing(); MyThing b = new MyThing(); a.SetValues(b);//calls more specific a.SetValues((ISomething)b);//calls just the thing } } public class MyThing : ISomethingMoreSpecific { public void SetValues(ISomethingMoreSpecific specificThing) { Console.WriteLine ("more specific"); } public void SetValues(ISomething thing) { Console.WriteLine ("just the thing"); } } public interface ISomethingMoreSpecific : ISomething { //...existing + my props, new void SetValues(ISomething thing); } public interface ISomething { //...many auto-props, void SetValues(ISomething thing) ; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.