views:

48

answers:

2
A: 

The obvious solution:

BaseObject baseObject = null;
if ( /* searchResult is a User */ )
{
  var user = new User() { /* Set User-specific properties */ };
  baseObject = user;
}
else if ( /* searchResult is a Group */ )
{
  var group = new Group() { Set /* Group-specific properties */ };
  baseObject = group;
}
if (baseObject != null)
{
   // Set common properties in baseObject which is a
   // reference to the underlying derived type
}
yield return baseObject;
carlsb3rg
A: 

I actually ended up creating this method in my BaseObject Class:

internal void PopulateChildObject(BaseObject childObject)
{
  childObject.Property1 = this.Property1
  childObject.Property2 = this.Property2
  ...
}

Then I created a created a constructor in each derived class that took a BaseObject parameter and called the mentioned method.

public User(BaseObject baseObject)
{
  baseObject.CopyPropertiesToChildClass(this);
}

This helped me avoid having to change each of the constructors in the child classes every time I added a property to BaseObject. I just add the new property assignments to the PopulateChildObject() method in BaseObject.

carlsb3rg