`DataContractSerializer`: Serialize child class as base class
- Dominant language
- C#
- Stars
- 1.8k
- Forks
- 576
- Avg merge
- 6d 9h
- Merged PRs (30d)
- 2
Description
### Description
I have following on my server:
```C#
[DataContract]
public class Base { }
[OperationContract]
void DoSth(Base base) { }
```
and on my client:
```C#
class Child : Base { }
```
When calling the server with `DoSth(child)` I get a serialization exception, because `Child` is not known to the WCF DataContractSerializer:
```
System.ServiceModel.CommunicationException: There was an error while trying to serialize parameter http://tempuri.org/:base. The InnerException message was 'Type 'Child' with data contract name 'Child:http://schemas.datacontract.org/2004/07/Client' is not expected. Add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.'. Please see InnerException for more details.
---> System.Runtime.Serialization.SerializationException: Type 'Child' with data contract name 'Child:http://schemas.datacontract.org/2004/07/Client' is not expected. Add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.
```
And I don't want to make it known, because the server uses only `Base`'s properties. How can I send my data as `Base object`?
I know, I could create a new `Base` object and copy all properties, but this is not really elegant.
see also https://stackoverflow.com/questions/78061526/datacontractserializer-deserialize-child-class-into-base-class
### Reproduction Steps
* Create WCF server and client.
* Add `Base` and `Child` classes as described above.
* Send instance of `Child` to server.
### Expected behavior
`Child` should be serialized as `Base`, ignoring all additional properties.
### Actual behavior
```
System.ServiceModel.CommunicationException: There was an error while trying to serialize parameter http://tempuri.org/:base. The InnerException message was 'Type 'Child' with data contract name 'Child:http://schemas.datacontract.org/2004/07/Client' is not expected. Add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.'. Please see InnerException for more details.
---> System.Runtime.Serialization.SerializationException: Type 'Child' with data contract name 'Child:http://schemas.datacontract.org/2004/07/Client' is not expected. Add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.
```
### Regression?
_No response_
### Known Workarounds
_No response_
### Configuration
.NET 8 on Windows 11, but should not be specific to that environment.
### Other information
_No response_
Contributor guide
Assessment
This issue has not been assessed yet.