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 7029001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:28:49+00:00 2026-05-28T00:28:49+00:00

I have a generic interface, public interface ICalculator<in T> { void Calculate(T input); }

  • 0

I have a generic interface,

   public interface ICalculator<in T>
   {
      void Calculate(T input);
   }

a general calculator,

   public class GeneralCalculator<T> : ICalculator<T>
   {
      public void Calculate(T input)
      {
         bla bla
      }
   }

a specific calculator that works only on some types,

   public class SpecificAndFastCalculator<T> : ICalculator<T> where T : ISpecificInterface
   {
      public void Calculate(T input)
      {
         bla bla
      }
   }

and a decorator that decorates ICalculators:

   public class CalculatorDecorator<T> : ICalculator<T>
   {
      private readonly ICalculator<T> component;

      public CalculatorDecorator()
      {  
         component = (typeof (ISpecificInterface).IsAssignableFrom(typeof (T)))
                        ? new SpecificAndFastCalculator<T>() //Compiler Error 
                        : new GeneralCalculator<T>();
      }

      public void Calculate(T input)
      {
         component.Calculate(input); bla bla
      }
   }

The problem is, as you see, the compiler does not accept to call the constructor of SpecificAndFastCalculator because it doesn’t know whether T obeys the constraint T : ISpecificInterface even though I check it at run-time. Can the compiler be right, or does it reject the code only because it is not smart enough?

Is there any way to assign the component field to a SpecificAndFastCalculator?

As far as I can see I have these options:

  • Write a specific decorator for having SpecificAndFastCalculator
    components, which I think beats the purpose of having decorators,
  • Use reflection magic somehow (I don’t have any idea how)
  • Delete the T : ISpecificInterface constraint, document it, and hope
    for the best.

Is there any other way? For the time being I’m opting for the third option (deleting the constraint), what would you suggest?

  • 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-28T00:28:50+00:00Added an answer on May 28, 2026 at 12:28 am

    It should work if you change the constructor of CalculatorDecorator<T> like this:

    public CalculatorDecorator()
    {  
        component = (ICalculator<T>)(typeof (ISpecificInterface).IsAssignableFrom(typeof (T))
            ? typeof(SpecificAndFastCalculator<>)
                .MakeGenericType(typeof(T))
                .GetConstructor(new Type[0])
                .Invoke(null)
            : new GeneralCalculator<T>());
    }
    

    That will make use of the specialized implementation and still make sure all elsewhere created instances will use it as well.

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

Sidebar

Related Questions

Ok I have a generic interface public IConfigurationValidator<T> { void Validate(); } a class
I have a class that inherits a generic dictionary and an inteface public class
I have generic type that looks like: public class GenericClass<T, U> where T :
Suppose you have a generic interface and an implementation: public interface MyInterface<T> { void
I have a generic class that implements IList public class ListBase<T>: IList<T>, IListBase{ IEnumerator<T>
I have a generic interface public interface Consumer<E> { public void consume(E e); }
I have a C# project with a generic interface public interface IMyFoo<T> { void
Say we have an interface as such... interface ICalculator<T extends CalculableObject>{ void calculate(T obj);
I have a generic PostProcessor interface that looks like this: public interface PostProcessor<T> {
I have a generic interface: public interface IUnauthorizedRequestRespondable<out T> where T:class { T GetResponseForUnauthorizedRequest();

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.