2

I did this multiple constraint

public class BaseValidation<S, R> 
        where R : BaseRepository 
        where S : BaseService<R>, new()
    {
        public S service;

        public BaseValidation()
        {
            service = new S();
        }
    }

Here's the BaseService class

public class BaseService<T> where T : BaseRepository, new(){ }

And when I build, an error occurs like this...

'R' must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'T' in the generic type

How to properly done this? Thank you.

1
  • 1
    Does BaseService have a parameterless constructor? Commented Jul 7, 2014 at 14:44

1 Answer 1

5

You need to add the new() constraint to R as well, since T has that constraint in the definition of BaseService<T>:

public class BaseValidation<S, R> 
        where R : BaseRepository, new()
        where S : BaseService<R>, new()
{
    public S service;

    public BaseValidation()
    {
        service = new S();
    }
}

If you don't actually need that constraint in BaseService<T>, just remove it.

Sign up to request clarification or add additional context in comments.

Comments

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.