dotnet / dotnet/samples

Bug in framework/wcf/Basic/Contract/Data/NetDCSasDCSwithDCR/CS/DCSwithDCR/DCSwithDCR.cs

Open
#6,467 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
3.7k
Forks
5.1k
Avg merge
9h 11m
Merged PRs (30d)
1

Description

**Local variable dictionary or field dictionary?**

In the `MyDataContractResolver` class in https://github.com/dotnet/samples/blob/main/framework/wcf/Basic/Contract/Data/NetDCSasDCSwithDCR/CS/DCSwithDCR/DCSwithDCR.cs, there is a field `dictionary` and there is also a local variable `dictionary` in the method `TryResolveType()`. So the field `dictionary` is never used. It seems to me that the original intent was to use the field `dictionary` and the local variable `dictionary` shouldn't have been there.

```csharp
// DataContractResolver to be used by the DataContractSerializer
class MyDataContractResolver : DataContractResolver
{
private XmlDictionary dictionary = new XmlDictionary(); <-- Here **********

...

// Used at serialization
// Maps any Type to a new xsi:type representation
public override bool TryResolveType(Type type, Type declaredType, DataContractResolver knownTypeResolver, out XmlDictionaryString typeName, out XmlDictionaryString typeNamespace)
{
if (!knownTypeResolver.TryResolveType(type, declaredType, null, out typeName, out typeNamespace))
{
XmlDictionary dictionary = new XmlDictionary(); <-- And here **********
typeName = dictionary.Add(type.FullName);
typeNamespace = dictionary.Add(type.Assembly.FullName);
}
return true;
}
}
```

Note that the same code is also on this page - https://learn.microsoft.com/en-us/dotnet/framework/wcf/samples/datacontractserializer-datacontractresolver-netdatacontractserializer.

**Target framework**

Check the .NET target framework(s) being used, and include the version number(s).

- [ ] .NET Core
- [x] .NET Framework
- [ ] .NET Standard

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.