On Thursday, August 11, 2016 at 8:05:04 AM UTC-4, Oskar Berggren wrote: > > > > 2016-08-11 12:59 GMT+01:00 Oskar Berggren <oskar.b...@gmail.com > <javascript:>>: > >> >> >> 2016-08-11 11:47 GMT+01:00 mwpowellhtx <mwpow...@gmail.com <javascript:>> >> : >> >>> >>> >>> On Wednesday, August 10, 2016 at 1:20:15 PM UTC-4, Oskar Berggren wrote: >>>> >>>> You can put something like this in relevant base class: >>>> >>>> public TCastTarget As<TCastTarget>() >>>> { >>>> return (TCastTarget)this; >>>> } >>>> >>> >>> Do you understand what's going on with Proxying? You don't see it in >>> your code, but what you end up with is something like this: >>> >>> class JuiceProxy : Juice { ... } >>> class AppleJuiceProxy : AppleJuice { ... } >>> class OrangeJuiceProxy : OrangeJuice { ... } >>> >>> Something like that, if memory serves, but I haven't verified the actual >>> inheritance tree. >>> >>> And since Cup wants a Juice, JuiceProxy fits the bill, but there is no >>> reasonable way for JuiceProxy to be side-cast into the expected AppleJuice, >>> much less AppleJuiceProxy, without losing necessary hierarchical details. >>> >> >> >> If you have >> AppleJuice : Juice >> >> you will get >> JuiceProxy : Juice >> >> but the important thing is that if you call a method on Juice: >> myJuice.As<AppleJuice>() >> >> then if myJuice is a JuiceProxy, it will override the As<>() method, >> trigger any required initialization, AND THEN FORWARD THE CALL TO THE REAL >> OBJECT. And the real object will be an instance of AppleJuice. So when the >> body of the As() method runs, the this-pointer will be the real, derived, >> object, that you can cast. >> >> >> > > For comparison, consider this: > > class Juice { public abstract string GetColor(); } > class AppleJuice { public override string GetColor() { return > "brown-semi-transparent"; } } > class OrangeJuice { public override string GetColor() { return "yellow"; > } } > > > If you now do: > juiceProxy.GetColor(); > > clearly you would expect it to return the correct color as per above? And > it will. Thus proving that there is a "hidden" instance of the correct > type. The As() method I showed exploits this by simply delegating the > casting to the object itself (the real object, not the proxy). >
That doesn't prove a thing. All that shows is that the proxy override the method. Because an AppleJuiceProxy is still an AppleJuice; but a JuiceProxy is not necessarily an AppleJuiceProxy. In fact, it is not, by the .NET type system. I appreciate all the feedback. As I peel back the layers on this thing, clearly it's not that cut and dry and I will need to consider how best to approach the issue. /Oskar > > > > > > > >> >> Then you don't have to turn off proxying. >>> >> >>> /Oskar >>> >>> >>> >>> 2016-08-10 18:02 GMT+01:00 Michael Powell <mwpow...@gmail.com>: >>> >>>> On Wed, Aug 10, 2016 at 11:55 AM, Michael Powell <mwpow...@gmail.com> >>>> wrote: >>>> > Hello, >>>> > >>>> > I have a use case where I'd like to cast an abstract base class to a >>>> > child class in order to verify one of the child class properties. >>>> >>>> In my joining table mapping, in this case the reference from Cup to >>>> Juice, I do something like this, which seems to work, but will want to >>>> test it heavily. >>>> >>>> References(x => x.Juice, "JuiceId") >>>> .LazyLoad(Laziness.NoProxy) >>>> .Not.Nullable(); >>>> >>>> And this seems to do the trick; at least there is literally no proxy >>>> involved and I can do the casting that I want to do. I might lose some >>>> proxiness about its usage, though, so need to test it more thoroughly. >>>> >>>> > Consider, an albeit somewhat contrived example: >>>> > >>>> > abstract class Juice { } >>>> > class AppleJuice : Juice { public virtual double Acidity { get; set; >>>> } } >>>> > class OrangeJuice : Juice { public virtual double Tanginess { get; >>>> set; } } >>>> > >>>> > class Cup { public virtual Juice Juice { get; set; } } >>>> > >>>> > I would like to test the Juice in the Cup in this manner: >>>> > >>>> > Cup cup = ...; >>>> > if (((ApplyJuice) cup.Juice).Acidity > 1.5) >>>> > { >>>> > // Respond to acidity... >>>> > } >>>> > >>>> > However, what I am finding is that the cast doesn't work "properly", >>>> > due to the proxies involved, I get InvalidCastException: >>>> > >>>> > "Unable to cast object of type 'JuiceProxy' to type 'AppleJuice'" >>>> > >>>> > Which I suppose is not surprising, but I wondered with the support for >>>> > class hierarchies, is there a recommended workaround? Do I need to do >>>> > some other form(s) of casting, or have some event handlers connecting >>>> > with ISession bits? >>>> > >>>> > Thanks! >>>> > >>>> > Regards, >>>> > >>>> > Michael Powell >>>> >>>> -- >>>> You received this message because you are subscribed to the Google >>>> Groups "Fluent NHibernate" group. >>>> To unsubscribe from this group and stop receiving emails from it, send >>>> an email to fluent-nhibern...@googlegroups.com. >>>> To post to this group, send email to fluent-n...@googlegroups.com. >>>> Visit this group at https://groups.google.com/group/fluent-nhibernate. >>>> For more options, visit https://groups.google.com/d/optout. >>>> >>> >>> -- >> You received this message because you are subscribed to the Google Groups >> "Fluent NHibernate" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to fluent-nhibern...@googlegroups.com <javascript:>. >> To post to this group, send email to fluent-n...@googlegroups.com >> <javascript:>. >> Visit this group at https://groups.google.com/group/fluent-nhibernate. >> For more options, visit https://groups.google.com/d/optout. >> > >> > -- You received this message because you are subscribed to the Google Groups "Fluent NHibernate" group. To unsubscribe from this group and stop receiving emails from it, send an email to fluent-nhibernate+unsubscr...@googlegroups.com. To post to this group, send email to fluent-nhibernate@googlegroups.com. Visit this group at https://groups.google.com/group/fluent-nhibernate. For more options, visit https://groups.google.com/d/optout.