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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:09:05+00:00 2026-05-24T04:09:05+00:00

Here are my classes definitions: public abstract class AbstractEntity : … public partial class

  • 0

Here are my classes definitions:

public abstract class AbstractEntity : ...
public partial class AbstractContactEntity : AbstractEntity, ...
public sealed class EntityCollectionProxy<T> : IList<T>, System.Collections.IList 
where T : AbstractEntity

Now I get an object from a delegate and I want to cast it, and it doesn’t work as I expect it to.

var obj = resolver.DynamicInvoke (this.entity);
var col = obj as EntityCollectionProxy<AbstractEntity>;

obj is of type EntityCollectionProxy<AbstractContactEntity>.

But col is null.
If I try the regular casting (var col = (Entity...) obj) I get an exception.

I would expect that it work since the types are coherent.
What do I miss?

  • 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-24T04:09:06+00:00Added an answer on May 24, 2026 at 4:09 am

    They are not the same types. It is the same as with List<string> and List<int>: They also can’t be casted to one another. That AbstractContactEntity is a AbstractEntity doesn’t change this.
    Extracting an interface from EntityCollectionProxy<T> and making it covariant doesn’t work either, because you want to implement IList<T> which means you have input paramaters and return values of type T which prevents covariance.

    The only possible solution is the following:

    var tmp = (EntityCollectionProxy<AbstractContactEntity>)obj;
    var col = tmp.Select(x => (AbstractEntity)x);
    

    col will be of type IEnumerable<AbstractEntity>. If you want to have a EntityCollectionProxy<AbstractEntity>, you need to create a new one:

    var result = new EntityCollectionProxy<AbstractEntity>(col);
    

    This assumes that your EntityCollectionProxy<T> class has a constructor that accepts an IEnumerable<T>.
    But beware, this will be a NEW instance and not the same as returned by resolver.DynamikInvoke.

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

Sidebar

Related Questions

Here are the domain model classes: public abstract class BaseClass { ... } public
Here is my problem in C# : I have the following classes: public class
Here is the question's plot: suppose I have some abstract classes for objects, let's
I have two classes: class Object { public: Object(); virtual void update(); virtual void
What I'm talking about here are nested classes. Essentially, I have two classes that
Here's what MSDN has to say under When to Use Static Classes : static
I have a few classes which do nothing except in their constructors/destructors. Here's an
When I write class templates, and need to fully-specialize members of those classes, Doxygen
I've got a class definition similar to the following: class UUID { public: //
I'm using the Code Contracts classes in the System.Diagnostics.Contracts namespace to define some contracts

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.