Looks like it can't be done without reflection or fully writing out
the code...Didn't think so, but it was worth asking.  Thanks anyway.

Just a little FYI here in case your curious: An extended description
of the concept follows - still a simplification, and not exactly what
I'm doing, but you'll get the idea:

I have a class representing a record of data from the database.  The
class is in a separate library that has no access to the database -
it's just used to store values only.  I retrieve a collection of these
objects from the database, and want to bind that data to a grid, but I
need additional properties added to this class that come from the
database in a reference table.  In reality, there are only a few
records in my collection, so I don't mind making a separate database
call for each object, so I was hoping to take the collection and
convert it into a collection of the new object, adding the additional
properties, then binding my grid with the collection of new objects.
So really, all I'm doing is flattening out my normalized data.

Vehicle contains OwnerId column - I'd like to get information about
the vehicle's owner, such as OwnerName and include that in a class
called VehicleEx.

class VehicleEx : Vehicle { public string OwnerName { get; set; } }

List<Vehicle> vehicles = /* Retrieve the Vehicle objects from the
database */ ;
List<VehicleEx> vehicleExes = vehicles.ConvertAll(delegate (Vehicle v)
{ return ConvertToVehicleEx(v); });
grd.DataSource = vehicleExes;

static VehicleEx ConvertToVehicleEx(Vehicle v) {
  /* Do something to turn v into a VehicleEx, at which point the
OwnerName property would be empty, but the rest of the properties
would be filled */
 vex.OwnerName = GetOwner(v.OwnerId).OwnerName;
}

I suppose another option would be to use a DataTable to bind the data,
which would make giving the record its own type unnecessary, but I'm
really not a big fan - it always seemed kind of sloppy to me.

On Mar 6, 4:09 pm, Joe Enos <[email protected]> wrote:
> I've got a scenario that I'm hoping to find a shortcut for.  I have a
> base class and a derived class - the derived class adds additional
> properties.  For example:
>
> public class Foo {
>         public int Prop1 { get; set; }
>         public int Prop2 { get; set; }
>
> }
>
> public class Bar : Foo {
>         public int Prop3 { get; set; }
>
> }
>
> Suppose I have a Foo, and want to convert it to a Bar (obviously Prop3
> would be empty) - I don't care if it's a cast or a convert.  I can
> think of several ways of doing this:
>  - create a constructor in Bar that accepts a Foo, then one-by-one
> assign the values of Prop1 and Prop2 to the new instance's Prop1 and
> Prop2.
>  - create a static method ConvertToBar(Foo foo) that does the same
> thing
>  - use reflection to retrieve the values of all properties of the Foo
> and assign to a new Bar.
>
> I can't put an explicit or implicit conversion operator in Foo,
> because Bar derives from Foo, and for some reason (which I'm sure
> makes a lot of sense to someone) the compiler won't let me do that.
> Even if I could, I'd still have to assign the properties one at a
> time.
>
> Any ideas?  I'd rather not use reflection, but that seems to be the
> only way to do this using the smallest amount of code and allowing for
> new properties to be added to Foo without a code change to Bar - there
> are a couple dozen properties in Foo, and only one or two extra
> properties in Bar, so I'm hoping there's some trick out there that
> would save me from doing this.
>
> Thanks
>
> Joe

Reply via email to