Derived Interface with derived interface member - c#

I have 2 base interfaces, IViewBase (which all views will implement) and IPresenterBase (which all presenters will implement):
public interface IViewBase { }
public interface IPresenterBase
{
IViewBase View { get; set; }
}
Then i've created a new interface ILogPresenter that derives from IPresenterBase and ILogView deriving from IViewBase:
public interface ILogPresenter : IPresenterBase { }
public interface ILogView : IViewBase{ }
When i create a class that implements ILogPresenter,
public class LogPresenter: ILogPresenter
{
public ILogView View { get; set; }
}
I get an error:
'LogPresenter' does not implement interface member 'IPresenterBase.View'. 'LogPresenter.View' cannot implement 'IPresenterBase.View' because it does not have the matching return type of 'Views.IViewBase'.
I cannot set the return type of LogPresenter.View to ILogView which derives from IViewBase? I would like implement ILogPresenter with a different IView which derives from IViewBase.

You probably want to use generics here:
public interface IViewBase { }
public interface IPresenterBase<T> where T : IViewBase
{
T View { get; set; }
}
then:
public interface ILogPresenter : IPresenterBase<ILogView> { }
public interface ILogView : IViewBase{ }
Aside from covariant return types not being supported in C# (which would cause a problem even just with a getter) you have a fundamental problem in the original. I could do:
IPresenterBase foo = new LogPresenterImpl();
foo.View = new SomeOtherView();
where SomeOtherView implemented IVewBase but not ILogView. How would you expect your property to cope with that?
The generic version above solves this by allowing a presenter to express what kind of view it's presenting.

You can do this with generics:
public interface IViewBase { }
public interface IPresenterBase<T> where T : IViewBase
{
T View { get; set; }
}
public interface ILogPresenter : IPresenterBase<ILogView> { }
public interface ILogView : IViewBase { }
public class LogPresenter : ILogPresenter
{
public ILogView View { get; set; }
}

Related

C# How to create a collection of generic class which derives from a specific implementation of another generic class which is a derived class

Struggling with wording that title to make any sense so I will just show my code and try and explain it
So I have this base class
public abstract class SpacecraftProperty
{
}
public abstract class SpacecraftProperty<T> : SpacecraftProperty
where T : SpacecraftProperty<T>
{
public abstract void Add(T property);
}
With this I can create a collection of SpacecraftProperty just fine thanks to the base SpacecraftProperty class that is not generic.
Then I have this class that derives from SpacecraftProperty that is also generic
public class ResourceStorage<ResourceType> : SpacecraftProperty<ResourceStorage<ResourceType>>
where ResourceType : ResourceInfo, new()
{
public ResourceType resource { get; private set; } = new ResourceType();
public float capacity { get; private set; }
public override void Add(ResourceStorage<ResourceType> property)
{
this.capacity += property.capacity;
}
}
Now the issus is that I need to create a collection of this ResourceStorage class, where each object in the collection can be of any type for ResourceType. But I can't use the same trick as above where I create a non-generic base class as then when I try and derive from SpacecraftProperty<>, it won't know what type of the generic SpacecraftProperty to derive from.
For completeness, here is the code for ResourceInfo, which is an abstract class which will be derived from
public abstract class ResourceInfo
{
public abstract string GetName();
public abstract string GetDescription();
}
So in summary I would for example want a collection that contains say a ResourceStorage<SomeTypeA>, ResourceStorage<SomeTypeB> etc. where SomeTypeA and SomeTypeB derive from ResourceInfo. And ResourceStorage derives from SpacecraftProperty, which has the generic method Add.
Any ideas?
But I can't use the same trick as above where I create a non-generic base class as then when I try and derive from SpacecraftProperty<>, it won't know what type of the generic SpacecraftProperty to derive from.
You can use the same trick. The type to derive from is SpacecraftProperty<ResourceStorage>, where ResourceStorage is the non-generic type you have created. This is because the implementation of Add in the generic ResourceStorage<T> doesn't actually use T.
// note that I've changed the naming conventions to match that of C#'s
public abstract class ResourceStorage: SpacecraftProperty<ResourceStorage> {
public abstract float Capacity { get; protected set; }
}
public class ResourceStorage<TResource> : ResourceStorage
where TResource: ResourceInfo, new()
{
public TResource Resource { get; private set; } = new TResource();
public override float Capacity { get; protected set; }
public override void Add(ResourceStorage property)
{
Capacity += property.Capacity;
}
}
ResourceStorage<TResource> is now a SpacecraftProperty<ResourceStorage>, however, and not a SpacecraftProperty<ResourceStorage<TResource>>.
To fix that, you can make the abstract classes contravariant interfaces instead. Since there is only one Add method:
public interface ISpacecraftProperty {}
public interface ISpacecraftProperty<in T> : ISpacecraftProperty
where T : ISpacecraftProperty<T>
{
void Add(T property);
}
public interface IResourceStorage: ISpacecraftProperty<IResourceStorage> {
public float Capacity { get; }
}
public class ResourceStorage<TResource> : IResourceStorage
where TResource: ResourceInfo, new()
{
public TResource Resource { get; private set; } = new TResource();
public float Capacity { get; private set; }
public void Add(IResourceStorage property)
{
Capacity += property.Capacity;
}
}
Now this compiles:
var list = new List<IResourceStorage>() {
new ResourceStorage<Foo>(),
new ResourceStorage<Bar>()
};
// This'd work too, but I'm sure you are aware already, since it is the nature of what you are trying to do
list[0].Add(list[1]);
ISpacecraftProperty<ResourceStorage<Foo>> x = new ResourceStorage<Foo>();
If ISpacecraftProperty has other methods that return a T, and so can't be contravariant, you can always just add ISpacecraftProperty<ResourceStorage<TResource>> as yet another interface of ResourceStorage<TResource>.
public class ResourceStorage<TResource> : IResourceStorage, ISpacecraftProperty<ResourceStorage<TResource>>
where TResource: ResourceInfo, new()
{
public TResource Resource { get; private set; } = new TResource();
public float Capacity { get; private set; }
public void Add(IResourceStorage property)
{
Capacity += property.Capacity;
}
// implement explicitly by delegation
void ISpacecraftProperty<ResourceStorage<TResource>>.Add(ResourceStorage<TResource> property) {
Add(property);
}
}

Cannot implement interface member because it doesn't have matching return type

I'm working on an abstract algebra library for C#, but am having trouble with implementing interfaces. I have gotten the implementation to work for certain groups, but attempting to create rings is giving me some serious problems. In particular, I've got the following:
public class Scaffolding {
public interface IMonoid<T> : ISemiGroup<T> {
T Identity { get; set; }
}
public interface IGroup<T> : IMonoid<T> {
T Inverse(T a);
}
public interface IRing<T> {
IGroup<T> AdditiveStructure { get; set; }
IMonoid<T> MultiplicativeStructure { get; set; }
}
}
public class ModularMonoid : Scaffolding.IMonoid<int> {
// Implements all necessary monoid properties
}
public class ModularGroup : Scaffolding.IGroup<int> {
// Implements all necessary group properties
}
public class ModularRing : Scaffolding.IRing<int> {
public ModularGroup AdditiveStructure { get; set; }
public ModularMonoid MultiplicativeStructure { get; set; }
// Implement ring-specific properties
}
I get an error stating that 'ModularRing' does not implement interface member 'Scaffolding.IRing.AdditiveStructure'. 'ModularRing.AdditiveStructure' cannot implement 'Scaffolding.IRing.AdditiveStructure' because it does not have the matching return type of 'Scaffolding.IGroup'. I get a similar error for the MultiplicativeStructure. This is strange to me, because both the ModularGroup and ModularMonoid implement IGroup and IMonoid respectively.
Yes, those classes implement the interfaces, but that interface doesn't say "The type of the AdditiveStructure property is something that implements IGroup<T>" - it says that the type of the AdditiveStructure property is IGroup<T>. To implement the interface, you have to match return types exactly.
If you want to be able to implement the interface like that, you'd need to change the interface, potentially like this:
public interface IRing<T, TGroup, TMonoid>
where TGroup : IGroup<T>
where TMonoid : IMonoid<T>
{
TGroup AdditiveStructure { get; set; }
TMonoid MultiplicativeStructure { get; set; }
}
Then implement it as:
public class ModularRing : Scaffolding.IRing<int, ModularGroup, ModularMonoid>
{
public ModularGroup AdditiveStructure { get; set; }
public ModularMonoid MultiplicativeStructure { get; set; }
}
Alternatively, you should consider making the properties read-only. That way, if you're happy with the ModularRing users only using the IGroup<int> and IMonoid<int> definitions (rather than depending on anything extra exposed on ModularGroup and ModularMonoid) then you could stick with just the single type parameter, which would simplify things quite a lot. For example:
public interface IMonoid<T> : ISemiGroup<T>
{
T Identity { get; }
}
public interface IGroup<T> : IMonoid<T>
{
T Inverse(T a);
}
public interface IRing<T>
{
IGroup<T> AdditiveStructure { get; }
IMonoid<T> MultiplicativeStructure { get; }
}
Implementation:
public class ModularRing : Scaffolding.IRing<int>
{
public IGroup<int> AdditiveStructure { get; } = new ModularGroup();
public IMonoid<int> MultiplicativeStructure { get; } = new ModularMonoid();
}
(Or accept them in the constructor; I don't know enough about what you're trying to do with them.)

Generic interface/base class - access members without type constraints

Suppose I have a class structure looking like this:
public abstract class MyOtherBaseClass
{
public string HelloWorld;
}
public interface MyInterface<T> where T : MyOtherBaseClass
{
T MyObject { get; set; }
}
public abstract class MyBaseClass<T> : MyInterface<T>
where T : MyOtherBaseClass
{
public T MyObject { get; set; }
}
public class MyImplementation : MyBaseClass<MyOtherBaseClass>
{
}
Is there any way I can access MyObject in any implementation of MyBaseClass? I can't use a variable of MyBaseClass or MyInterface because I have to specify type constraints, but in my case I'm not interested in specifying them since all I want to do is access the value within.
Ideally I'd like to be able to do something like this:
MyBaseClass baseObject = null;
if(someCondition)
{
baseObject = new MyImplementation();
}
else if(otherCondition)
{
baseObject = new OtherImplementation(); //this also inherits from MyBaseClass
}
var objectValue = baseObject.MyObject;
var helloWorldValue = objectValue.HelloWorld;
What you want is not entirely possible, not with generics. The type MyBaseClass simply does not exists. A generic type must have a generic type argument.
If you do not want to use generics, why use generics?
This could also be a valid option:
public interface MyInterface
{
object MyObject { get; set; }
}
public abstract class MyBaseClass : MyInterface
{
public object MyObject { get; set; }
}
Of course, in this example you have to cast the object to a specific type.
You could also combine both techniques:
public interface MyInterface // This is the non-generic interface.
{
object MyObject { get; set; }
}
public interface MyInterface<T> // This is the generic interface.
where T : MyOtherBaseClass
{
T MyObject { get; set; }
}
public abstract class MyBaseClass<T> : MyInterface, MyInterface<T> // This class implements both the non-generic and the generic interface.
where T : MyOtherBaseClass
{
public T MyObject { get; set; } // Implementation of the generic property.
object MyInterface.MyObject // Implementation of the non-generic property.
{
get { return MyObject; }
set { MyObject = (T)value; }
}
}
...
MyInterface baseObject; // The non-generic interface is used as base object.
baseObject = new MyImplementation(); // It is assigned an instance of MyImplementation which uses a generic base class.
object value = baseObject.MyObject;

Inherit from generic type as interface

I am attempting to do something similar to:
public interface IView<T> : T where T : class
{
T SomeParam {get;}
}
So that i can later do
public class SomeView : IView<ISomeView>
{
}
Is it possible to specify inheritance using generics in this way or do i have to go the long way round and explicitly specify both interfaces when defining the class and do:
public interface IView<T>
{
T SomeParam {get;}
}
public class SomeView : IView<ISomeView>, ISomeView
{
}
This isn't possible, but your goal may be achievable with conversion operators. It seems that what you're trying to do is make it possible to pass an IView<T> as the T object which it contains. You could write a base class like this:
public abstract class ViewBase<T> {
public abstract T SomeParam { get; }
public static implicit operator T(ViewBase<T> view) {
return view.SomeParam;
}
}
Then, if you define a class like:
public class SomeView : ViewBase<ISomeView> { }
It can be accepted anywhere an ISomeView is expected:
ISomeView view = new SomeView();
Short answer: It is not possible. See this post
An Interface can't derive from a class. However nothing prevent you from doing this:
public interface ISomeView
{
}
public interface IView<out T> where T:class
{
T SomeParam { get; }
}
public class SomeView:IView<ISomeView>
{
public ISomeView SomeParam { get; set; }
}
Edit:
If you don't want to implement the T SomeParam { get; } each time you need to have an implementation, Does this would work?
public interface ISomeView
{
}
public abstract class BaseView<T> where T : class
{
public T SomeParam { get; set; }
}
public class SomeView : BaseView<ISomeView>{
}
In both case this would work:
public class main
{
public class OneOfThoseView : ISomeView
{
}
public main()
{
OneOfThoseView oneOfThose = new OneOfThoseView();
SomeView x = new SomeView();
x.SomeParam = oneOfThose;
}
}
Edit 2:
Not exactly what you want to do but this would force your SomeView class to return a BaseView<SomeView> class
public interface ISomeView
{
}
public abstract class BaseView<T> where T : BaseView<T>
{
public T SomeParam { get; set; }
}
public class SomeView : BaseView<SomeView>
{
}
Now only this would work.
public main()
{
SomeView y= new SomeView ();
SomeView x = new SomeView();
x.SomeParam = y;
}

Constraining T to a common generic property

I have the following:
public interface IEntity
{
IEntityContent Data { get; set; }
}
public interface IEntityContent { }
public interface IThingService<T>
{
void DoThing(T item);
}
public class BaseEntity<T> : IEntity
where T : IEntityContent
{
public abstract T Data { get; set; }
}
public class FooEntity : BaseEntity<FooContent>
{
public override FooContent Data { get; set; }
}
public class FooContent : IEntityContent
{
// Some properties
}
public class ThingService<T> : IThingService<T>
where T : IEntity
{
public void DoThing(T item)
{
Serializer.Instance.Serialize(item.Content);
}
}
The signature of Serializer.Instance.Serialize is:
string Serialize<T>(T from)
But I get the following:
'BaseEntity<T>' does not implement interface member 'IEntity.Data'. 'BaseEntity<T>.Data' cannot implement 'IEntity.Data' because it does not have the matching return type of 'IEntityContent'
Why is this? As it stands, I am forced to create a bunch of near-identical strongly-typed implementations of IThingService - which is a shedload of duplication - just to specify different type arguments which, as far as I can see, should be generic.
Is this somehow related to a lack of covariance in BaseEntity? How can I make ThingService<T> work?
Why not simply have:
public interface IEntity<T>
{
T Data { get; set; }
}
For an implementation of the interface has to match the interface (as you've not declared any contra/covariance), including the return types (hence your error message)

Categories

Resources