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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:49:02+00:00 2026-05-20T11:49:02+00:00

here is my problem: I am modifying a framework developed in my company, and

  • 0

here is my problem:
I am modifying a framework developed in my company, and we have a function that returns any changes in a class’ properties.
This is the structure in the framework:

namespace Fwk.Business.Entities

    public class BusinessEntity : IIdentifiable, ICloneableExt
    {
       ...
    }

    public class EntityList<BE> where BE : BusinessEntity, new()
    {
       ...
    }
}

and this is a tipical use:

namespace Common.Business.Entities
{

    public class ImportFileEntity : Fwk.Business.Entities.BusinessEntity
    {
       ...
    }

    public class ImportFileList : Fwk.Business.Entities.EntityList<ImportFileEntity>
    {
    }

}

Fwk.Business.Entities.BusinessEntity has a function named GetChanges() that using reflection iterates through all properties and checks if they changed their value (using a copy of the original BusinessEntity). It even checks if the property is itself an instance of Fwk.Business.Entities.BusinessEntity and if so recursively calls the GetChanges method. But my problem is when I have a property that is an instance of Fwk.Business.Entities.EntityList. I want to call each element’s GetChanges method, but I can’t seem to identify these EntityList<> properties.
I’ve tried with

pi.GetValue(this, null).GetType().IsSubclassOf(typeof(EntityList<BusinessEntity>))

where pi is a PropertyInfo element that identifies the property I’m checking, but this returns false. I’ve also tried many other Type functions such as IsInstanceOfType and IsAssignableFrom, always getting a false where I need a true.
The weird thing is that if I check for a specific BusinessEntity type it does work:

pi.GetValue(this, null).GetType().IsSubclassOf(typeof(EntityList<ImportFileEntity>))

but of course this is not acceptable since I can have a list of ANY BusinessEntity.

Can anyone help me solve this?

Than’s in advance for all replys
Alex.

UPDATE:
SLaks gave me a good answer and I coded this:

bool isEntityList = false;
Type thisType = (pi.GetValue(this, null) ?? new object()).GetType();
while (thisType != typeof(object) && !isEntityList)
    if (thisType.IsGenericType && thisType.GetGenericTypeDefinition() == typeof(EntityList<>))
        isEntityList = true;
    else
        thisType = thisType.BaseType;

// If property is a subclass of EntityList, invoke GetChanges method.
if (isEntityList)
{
    EntityList<BusinessEntity> elList = (EntityList<BusinessEntity>)pi.GetValue(this, null);
    foreach (BusinessEntity beEntity in elList)
        returnValue += beEntity.GetChanges(messageFormat, stopAtFirstDifference);
}

But I’m getting a cast exception when doing

EntityList<BusinessEntity> elList = (EntityList<BusinessEntity>)pi.GetValue(this, null);

Ah! I think my problem is C# 3.5 does not accept covariance (something new to me, and existent in 4.0). So I had to expose a property with the list as BusinessEntities

At EntityList where BE : BusinessEntity, new()

public virtual List<BusinessEntity> ItemsAsBE
{
    get
    {
        List<BusinessEntity> returnValue = new List<BusinessEntity>(this.Items.Count);
        foreach (BusinessEntity item in this.Items)
            returnValue.Add(item);
        return returnValue;
    }
}

and at BusinessEntity

// If property is a subclass of EntityList, invoke GetChanges method.
if (isEntityList)
{
    foreach (BusinessEntity beEntity in thisType.GetProperty("ItemsAsBE").GetValue(pi.GetValue(this, null), null) as List<BusinessEntity>)
        returnValue += beEntity.GetChanges(messageFormat, stopAtFirstDifference);
}

Thanks everyone! Hope this help someone in the future!

  • 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-20T11:49:03+00:00Added an answer on May 20, 2026 at 11:49 am

    You need to recursively loop through the Type‘s base types, and look for a Type which IsGenericType and for which GetGenericTypeDefinition() == typeof(EntityList<>)

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

Sidebar

Related Questions

I'm having a strange problem here... I have an ASP.NET 3.5 application that has
I've got a problem here with an MSI deployment that I'm working on (using
I'm having a bit of a problem here. We have 2 urls let me
This is in reference to the question previously asked The problem here is, each
Here's a problem I ran into recently. I have attributes strings of the form
I'm pretty sure this is not possible in Zend Framework (I have searched the
My problem here is that I would like to pass an object to a
Here's the problem: When a script starts modifying the database and something goes wrong,
I have an application that I am working on modifying and I am having
This should be quite a simple problem, I've come across it enough that there

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.