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

  • Home
  • SEARCH
  • 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 7003877
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:07:34+00:00 2026-05-27T21:07:34+00:00

I have a function which needs to go through a class’ types and identify

  • 0

I have a function which needs to go through a class’ types and identify a specific type. If I find that type, I want to cast it to the type that I know it is. But in the code below, the following case is failing:

 BaseTableObjectList<BaseTableObject> obj = pi.GetValue(item, null) as BaseTableObjectList<BaseTableObject>;

Here is the code. Basically, I am making an iterator which returns certain properties. Why can’t I cast?

foreach (PropertyInfo pi in item.GetType().GetProperties())
{
  if (pi.PropertyType.BaseType == null) continue; // skip interfaces
  if (!pi.PropertyType.BaseType.IsGenericType) continue;  // only interested in childlists

  Type[] types = pi.PropertyType.BaseType.GetGenericArguments();
  // is my generic argument derived from baseObject?
  if (typeof(BaseTableObject).IsAssignableFrom(types[0]))
  {
    // this is a child we want
    log.Info(types[0].Name);
    BaseTableObjectList<BaseTableObject> obj = pi.GetValue(item, null) as BaseTableObjectList<BaseTableObject>;
    yield return obj;
  }
}
  • 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-27T21:07:35+00:00Added an answer on May 27, 2026 at 9:07 pm

    The problem you have I think is one of covariance, which only applies to interfaces. If you had a IBaseTableObjectList<out T> where T : BaseTableObject the cast wouldn’t fail. Covariance for generics is only supported on delegates and interfaces not on base classes:

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

    In C#, variance is supported in the following scenarios:

    • Covariance in arrays (since C# 1.0)
    • Covariance and contravariance in delegates, also known as “method group variance” (since C# 2.0)
    • Variance for generic type parameters in interfaces and delegates (since C# 4.0)

    This is essentially the same problem is assigning a List<string> to an IEnumerable<object> which is one of the examples in the linked article.

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

Sidebar

Related Questions

I have an if statement that needs to look like this: UPDATE $(input#textbox).keypress(function(e){ key==e.which;
I have an application in VC++ which needs to execute a function provided to
i have a function which retrieves values from a webservice , then loops through
i have a function (below) which is used in my mysql abstraction class and
I have a class which models online campaigns i.e. people clicking through to the
I have made an each statement which iterates through each class item found. At
I have 2 computers whose time is synchronized through NTP which assures that the
I have a Decorator Control class which I want to collect all the validation
I have a C++ unmanaged class NativeDog that needs to be used from C#,
I have a function that I want to write and cannot work out how

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.