Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build">
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" />
<PropertyGroup>
Expand Down Expand Up @@ -158,4 +158,4 @@
<None Include="project.json" />
</ItemGroup>
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
</Project>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -439,47 +439,51 @@ static internal bool IsNonAttributedTypeValidForSerialization(Type type)
}
}

private static string[] s_knownSerializableTypeNames = new string[] {
"System.Collections.Queue",
"System.Collections.Stack",
"System.Globalization.CultureInfo",
"System.Version",
"System.Collections.Generic.KeyValuePair`2",
"System.Collections.Generic.Queue`1",
"System.Collections.Generic.Stack`1",
"System.Collections.ObjectModel.ReadOnlyCollection`1",
"System.Collections.ObjectModel.ReadOnlyDictionary`2",
"System.Tuple`1",
"System.Tuple`2",
"System.Tuple`3",
"System.Tuple`4",
"System.Tuple`5",
"System.Tuple`6",
"System.Tuple`7",
"System.Tuple`8",
private static readonly Dictionary<string, string[]> s_knownSerializableTypeInfos = new Dictionary<string, string[]> {
{ "System.Collections.Generic.KeyValuePair`2", Array.Empty<string>() },
{ "System.Collections.Generic.Queue`1", new [] { "_syncRoot" } },
{ "System.Collections.Generic.Stack`1", new [] {"_syncRoot" } },
{ "System.Collections.ObjectModel.ReadOnlyCollection`1", new [] {"_syncRoot" } },
{ "System.Collections.ObjectModel.ReadOnlyDictionary`2", new [] {"_syncRoot", "_keys","_values" } },
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: for consistency, in the above two lines, new [] can be new[] (without the space between new and []).

{ "System.Tuple`1", Array.Empty<string>() },
{ "System.Tuple`2", Array.Empty<string>() },
{ "System.Tuple`3", Array.Empty<string>() },
{ "System.Tuple`4", Array.Empty<string>() },
{ "System.Tuple`5", Array.Empty<string>() },
{ "System.Tuple`6", Array.Empty<string>() },
{ "System.Tuple`7", Array.Empty<string>() },
{ "System.Tuple`8", Array.Empty<string>() },
{ "System.Collections.Queue", new [] {"_syncRoot" } },
{ "System.Collections.Stack", new [] {"_syncRoot" } },
{ "System.Globalization.CultureInfo", Array.Empty<string>() },
{ "System.Version", Array.Empty<string>() },
};

private static string GetGeneralTypeName(Type type)
{
TypeInfo typeInfo = type.GetTypeInfo();
return typeInfo.IsGenericType && !typeInfo.IsGenericParameter
? typeInfo.GetGenericTypeDefinition().FullName
: type.FullName;
}

internal static bool IsKnownSerializableType(Type type)
{
// Applies to known types that DCS understands how to serialize/deserialize
//
string typeFullName = GetGeneralTypeName(type);

// Ajdust for generic type
if (type.GetTypeInfo().IsGenericType && !type.GetTypeInfo().IsGenericTypeDefinition)
{
type = type.GetGenericTypeDefinition();
}
return s_knownSerializableTypeInfos.ContainsKey(typeFullName)
|| Globals.TypeOfException.IsAssignableFrom(type);
}

// Check for known types
if (Enumerable.Contains(s_knownSerializableTypeNames, type.FullName))
{
return true;
}
//Enable ClassDataContract to give support to Exceptions.
if (Globals.TypeOfException.IsAssignableFrom(type))
return true;
internal static bool IsNonSerializedMember(Type type, string memberName)
{
string typeFullName = GetGeneralTypeName(type);

return false;
string[] members;
return s_knownSerializableTypeInfos.TryGetValue(typeFullName, out members)
&& members.Contains(memberName);
}

private XmlDictionaryString[] CreateChildElementNamespaces()
Expand Down Expand Up @@ -1073,8 +1077,7 @@ private void ImportDataMembers()

private static bool CanSerializeMember(FieldInfo field)
{
return field != null &&
field.FieldType != Globals.TypeOfObject; // Don't really know how to serialize plain System.Object instance;
return field != null && !ClassDataContract.IsNonSerializedMember(field.DeclaringType, field.Name);
}

private bool SetIfGetOnlyCollection(DataMember memberContract)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1596,6 +1596,20 @@ public static void DCJS_ReadOnlyCollection()
Assert.StrictEqual(value[1], deserializedValue[1]);
}

[Fact]
public static void DCJS_ReadOnlyDictionary()
{
var dict = new Dictionary<string, int>();
dict["Foo"] = 1;
dict["Bar"] = 2;
ReadOnlyDictionary<string, int> value = new ReadOnlyDictionary<string, int>(dict);
var deserializedValue = SerializeAndDeserialize(value, @"{""_dictionary"":[{""Key"":""Foo"",""Value"":1},{""Key"":""Bar"",""Value"":2}]}");

Assert.StrictEqual(value.Count, deserializedValue.Count);
Assert.StrictEqual(value["Foo"], deserializedValue["Foo"]);
Assert.StrictEqual(value["Bar"], deserializedValue["Bar"]);
}

[Fact]
public static void DCJS_KeyValuePair()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1740,6 +1740,20 @@ public static void DCS_ReadOnlyCollection()
Assert.StrictEqual(value[1], deserializedValue[1]);
}

[Fact]
public static void DCS_ReadOnlyDictionary()
{
var dict = new Dictionary<string, int>();
dict["Foo"] = 1;
dict["Bar"] = 2;
ReadOnlyDictionary<string, int> value = new ReadOnlyDictionary<string, int>(dict);
var deserializedValue = SerializeAndDeserialize(value, @"<ReadOnlyDictionaryOfstringint xmlns=""http://schemas.datacontract.org/2004/07/System.Collections.ObjectModel"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""><_dictionary xmlns:a=""http://schemas.microsoft.com/2003/10/Serialization/Arrays""><a:KeyValueOfstringint><a:Key>Foo</a:Key><a:Value>1</a:Value></a:KeyValueOfstringint><a:KeyValueOfstringint><a:Key>Bar</a:Key><a:Value>2</a:Value></a:KeyValueOfstringint></_dictionary></ReadOnlyDictionaryOfstringint>");

Assert.StrictEqual(value.Count, deserializedValue.Count);
Assert.StrictEqual(value["Foo"], deserializedValue["Foo"]);
Assert.StrictEqual(value["Bar"], deserializedValue["Bar"]);
}

[Fact]
public static void DCS_KeyValuePair()
{
Expand Down