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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:28:17+00:00 2026-06-10T15:28:17+00:00

I’ve got an instance of class B , which is a specialisation of an

  • 0

I’ve got an instance of class B, which is a specialisation of an abstract class A<TInput, TOutput>. There are several variants of class B, as I’ve implemented it with a variety of input and outputs.

TInput and TOutput are constrained to specific input and output classes, let’s call them I and O.

I am instantiating B by using Activator.CreateInstance, but since it returns an object, I need to to cast it to A<I, O>. I expect this to work as I and O are base classes (in this case B<SpecialisationOfI, SpecalisationOfO>).

This is where it fails as this cast is apparently invalid.

Pseudo code:

abstract class I  { }
abstract class O { }

abstract class A<TInput, TOutput> 
  where TInput : I
  where TOutput : O
{ 
  abstract TOutput Foo(TInput bar);
}

class Input : I { }
class Output : O { }

class B : A<Input, Output> { }

A<I, O> instance = (A<I, O>)Activator.CreateInstance(typeOfB); // <-- fail
instance.Foo(input);

Is it possible to make this work? Thank you!

edit Based on the answer that I was given, I’ve solved this by restructuring the code significantly based on the covariance: I moved Foo from A to an interface:

interface IA<TResult> {
   TResult Foo(object input);
}

class A<TInput, TOutput> : IA<TOutput>
  where TInput : I
  where TOutput : O {

  public TOutput Foo(object input) {
     if (!(input is TInput)) {
       throw new ArgumentException("input");
     }

     return FooImpl(input as TInput);
  }

  protected abstract TOutput FooImpl(TInput input);
}

var instance = (IA<Output>) Activator.CreateInstance(type);
instance.Foo(input);

Thank you for sharing your insight with me!

  • 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-06-10T15:28:19+00:00Added an answer on June 10, 2026 at 3:28 pm

    This has nothing to do with how you’re creating the instance – it’s a problem with generic variance. What you’re essentially trying to do is similar to this:

    List<object> objects = new List<string>();
    

    And the reason it’s not valid is that the next line of code might (in general) be:

    objects.Add(new object());
    

    If that’s trying to add to a list which is really of type List<string>, that’s bad news.

    Now if you change from an abstract class to an interface, and if you’re using C# 4, you can use generic covariance and generic contravariance:

    interface class A<in TInput, out TOutput> 
      where TInput : I
      where TOutput : O
    { 
      abstract TOutput Foo(TInput bar);
    }
    

    That’s still not going to work for your code though, as you’re trying to use both input and output covariantly. Your B class will have a method like this:

    Output Foo(Input bar);
    

    … in other words, it needs input of type Input. But if you’ve got an A<I, O> that should be able to work for any implementation of I:

    A<I, O> x = new B();                // Invalid, as discussed, to prevent...
    O output = x.Foo(new SomeOtherI()); // ... this line
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have an autohotkey script which looks up a word in a bilingual dictionary
I have a text area in my form which accepts all possible characters from

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.