Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6116333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:11:41+00:00 2026-05-23T15:11:41+00:00

Given the class below: public class ConcreteEmployeeRoleCreator<T, TConcrete, TSpec> where TConcrete : class, T,

  • 0

Given the class below:

public class ConcreteEmployeeRoleCreator<T, TConcrete, TSpec> 
    where TConcrete : class, T, new()
    where T : EmployeeRole
    where TSpec : EmployeeRoleSpecification
{
    public ConcreteEmployeeRoleCreator(TSpec spec) { Specification = spec; }

    public TSpec Specification { get; private set; }

    public T Create() { return new TConcrete(); }

}

I would like to have a dictionary of ‘creators’, but I haven’t been able to work out how to do it yet. If I try and define the dictionary using the lowest common denominator types, the compiler is not allowing an instance to be added

[Test]
public void Creator_CanCreateFromSpec() {
    var creators = new Dictionary<string, ConcreteEmployeeRoleCreator<EmployeeRole, EmployeeRole, EmployeeRoleSpecification>>();
    var spec = new SalesmanRoleSpecification();
    var creator = new ConcreteEmployeeRoleCreator<EmployeeRole, Salesman, SalesmanRoleSpecification>(spec);
    creators.Add("salesman", creator); <==== ** compile error **
}

Salesman is an EmployeeRole, and SalesmanRoleSpecification is an EmployeeRoleSpecification (or I wouldn’t be able to define the creator above without a compiler error as well).

SO I guess it is the way I am declaring the dictionary? What am I doing wrong?

Cheers,
Berryl

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T15:11:42+00:00Added an answer on May 23, 2026 at 3:11 pm

    Generic covariance has to be specified at the declaration of the covariant type – and it can only be specified for interfaces and delegates.

    So, given that you’re using a class, a

    ConcreteEmployeeRoleCreator<EmployeeRole, Salesman, SalesmanRoleSpecification>
    

    will never be a

    ConcreteEmployeeRoleCreator<EmployeeRole, EmployeeRole, EmployeeRoleSpecification>
    

    You’ll need to look for an alternative approach. To be honest, by the time you get to three type parameters and want two of them to be covariant, you’ve got a pretty hard-to-understand design to start with, I’m afraid 🙁

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data structure which is as given below: class File { public
I am new to Mockito. Given the class below, how can I use Mockito
I have a class with a set of properties As given below. class ContactInfo
Given the class below, public class ClassOne { public static void main(String... args) {
Given a class like below: public class LoginInfo { public int UserId; public string
Suppose we have the two classes given below: public class baseclass implements interface {}
Given a type declared as shown below public class EqualityProbe<T> { public EqualityProbe( Func<T>
Given the code below: class Sample { public static void Run() { int i
Given the classes below: public class Address : Place { public virtual string Street
I wrote a Multithreaded program in Java given below :- public class Client {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.