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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T08:07:33+00:00 2026-05-28T08:07:33+00:00

When I use reflection in this case, the created type can be many generic

  • 0

When I use reflection in this case, the created type can be many generic types.

BaseStepHandler<BaseStepDataModel> activator = (BaseStepHandler<BaseStepDataModel>)Activator.CreateInstance(....);

The created instance can be all childs of BaseStepDataModel.

BaseStepHandler<OneDataModel>
OR
BaseStepHandler<TwoDataModel>

OneDataModel and TwoDataModel are extending BaseStepDataModel.

this is the exception that I get:

Unable to cast object of type ‘….GlobalOnBoardingStepOneHandler’ to type ‘….BaseStepHandler`1[….BaseStepDataModel]’.

this is the declaration if GlobalOnBoardingStepOneHandler.

public class GlobalOnBoardingStepOneHandler : BaseStepHandler<GlobalOnBoardingStepOneDataModel>{}
  • 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-28T08:07:33+00:00Added an answer on May 28, 2026 at 8:07 am

    You are getting the exception because GlobalOnBoardingStepOneHandler inherits from BaseStepHandler<GlobalOnBoardingStepOneDataModel>, not BaseStepHandler<BaseStepDataModel>. This is probably the most common mistake with .NET generics. Generics are not covariant for type parameters.

    See:

    C#: cast to generic interface with base type

    http://blogs.msdn.com/b/csharpfaq/archive/2010/02/16/covariance-and-contravariance-faq.aspx

    http://blogs.msdn.com/b/ericlippert/archive/2007/10/16/covariance-and-contravariance-in-c-part-one.aspx

    http://blogs.msdn.com/b/ericlippert/archive/2007/10/16/covariance-and-contravariance-in-c-part-two.aspx

    http://blogs.msdn.com/b/ericlippert/archive/2007/10/17/covariance-and-contravariance-in-c-part-two-array-covariance.aspx

    http://blogs.msdn.com/b/ericlippert/archive/2007/10/19/covariance-and-contravariance-in-c-part-three-member-group-conversion-variance.aspx

    etc…

    The issue is that you assume that because GlobalOnBoardingStepOneDataModel inherits from BaseStepDataModel, that GlobalOnBoardingStepOneHandler inherits from BaseStepHandler<BaseStepDataModel>. That simply isn’t the case, and so you can’t cast from one to the other.

    As an example, consider the following:

    var myListOfStrings = new List<String>();
    
    // By your logic, this should compile (it doesn't):
    var myListOfObjects = ((List<Object>)myListOfStrings);
    
    // But if it did, this would be possible:
    myListOfObjects.Add(1); // Holy cow, I just added an integer to a list of strings!  What is the world coming to?
    

    Now, this is very confusing for recovering Java programmers because this is possible in Java. In Java, you have type erasure, so that at runtime a List<String> is actually just a List<Object>, and so you can cast it to anything you like, and put anything you want into it. Because the CLR uses reified generics, not type erasure, a List<String> is actually a separate and distinct type from a List<Object> or a List<Integer>

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

Sidebar

Related Questions

How can I use reflection to create a generic List with a custom class
If I wrote this code: typeof(myType).TypeHandle Would it use reflection? How much different from:
I use this code to load a .Net assembly to PowerShell: [System.Reflection.Assembly]::Load(System.Windows.Forms, Version=2.0.0.0, Culture=neutral,
System.Reflection.Assembly.Load(System.IO.File.ReadAllBytes(path)) So this is a work around to not being able to use T4
I have an object, o, and a type, T. I'd like to use reflection
I am attempting to use reflection to determine which Properties of a Type have
I'm trying to use reflection to get a property from a class. Here is
Is it possible to use reflection to know what the code is doing, not
I need to use reflection to get the binding value in a control that
In WPF, how do I use reflection to find all classes in a project?

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.