views:

80

answers:

4

Hi

I need to have a wrapper class that exposes some properties of my entity class called ProfileEntity.

I tried doing it by deriving from this entity and then creating properties that return specific entity properties, but it says I cannot cast from ProfileEntity to ProfileEntityWrapper.

When I try to put the return values of a method that returns a 'ProfileEntity' into the wrapper I get the above error.

How do I create such a wrapper class that is castable?

Example

class ProfileEntityWrapper : ProfileEntity
{
    public string Name
    {
        get
        {
            return this.ProfileEntityName;
        }
    }
}

public class Someclass
{
  public ProfileEntity SomeMethod()
  {
     return ProfileEntity; // example of method returning this object
  }
}

public class SomeOtherlClass
{
   SomeClass sc = new SomeClass();

  public void DoSomething()
  {
    ProfileEntityWrapper ew = (ProfileEntityWrapper)sc.SomeMethod(); // Cannot do this cast!!!
  }
}
+1  A: 

You cannot cast an object of ProfileEntity to ProfileEntityWrapper.

var entity = new ProfileEntity(); // this object is only of type ProfileEntity
var wrapper = new ProfileEntityWrapper(); // this object can be used as both ProfileEntityWrapper and ProfileEntity

You probably want to return a ProfileEntityWrapper in SomeMethod():

public class Someclass
{
    public ProfileEntity SomeMethod()
    {
         return new ProfileEntityWrapper(); // it's legal to return a ProfileEntity
    }
}
bjornhol
But I want it the other way, is there some solution to this?
Tony
In my mind a wrapper should not inherit the wrapped type. Try to have the wrapper receive a ProfileEntity through the constructor and remember it in a member variable, private ProfileEntity _innerEntity; In the Name property's get you go return _innerEntity.ProfileEntityName;
bjornhol
+1  A: 

No, that is not possible.

To accomplish this problem you can maybe try this one:

public class ProfileEntity
{
    public string ProfileEntityName { get; set; }
}

public class ProfileEntityWrapper
{
    public ProfileEntityWrapper(ProfileEntity entity)
    {
        Entity = entity;
    }

    public ProfileEntity Entity { get; private set; }

    public string Name
    {
        get
        {
            return Entity.ProfileEntityName;
        }
    }
}

public class SomeClass
{
    public ProfileEntity SomeMethod()
    {
        // example of method returning this object
        ProfileEntity temp = new ProfileEntity();
        return temp;
    }
}

public class SomeOtherClass
{
    SomeClass sc = new SomeClass();

    public void DoSomething()
    {
        //Create a new Wrapper for an existing Entity
        ProfileEntityWrapper ew = new ProfileEntityWrapper(sc.SomeMethod());
    }
}
Oliver
A: 

If you are allowed to edit the ProfileEntity class, or if the ProfileEntity class is a generated partial class, you could add an interface instead of using a wrapper. You wouldn't need to do any casting with an interface either. Example:

public interface IProfile
{
    string Name { get; }
}

public partial class ProfileEntity : IProfile
{
    public string Name
    {
        get
        {
            return this.ProfileEntityName;
        }
    }
}

public class SomeClass
{
    public ProfileEntity SomeMethod()
    {
        return ProfileEntity;
    }
}

public class SomeOtherClass
{
    SomeClass sc = new SomeClass();

    public void DoSomething()
    {
        IProfile ew = sc.SomeMethod();
    }
}

The IProfile instance will only provide access to the Name property.

300 baud
A: 

This's no correct code from polymorphism aspect. If we will take the famous polymorphism example when there're base Shape class and Circle, Polygon and Rectangle classes that extend the Shape class, your code will try to cast some shape into circle and as you understand this's invalid casting operation. So to make this code work you must be sure that SomeClass.SomeMethod() will return instance of ProfileEntityWrapper or perform type check before the casting, like this:

ProfileEntity temp = sc.SomeMethod();
if(temp is ProfileEntityWrapper)
     ProfileEntityWrapper ew = (ProfileEntityWrapper) temp; 
Klement