Unable to cast Base class (data contract) to derived class
You cannot cast this way!
If you do new
you create a new memory object of a certain size. In your case new SearchCriteria()
creates a new memory object with enough size to hold one string, nothing more, nothing less.
In your last line you do searchCriteria as CitySearchCriteria
trying to cast the object in searchCriteria
to a larger type CitySearchCriteria
. But it cannot be done. You are trying to 'convert' a memory object which holds 1 string into a memory object that can hold 2 strings. But casting does not convert an new memory object. What would be the value of the new string? It simply looks under water to check if your reference searchCriteria
already contains an object of type CitySearchCriteria
. In your case: it does not (the object is of type SearchCriteria
) and returns null
.
So... the next example DOES work (because CitySearchCriteria has already been created). This is also your solution:
SearchCriteria searchCriteria = new CitySearchCriteria();
CitySearchCriteria citySearchCriteria = searchCriteria as CitySearchCriteria;
And this does not work (because CitySearchCriteria has NOT already been created). This is your situation:
SearchCriteria searchCriteria = new SearchCriteria();
CitySearchCriteria citySearchCriteria = searchCriteria as CitySearchCriteria;
It is the same thing as the next example.
This does work (because SearchCriteria has already been created):
object o = new SearchCriteria();
SearchCriteria searchCriteria = o as SearchCriteria;
And this does not (because SearchCriteria has NOT already been created)::
object o = new object();
SearchCriteria searchCriteria = o as SearchCriteria;
For the record: I would always use a direct cast, not a cast using as
, unless you want to explicitly test if an object is of that type.
Everybody has already (and correctly) told you that you simply can't cast from Base to Derived, but it seems to me that you still don't get the reason why this line works in another chunk of your code:
CitySearchCriteria citySearchCriteria = (CitySearchCriteria)Model.SearchCriteria;
I think that you are a little bit confused about what the "Type" of an instance is. You didn't post the definition of Model, but I think that you have something like this:
public SearchCriteria SearchCriteria;
This doesn't mean that SearchCriteria always contains instances of SearchCriteria, but only that it contains instances of types that can be cast to SearchCriteria. In your case it can contain instances of SearchCriteria or of CitySearchCriteria. I suppose that somewhere in your code you will find something like:
Model.SearchCriteria = new CitySearchCriteria();
and this is what allows yor cast to be executed correctly. You can see that the instance is indeed a CitySearchCriteria (and not simply an instance of SearchCriteria) executing this code just before the cast:
MessageBox.Show(Model.SearchCriteria.GetType().FullName);
To understand better you could try to modify the value in SearchCriteria just before your working cast as shown below only to find out that the cast won't work anymore:
Model.SearchCriteria = new SearchCriteria();
MessageBox.Show(Model.SearchCriteria.GetType().FullName);
CitySearchCriteria citySearchCriteria = (CitySearchCriteria)Model.SearchCriteria;
You could create CitySearchCriteria, and cast it to SearchCriteria. This way you can only see CountryId. Later, you can cast it back to CitySearchCriteria, and see CountryId and CityId.
This has nothing to do with DataContract. Solution in your case would be to create CitySearchCriteria and cast it to SearchCriteria (if you need it).