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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:21:42+00:00 2026-05-17T02:21:42+00:00

I have a base class DomainObject for all my business objects I am using

  • 0

I have a base class DomainObject for all my business objects I am using with NHibernate. It contains the Id property.

public abstract class DomainObject
{
    public virtual int Id { get; private set; }
}

I would like to write an IEqualityComparer to compare my domain objects. If two objects have the same Id and are the same kind of object they should be equal. However when I use GetType() to get the type of the object, it will return the NHibernate proxy type. So this code:

bool IEqualityComparer.Equals(object x, object y)
{
    // null checking code skipped here
    if(x is DomainObject && y is DomainObject)
    {
            return ((DomainObject) x).Id == ((DomainObject) y).Id
                    && x.GetType() == y.GetType();
    }
    return x.Equals(y);
}

Doesn’t work correctly, because the type of x is Asset but the type of y is AssetProxy21879bba3e9e47edbbdc2a546445c657.

So, how do I get the entity type on an object that may be a NHibernate proxy object? i.e. in the example above Asset instead of AssetProxy21879bba3e9e47edbbdc2a546445c657?

  • 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-17T02:21:43+00:00Added an answer on May 17, 2026 at 2:21 am

    You can get the real type of a proxy with:

    NHibernateUtil.GetClass(x);
    

    or you can add a method to DomainObject like:

    public virtual Type GetTypeUnproxied()
    {
        return GetType();
    }
    

    Which is really slick and doesn’t depend directly on NHibernate.

    Alternatively, one can approach the problem by saying you need to get the true object, rather than the proxy, which, if the session is handy, can be done with:

    session.PersistenceContext.Unproxy(x);
    

    As mentioned in another answer, if you’re trying to implement equals it would be a good idea to check out the Sharp architecture implementation of Equals.

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

Sidebar

Related Questions

I have a base class: class motorcycle { public: virtual int speed() { return
I have a base class ReportElement which has type property: public abstract class ReportElement
I have a base class, defined as below (I'm also using DevExpress components): public
I have a base class ActivityA that has some dialogs: public static final int
I have a base class called Entity: public class Entity { public int Id
I have this base class: public abstract class Parent { public void DoSomething() {
I have a base class like this: public class BaseResponse { public string ErrorMessage
I have a class hierarchy: abstract DomainObject { ... @Id @GeneratedValue(strategy = GenerationType.SEQUENCE, generator=SEQ)
I have base class (virtual), with some children, and an array with pointers on
I have an abstract base class along the lines of: class MyAbstractClass(object): __metaclass__ =

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.