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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T04:22:18+00:00 2026-05-21T04:22:18+00:00

I have a set of classes which all take part in an inheritance chain,

  • 0

I have a set of classes which all take part in an inheritance chain, listed at the bottom of this post, the last two types and the method following them are part of an abstract library and the first class the usage of such.

The problem I am having is upon instantiation of the UserService type using Activator.CreateInstance – with an InvalidCastException stopping me dead; this did used to cast OK though, so I’m probably just overlooking something that I’ve recently altered subtly (or stupidly).

As detailed in the method creating this instance at runtime, this is the code causing the error:

return (IDataService<DataContextType, DataConnectionProviderType, DataEntityType>)Activator.CreateInstance(service); 

However, the following code succeeds:

var a = new UserService();
var b = (UserService<DataContext, ConnectionProvider, User, UserRole>)a;
var c = (DataService<DataContext, ConnectionProvider, User>)b;
var d = (IDataService<DataContext, ConnectionProvider, User>)c;

var e = (IDataService<DataContext, ConnectionProvider, User>)a;

Can anyone see what I can’t?

Not entirely sure what is going on here, but have a hunch it may be related to the typing of IDataEntity. The code for your examination, enquire if you feel you need more:

public class UserService
    : UserService<DataContext, ConnectionProvider, User, UserRole>
{
}

public class UserService<DataContextType, DataConnectionProviderType, UserType, UserRoleType>
    : DataService<DataContextType, DataConnectionProviderType, UserType>
    where DataContextType : DataContext<DataConnectionProviderType>, new()
    where DataConnectionProviderType : DataConnectionProviderBase, new()
    where UserType : class, IUser<UserRoleType>, IDataEntity
    where UserRoleType : class, IUserRole, IDataEntity
{
}

public abstract class DataService<DataContextType, DataConnectionProviderType, DataEntityType> 
    : IDataService<DataContextType, DataConnectionProviderType, DataEntityType>
        where DataContextType : DataContext<DataConnectionProviderType>, new()
        where DataConnectionProviderType : DataConnectionProviderBase, new()
        where DataEntityType : class, IDataEntity
{
}

public interface IDataService<DataContextType, DataConnectionProviderType, DataEntityType> : IDisposable
    where DataContextType : DataContext<DataConnectionProviderType>, new()
    where DataConnectionProviderType : DataConnectionProviderBase, new()
    where DataEntityType : class, IDataEntity
{
}

public static IDataService<DataContextType, DataConnectionProviderType, DataEntityType>
    LoadServiceType<DataContextType, DataConnectionProviderType, DataEntityType>(Type service)
    where DataContextType : DataContext<DataConnectionProviderType>, new()
    where DataConnectionProviderType : DataConnectionProviderBase, new()
    where DataEntityType : class, IDataEntity
{
    ...

    return (IDataService<DataContextType, DataConnectionProviderType, DataEntityType>)Activator.CreateInstance(service);             
}

Exception Details:

System.InvalidCastException: Unable to cast object of type '...UserService' to type '...IDataService'3[...DataContext,...Domain.Data.ConnectionProvider,...IDataEntity]'.

  • 1 1 Answer
  • 1 View
  • 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-21T04:22:19+00:00Added an answer on May 21, 2026 at 4:22 am

    It looks like you’re actually calling:

    LoadServiceType<DataContext, ConnectionProvider, IDataEntity>(typeof(UserService));
    

    That won’t work, because it’s trying to cast to

    IDataService<DataContext, ConnectionProvider, IDataEntity>
    

    when UserService actually implements

    IDataService<DataContext, ConnectionProvider, User>
    

    You’ll find that if you change your test code to try to cast using IDataEntity as a type argument instead of User, it’ll fail in the same way.

    This is all to do with generic variance. It’s a big topic – Eric Lippert has a whole series of blog posts on it and last year I gave a talk at NDC on it… I don’t know if the streaming site with the talk on is still available, but you can grab a torrent of all the talks here.

    To give a very simple example, you can’t cast an IList<string> to IList<object> in exactly the same way – otherwise this would compile, but have to fail at execution time:

    IList<string> strings = new List<string>();
    IList<object> objects = strings;
    objects.Add(new object());
    string firstString = strings[0]; // Eek!
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Present Scenario : I have a set of classes that all take a common
I have classes which have automatic properties only like public customerName {get; set;}. They
I've got quite a number of classes, which have got the standard set and
I have two classes set up as follows: class Point { protected: double coords[3];
I have a common base class from which all my ASMX webservice classes will
I have a set of classes that all need to be acted on in
I have a set of related C++ classes which must be wrapped and exported
I have a set of classes and interfaces which have a relatively simple hierarchy,
I have a .NET 3.5 web application which has a set of classes handling
All, I have a VB6 project with about 100 custom collection classes which I

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.