C#: A method parameter that implements a particular interface - c#

Let's say I have a class called UIViewController.
public class UIViewController
{
// ... methods
}
And I have an interface IDoFancyFlips
public interface IDoFancyFlips
{
// ... methods
}
I have a classes that extend UIViewController and implement the interface as follows.
public class FancyViewController : UIViewController, IDoFancyFlips
{
// ... methods
}
Now I want to write a method that takes a UIViewController as a parameter, but only ones that implement IDoFancyFlips.
public void FlipAndShow(??? controller) {
// implementation
}
My question is what do I put in the ???. I couldn't seem to find any way to do this.
I can do this in Objective C as follows:
- (void) flipAndShow:(UIViewController<IDoFancyFlips>*) controller {
// implementation
}
But I can't find the equivalent declaration in C#.

You can use generics to accomplish this. Try the following:
public void FlipAndShow<TFancyFlippingController>
(TFancyFlippingController fancyFlippingController)
where TFancyFlippingController : UIViewController, IDoFancyFlips
{
// implementation
}
The where TFancyFlippingController : UIViewController, IDoFancyFlips constraint ensures that the argument specified for TFancyFlippingController must both inherit from UIViewController and implement the IDoFancyFlips interface.

Considering that all your specific view controller inherits from base UIViewController class
public class FancyViewController : UIViewController, DoFancyFlips
{
// ... methods
}
public class NormalViewController : UIViewController
{
// ... methods
}
you can check at runtime whether the passed instance is of type DoFancyFlips and then do the processing further; like
public static void FlipAndShow(UIViewController controller)
{
if (!(controller is DoFancyFlips))
return;
// implementation
}

Related

How can I inherit from a control class and an abstract class?

I have a class with a method that accepts an argument which must be a Control with expected methods.
I've created an interface with those methods.
I've created an abstract class that inherits from Control and implements the interface putting all methods abstract (this is the type of my argument above).
Then i've created a class that inherits from TableLayoutPanel and implements the interface.
I create an instance of this class but then i cannot use it as the argument.
I know why. But what is the workaround? I know i could add a method to the interface that returns the Control instance. In this case, the TableLayoutPanel, but i wanted to use the instance itself...
Also, i don't want to make casts inside the method that receives the argument, it has to be "compile-time/type safe" to use in a library for example...
class CollapsibleList : Panel
{
public void AddItem(CollapsibleListItem item)
{
someContainer.Controls.Add(item);
item.CollapsibleListItemCollapse();
}
}
public interface ICollapsibleListItem
{
string CollapsibleListItemName { get; }
void CollapsibleListItemCollapse();
void CollapsibleListItemExpand();
}
public abstract class CollapsibleListItem : Control, ICollapsibleListItem
{
public abstract string CollapsibleListItemName { get; }
public abstract void CollapsibleListItemCollapse();
public abstract void CollapsibleListItemExpand();
}
class ListBoxCollapsibleListItem : TableLayoutPanel, ICollapsibleListItem
{
//... implemented interface methods
}
class Main
{
public void SomeMethod()
{
var item = new ListBoxCollapsibleListItem();
var collapsibleList = new CollapsibleList();
collapsibleList.AddItem(item as CollapsibleListItem); //cast error!
}
}
try below,
collapsibleList.AddItem(item as ICollapsibleListItem);

Cannot pass a sub-interface as parameter in C#

I have the following interface hierarchy:
public interface IActor { ... }
public interface IHealthUser : IActor { ... }
and a third interface:
public interface IAction { void Perform(IActor caster) }
Why is the following not legal and can I get around it somehow?
public class HealthPotion : IAction
{
public void Perform(IHealthUser caster) { ... }
}
The contract defined by IAction states that you can Perform the action on any IActor. Any IActor. Not only IHealthUser. What your HealthPotion tries to do is only implement a subset of IAction, meaning to perform its task only on a subset of IActors. That's not what the IAction interface states.
If you want a specific IAction to be able to restrict the type of IActor it applies to, you can do it using generic constraints:
public interface IAction<TAppliesTo> where TAppliesTo : IActor
{
void Perform(TAppliesTo appliesTo);
}
public class UniversalAction : IAction<IActor>
{
public void Perform (IActor anyone) {}
}
public class HealthPotion : IAction<IHealthUser>
{
public void Perform (IHealthUser healthUserOnly){}
}
You need to implement the same signature as your interface defines. You are using IActor in the interface but then you are using IHealthUser in the implementation of IAction interface. You should implement the Perform with the IActor parameter. It can be called however with class that implements IHealthUser.

Abstract method to be overridden with concrete type

Maybe this is a dumb question. But, I don't get the point what I am missing.
Given the following class-definition
public abstract class AbstractBaseClass
{
public abstract void Create(AnotherAbstractClass param1);
}
Wheras AnotherAbstractClass is defined
public abstract class AnotherAbstractClass
{
}
with a concrete implementation
public class AnotherConcreteImplementation : AnotherAbstractClass
{
}
I want to be able to have the override of the Create method to use a concrete type:
public class ConcreteImplementation : AbstractBaseClass
{
public override void Create(AnotherConcreteImplementation param1) <-- There is no suitable method for override
{
// param1 is an instance of the concrete implementation
}
public override void Create(AnotherAbstractClass param1) <-- this is working but I'll have to cast on each implementation
{
// param1 is an instance of the abstract class and needs a cast
}
}
Is this simply not possible or is there some way I'm not aware of? Maybe using generics?
Edit #1 (added more context)
I'm trying to achieve/enforce that in a concrete implementation there is only one parameter valid.
Think of it like it's a database-layer. The Create method will create a new entry in the database. As of each table has different values, the create-parameter also has.
The casting inside smells (in my opinion) as of it can be called with any concrete implementation of AnotherAbstractClass.
public class AddressTable : AbstractBaseClass
{
public override void Create(AnotherAbstractClass param1)
{
// cast to concrete instance
var casted = (ConcreteAddressCreate)param1;
}
}
public class CityTable : AbstractBaseClass
{
public override void Create(AnotherAbstractClass param1)
{
// cast to concrete instance
var casted = (ConcreteCityCreate)param1;
}
}
Having an instance of AddressTable I can call
addressIntance.Create(new ConcreteAddressCreate()); // would be okay
on the other hand I can call it
addressIntance.Create(new ConcreteCityCreate()); // would be okay but will fail at runtime with InvalidCastException
Edit #2 (additional info)
It should also be possible to extend the AbstractBaseClass class with more abstract methods later.
So, for me it's more likely to have generic methods instead of an concrete class-implemenation with 200 generic parameters for each method to implement.
It violates the Liskov Substitution Principle so it makes perfect sense you can't do this. Namely, you can't just "have" covariance like this for free:
AbstractBaseClass bcl = new ConcreteImplementation();
bcl.Create(new DifferentImplementationWithoutSecondAbstract());
The contract AbstractBaseClass defines makes Create have to work with any implementation of AbstractBaseClass passed in - if you give a constraint on what can be passed in you've violated the contract it defines.
Like you assumed - you can use generics:
// notice the recursive definition, we require the generic parameter
// to be a generic parameter of itself - allowing AbstractBaseClass
// to not be aware of its subclasses like in the other answers.
public abstract class AbstractBaseClass<T> where T : AbstractBaseClass<T>
{
public abstract void Create(T param1);
}
public class Concrete : AbstractBaseClass<Concrete>
{
public override void Create(Concrete param1)
{
Console.WriteLine("Hello!");
}
}
Yes, you can do that using generics:
public abstract class AbstractBaseClass<T>
where T : AnotherAbstractClass
{
public abstract void Create(T param1);
}
public class ConcreteImplementation : AbstractBaseClass<AnotherConcreteImplementation>
{
public override void Create(AnotherConcreteImplementation param1)
{
}
}
Generics is indeed the way to do it.
public abstract class AbstractBaseClass<TDerivedClass> where TDerivedClass : AnotherAbstractClass
{
public abstract void Create(TDerivedClass param1);
}
And then you can do:
public class ConcreteImplementation : AbstractBaseClass<AnotherConcreteImplementation>
{
public override void Create(AnotherConcreteImplementation param1) // Works because TDerivedClass = AnotherConcreteImplementation
{
...
}
}

Calling Generic Method on Generic Field?

I have the following and at some point I need to create Failures for Validations. We suppose each type deriving from Validation has one and only one type deriving from Failure<T> where T is the aforementioned implementation of Validation.
As I have a growing number of implementations of Validation, I need to be able to instantiate the right type deriving from Failure<T>, and call the link method on it within a method that looks like
void recordFailureForValidation(Validation v) {
Type failureType = dict[v.GetType()];
Object failure = Activator.CreateInstance(failureType);
// how do I call failure.link(v) ?
}
At Runtime, a dictionary gives me the type deriving from Failure<T> given T.
I am able to instantiate Failure<T> (Failure1, Failure2, etc...), but I can't find how to call link on the public field reference of my newly created Failure instance (by making all uses that made sense to me of GetMethod, MakeGenericMethod, Invoke, etc...)
public class MyReferenceClass<T>
where T : Object, new() {
public void link(T arg) { ... }
}
public abstract class Failure<T>
where T : ValidationRule, new() {
...
public MyReferenceClass<T> reference;
...
}
public class Failure1 : Failure<Validation1> {
}
public class Failure2 : Failure<Validation2> {
}
public abstract class ValidationRule {
...
}
public class ValidationRule1 : ValidationRule {
...
}
public class ValidationRule2 : ValidationRule {
...
}
link is private since you do not specify a different accessibility. Make it public or internal:
public class MyReferenceClass<T>
where T : Object, new() {
public void link(T arg) { ... }
}
then you can call it from Failure<T> through the reference property:
public abstract class Failure<T>
where T : ValidationRule, new()
{
protected T Validation {get; set;};
public MyReferenceClass<T> reference;
}
public class Failure1 : Failure<Validation1>
{
public void Test()
{
this.reference.link(Validation);
}
}
Let Failures implement a non generic IFailure interface as well as a generic one in the same manner as IEnumerable and IEnumerable<T>
Create an abstract factory method within ValidationRule that has to be implemented by each concrete Validation
public ValidationRule1 : ValidationRule
{
public override IFailure ToFailure()
{
return new Failure1(this);
}
...
}

Overriding a method returning a generic class

in my Silverlight 4 application I started creating and using some generics and now I stumbled upon the following problem:
In a non-generic class, I have a abstract method, that returns a generic class:
public abstract class DTO_Base()
{
public abstract ServiceModelBase<ServiceNodeBase> CreateBusinessObject();
}
The generic class is defined in the following way:
public abstract class ServiceModelBase<RootNodeType> where RootNodeType : ServiceNodeBase
Naturally, from DTO_Base derived classes will have to override the CreateBusinessObject method:
public class DTO_Editor : DTO_Base
{
public override ServiceModelBase<ServiceNodeBase> CreateBusinessObject()
{
// the object to return have to be of type ServiceModelEditor
// which is derived from ServiceModelBase<ServiceNodeEditor>
// public class ServiceModelEditor : ServiceModelBase<ServiceNodeEditor>
// ServiceNodeEditor is derived from ServiceNodeBase
// public class ServiceNodeEditor : ServiceNodeBase
ServiceModelEditor target = new ServiceModelEditor()
...
Functions to populate the 'target'
...
return target;
}
}
The line return target; causes an error, stating that it isn't possible to implicitly convert the type ServiceModelEditor in ServiceModelBase<ServiceNodeBase>. Also, an explicit conversion via target as ServiceModelBase<ServiceNodeBase> doesn't work.
How would I have to implement this method to work?
Try this:
public interface IDTO<Node> where Node : ServiceNodeBase
{
ServiceModelBase<Node> CreateBusinessObject();
}
public abstract class DTO_Base<Model,Node> : IDTO<Node>
where Model : ServiceModelBase<Node>
where Node : ServiceNodeBase
{
public abstract Model CreateBusinessObject();
#region IDTO<Node> Members
ServiceModelBase<Node> IDTO<Node>.CreateBusinessObject()
{
return CreateBusinessObject();
}
#endregion
}
public class DTO_Editor : DTO_Base<ServiceModelEditor, ServiceNodeEditor>
{
public override ServiceModelEditor CreateBusinessObject()
{
// the object to return have to be of type ServiceModelEditor
// which is derived from ServiceModelBase<ServiceNodeEditor>
// public class ServiceModelEditor : ServiceModelBase<ServiceNodeEditor>
// ServiceNodeEditor is derived from ServiceNodeBase
// public class ServiceNodeEditor : ServiceNodeBase
ServiceModelEditor target = new ServiceModelEditor();
return target;
}
}
I have faced a similar problem before and the only thing reasonable to do is to make the core base class generic also. You can remove the Model generic parameter (and the interface) and it will look a little less scary, but you loose visibility on the functionality of ServiceModelEditor outside of the method.
As it is, you've got to return a ServiceModelBase<ServiceNodeBase>. One option is to make your base class generic:
public abstract class DtoBase<T> where T : RootNodeType
{
public abstract ServiceModelBase<T> CreateBusinessObject();
}
Then:
public class DtoEditor : DtoBase<ServiceNodeBase>
{
public override ServiceModelBase<ServiceNodeBase> CreateBusinessObject()
{
...
}
}
If you are using .Net 4.0 I suggest you use interfaces to define your ServiceModelBase and specify an out variance modifier on that interface generic type:
class ServiceNodeBase { }
class ServiceNodeEditor : ServiceNodeBase {/*implementation*/}
//
interface IServiceModelBase<out RootNodeType>
where RootNodeType : ServiceNodeBase {
}
class ServiceModelEditor : IServiceModelBase<ServiceNodeEditor> {
/*implementation*/
}
//
abstract class DTO_Base {
public abstract IServiceModelBase<ServiceNodeBase> CreateBusinessObject();
}
class DTO_Editor : DTO_Base {
public override IServiceModelBase<ServiceNodeBase> CreateBusinessObject() {
return new ServiceModelEditor();
}
}

Categories