3

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?

1
  • What is IComponent? Commented Feb 2, 2018 at 15:46

1 Answer 1

6

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; }
}
Sign up to request clarification or add additional context in comments.

1 Comment

It looks like this is the solution I was looking for. Thanks. I'll try this out and if it works I'll mark this as the accepted answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.