I have a class Record
that works fine:
public class Record
{
protected string table;
protected string idcolumn;
public Record(string _table, string _idcol, int _id)
{
table = _table;
idcolumn = _idcol;
Id = _id;
}
}
I also have a class Order
that is derived from Record
, that implements extra methods, only applicable on a certain type of record:
class Order : Record
{
public void Start()
{
}
}
In my application, I have an object theRecord
of type Record
that I want to cast to Order
, so that I can call the Start
method on it.
I tried to cast it:
Order r = (Order)theRecord;
but that throws an InvalidCastException
.
I am thinking that I could create a new constructor for Order
that takes a Record
, but I already have the object (which is built by fetching a record from the database).
How could I implement this correctly?
You'll have to convert into a new object, rather than cast it. To do this without writing the mapping code one property at a time, the easiest way would be to serialize the
Record
object, then deserialize it as anOrder
. You can use whatever serialization technique you like (XmlSerializer
,JavaScriptSerializer
, JSON.NET, etc.).This assumes that all the data is accessible in public properties with plain getters/setters (which you don't in your example, but you should do). If you don't want to do that, you can use reflection to loop through the fields of
Record
, getting the values from there, and using reflection to populate the fields ofOrder
.As I said in comments
To make it work you've to do something like this
Then you can cast it like this
You can use the
is
andas
operators to make sure something is of a certain type before actually trying to cast it, like this:The
is
will check to see if the object is that exact type or is part of the inheritance chain.The
as
will attempt to cast the object to the type, if it cannot then it will return null instead of an exception.Note:
as
will only work on reference types, not value types; whileis
will work on both reference types and value types. So you canis
an integer, but notas
an integer, for example.If you get an
InvalidCastException
thentheRecord
is not anOrder
and you can't cast it. You could only cast it if it were created as anOrder
or a subclass ofOrder
.My guess is that whatever fetches the data from the database creates a
Record
when it could create anOrder
(and return it as aRecord
). Something like: