C#: Implement a abstract property in an interface to implement a child - c#

I couldn't find the answer in the internet so I gonna ask for your help.
I have an abstract class:
public abstract class AbstractClass
{
String Name { get; }
}
Child Class 1:
public class ApplicationDetailsModel : AbstractClass
{
// This will implement unique properties
}
And each class that implements IComponent will implement a child of "AbstactClass".
An example implementation:
public class ExampleClass : IComponent
{
public ApplicationDetailsModel DetailsModel;
}
And an interface
public interface IComponent : IPageViewModel
{
AbstractClass DetailsModel { get; }
}
This example does not compile because the interface forces to implement
AbstractClass DetailsModel = new ApplicationDetailsModel();
Is there a correct way to force an implementation of a child property of a specific abstract class in an interface?

Make your interface generic with a constraint
public interface IComponent<T> : IPageViewModel
where T : AbstractClass
{
T DetailsModel {get;}
}
By example:
public class ExampleClass : IComponent<ApplicationDetailsModel>{
{
public ApplicationDetailsModel DetailsModel { get; private set; }
}

Related

Should I create an interface for abstract class?

Is there any practial reason to create interface for abstract class? I encountered such thing:
public interface IEntity<T>
{
T Id { get; set; }
}
public abstract class BaseEntity {
}
public abstract class Entity<T> : BaseEntity, IEntity<T>
{
public virtual T Id { get; set; }
}
and i really don't understand what is the difference between that and this code, because IEntity is not a thing i whould use more than once:
public abstract class BaseEntity {
}
public abstract class Entity<T> : BaseEntity
{
public virtual T Id { get; set; }
}
Thanks!
Since the BaseEntity class does actually add any properties of methods, yes, it is completely different from the interface. The interface defines a property Id of type T, and that interface (contract) can be used in other locations in your application.
The base class as it is now, is useless IMHO.
A base class which would implement the interface for you is something that would be usable, like this:
public interface IEntity<T> {
T Id { get; set; }
}
public abstract class BaseEntity<T>: IEntity<T> {
public virtual T Id { get; set; }
}
public abstract class Entity<T> : BaseEntity<T> {
// No need to implement the Id property, we already have it inherited
}
Here is the same thing with correct answer that i wanted to know: c# Abstract Class implementing an Interface
Thanks everyone for help.

Verify that generic type implements a certain interface

I have the following interfaces:
public interface IView<TViewModel>
{
TViewModel ViewModel { get; set; }
}
public interface IViewModel : INotifyPropertyChanged
{
}
I would like to make sure that the generic TViewModel is always a class that implements interface IViewModel. I could do the following:
public interface IView
{
IViewModel ViewModel { get; set; }
}
But then I would not have access to all the properties and methods of the specific class of ViewModel.
How can I make sure that TViewModel is always a class that implements interface IViewModel?
Specify a generic type constraint using the where clause.
public interface IView<TViewModel> where TViewModel : IViewModel
{
TViewModel ViewModel { get; set; }
}

Why can't a derived class override base class' interface type abstract property with a type that extends that interface?

In the following code, compiler complains about B not implementing TestProperty of abstract class A. ITest2 is derived from ITest1 so it implements everything ITest1 has. Why is this not possible?
public interface ITest1 { }
public interface ITest2 : ITest1 { }
public abstract class A
{
public abstract ITest1 TestProperty { get; set; }
}
public class B:A
{
public override ITest2 TestProperty { get; set; }
}
This wouldn't be safe since you could do:
interface ITest3 : ITest1 { }
public class Test3 : ITest3 { }
A b = new B();
b.TestProperty = new Test3();
however Test3 does not implement ITest2 as required by B.
Make A class generic
Public abstract class A<TTest>
Where TTest : ITest1
{
Public abstract TTest TestProperty {get; set;}
}
Public class B : A<ITest2>
{
....
}

C# public only for classes of the same interface

Can I make some properties public only to same interface classes and readonly to all other classes?
You can use explicit implementation, for example:
interface IFoo {
int Value { get; set; }
}
public class Foo : IFoo {
public int Value { get; private set; }
int IFoo.Value {
get { return Value; }
set { Value = value; }
}
}
When accessed via Foo only the get will be accessible; when accessed via IFoo both getter and setter will be accessible.
Any use?
An interface is just something like a contract for classes. It doesn't change the accessibility level.
If a member of a class is public it is public to all classes that can access the class. The only restrictions you can have is the use of internal or protected. internal makes the member public to classes which are defined within the same assembly and protected makes it public to classes derived from the class.
Instead of the interface you can create an abstract base class and make the members protected:
public interface IFoo
{
int Value { get; set; }
}
public abstract class FooBase : IFoo
{
public abstract int Value { get; set; }
protected void ProtectedMethod()
{
}
}
public class Foo : FooBase
{
public int Value { get; set; }
}
However you can not define a member that is accessible by classes that implement a specific interface. There is no access modifier like public-to-IFoo-otherwise-private.

Specifying multiple interfaces for a parameter

I have an object that implements two interfaces... The interfaces are:
public interface IObject
{
string Name { get; }
string Class { get; }
IEnumerable<IObjectProperty> Properties { get; }
}
public interface ITreeNode<T>
{
T Parent { get; }
IEnumerable<T> Children { get; }
}
such that
public class ObjectNode : IObject, ITreeNode<IObject>
{
public string Class { get; private set; }
public string Name { get; private set; }
public IEnumerable<IObjectProperty> Properties { get; set; }
public IEnumerable<IObject> Children { get; private set; }
public IObject Parent { get; private set; }
}
Now i have a function which needs one of its parameters to implement both of these interfaces. How would i go about specifying that in C#?
An example would be
public TypedObject(ITreeNode<IObject> baseObject, IEnumerable<IType> types, ITreeNode<IObject>, IObject parent)
{
//Construct here
}
Or is the problem that my design is wrong and i should be implementing both those interfaces on one interface somehow
public void Foo<T>(T myParam)
where T : IObject, ITreeNode<IObject>
{
// whatever
}
In C#, interfaces can themselves inherit from one or more other interfaces. So one solution would be to define an interface, say IObjectTreeNode<T> that derives from both IObject and ITreeNode<T>.
It's probably easiest to define an interface that implements both IObject and ITreeNode.
public interface IObjectNode<T> : IObject, ITreeNode<T>
{
}
Another option, in case you don't expect the above interface would be used often, is to make the method/function in question generic.
public void Foo<T>(T objectNode) where T : IObject, ITreeNode<IObject>
public void MethodName<TParam1, TParam2>(TParam1 param1, TParam2 param2)
where TParam1 : IObject
where TParam2 : ITreeNode<IObject>

Categories

Resources