Reputation: 44312
I need to add a new method (MethodC) to an interface but only for one specific class. Rather than clogging up the current interface (IMyInterface), I'd like to use another interface for this one class. This new interface would contain just the one new method (MethodC). But the class uses the new interface also needs to use methods in IMyInterface.
I'm not sure how that can be structured. I send back type IMyInterface from a factory method. I don't know how the second interface can be sent back or if I can cast to it sometime later.
IMyInterface
- MethodA()
- MethodB()
//new interface for single class
IMyInterfaceExtend
- MethodC()
//factory method definition
IMyInterface Factory()
IMyInterface myi = StaticClass.Factory()
myi.MethodA()
myi.MethodB()
// sometime later
// I know this doesn't work but kind of where I'm wanting to go
((IMyInterfaceExtend)myi).MethodC()
Any ideas how this can be accomplished?
Upvotes: 0
Views: 172
Reputation: 439
public interface BaseInterface
{
string FirstName { get; set; }
string LastName { get; set; }
void Method1();
}
public interface ExtendedInterface
{
string FulllName { get; set; }
void Method2();
}
public class ClassA : BaseInterface
{
public string FirstName { get; set; }
public string LastName { get; set; }
public void Method1()
{
}
}
public class ClassB : BaseInterface, ExtendedInterface
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string FullName { get; set; }
public void Method1()
{
}
public void Method2()
{
}
}
Upvotes: 2