map custom object properties back to original object

67 Views Asked by At

Is there a way to associate one custom objects properties to another custom objects properties so that there is an easy means of casting?

For example:

I have an object called degree which has two columns: TypeID, TypeName I have another object called typelist which also has two columns: _id, _typename

I would like to be able to perform the following: degree _dg = (degree)typelistitem without having to explicitly say _dg.TypeID = typelistitem._id

1

There are 1 best solutions below

1
On BEST ANSWER

There is nothing built-in in the C# language.

Of course, you could write a helper method, provide it with a map from class X properties to class Y properties, but what would be the point? Compare:

// fake syntax, this is not really possible
y = makeCopy<Y>(x, {_id -> TypeId,
                    _typename -> TypeName});

with

y = new Y();
y._id = x.TypeId;
y._typename = x.TypeName;

Is the former really more readable or more concise? You need to write down which property of X gets mapped to which property of Y anyway, and the latter example only adds 8 chars of overhead to each line (y.? = x.?;), so I really don't think that a more general solution is worth the effort.