We have an abstract BaseClass (note the general argument!) Using a method called me. I am returning this.
If we use Me in specific classes, we get an object of type return. Then we must cast the result of Me to the type with which we initially work.
How can we achieve that Me returns the actual type of this? In this example, type A?
public abstract class BaseClass<TIdentifier>{ public virtual object Me{ get { return this; } } } public class A: BaseClass<long> { } public class B: BaseClass<long> { } public class controller{ public void SomeMethod(){ var a = new A(); var b = new B(); var aObject = a.Me;
Update
Since some people really, desperately (wink :-)), want to understand what I'm actually trying to do.
At NHibernate we do this:
var result = Session.Get<A>(idToLookUp);
In some cases, it happens that the result is not of type A, but of type AProxy, due to the impossibility of loading, etc. Now, if we want to distinguish the result from something else: we will get an invalid exception, because the actual type of the result is not A, but AProxy. And this type cannot be discarded. We can only use type A for another type.
A workaround for this is described here: http://sessionfactory.blogspot.be/2010/08/hacking-lazy-loaded-inheritance.html . Where the Me property is in the above examples.
So, to get the result of type A, and not of type AProxy, we must do this:
var result = (A)Session.Get<A>(idToLookUp).Me;
Please note that we must return me to type A if we want to read and find out the property of the result.
My question is: can we get rid of casting and set the Me property so that we immediately return a specific type?
Hope this is clear now.