These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: problem of Dynamic Proxy and reflection
PostPosted: Thu Apr 03, 2008 4:09 am 
Newbie

Joined: Tue Mar 25, 2008 2:44 am
Posts: 3
my progrom need do reflection on some persisent object
like this

persisent .GetType()

now I m using NHibernate v1.20

form the following post I know that it may cause some problem when using lazy loading and reflection at the same time

and it really cause some problem when I use reflection.
e.g AmbiguousMatchException, wrong type return ...etc

http://forum.hibernate.org/viewtopic.ph ... reflection


Finally, I have rewrite the GetType method @ my persisent obj

public virtual new Type GetType()
{
return base.GetType();
}

then the problem seems solve

However, I'm not very sure that it will fix the problem, since I can't seen any connnetion between above coding and the dynamic proxy

do any one know why above work?


Top
 Profile  
 
 Post subject: problem of Dynamic Proxy and reflection
PostPosted: Thu Apr 03, 2008 7:17 am 
Senior
Senior

Joined: Thu Jun 21, 2007 8:03 am
Posts: 127
Location: UK
Hi prowlclark,

The reason this works is that Dynamic Proxy has created a sub-class of your real class. So the base class of ths DP created class is your real class.

http://forum.hibernate.org/viewtopic.php?t=982573

Regards,
Richard


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 04, 2008 11:04 pm 
Newbie

Joined: Tue Mar 25, 2008 2:44 am
Posts: 3
thanks FlukeFan,

public virtual new Type GetType()
{
return base.GetType();
}

this coding seems to do nothing, and no any value added job here.
why this will trigger the proxy to create real class when do the following call ?

presistenat.GetType();...

when the proxy will decede to create a real class, why overide gettype will trigger it to create a real class ?

thank


Top
 Profile  
 
 Post subject: problem of Dynamic Proxy and reflection
PostPosted: Sat Apr 05, 2008 10:30 am 
Senior
Senior

Joined: Thu Jun 21, 2007 8:03 am
Posts: 127
Location: UK
Accessing any method/property (except the identifier property) will cause the proxy to inflate the real object. This is correct behaviour.

If you need to get the type of the object, but need to handle proxies too, then you might be better using the GetClass() utility method in NHibernate.Proxy:
Code:
using NHibernate.Proxy;
...
    Type myObjectType = NHibernateProxyHelper.GetClass(myObject);


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.