public void TypeNames() { Assert.Equal("String", UnionEncoding.GetTypeName(typeof(string))); Assert.Equal("Int32", UnionEncoding.GetTypeName(typeof(int))); Assert.Equal("Version", UnionEncoding.GetTypeName(typeof(Version))); Assert.Equal("Guid", UnionEncoding.GetTypeName(typeof(Guid))); Assert.Equal("Union<Int32,String>", UnionEncoding.GetTypeName(typeof(Union <int, string>))); Assert.Equal("Dasher.Tests.ValueWrapper<String>", UnionEncoding.GetTypeName(typeof(ValueWrapper <string>))); Assert.Equal("Dasher.Tests.UnionEncodingTests", UnionEncoding.GetTypeName(typeof(UnionEncodingTests))); Assert.Equal("Dasher.Tests.UnionEncodingTests+NestedType", UnionEncoding.GetTypeName(typeof(NestedType))); Assert.Equal("Dasher.Tests.UnionEncodingTests+NestedType<Int32>", UnionEncoding.GetTypeName(typeof(NestedType <int>))); Assert.Equal("Dasher.Tests.Foo<Int32>+Bar", UnionEncoding.GetTypeName(typeof(Foo <int> .Bar))); Assert.Equal("Dasher.Tests.Foo<Int32>+Baz<String>", UnionEncoding.GetTypeName(typeof(Foo <int> .Baz <string>))); Assert.Equal("[String]", UnionEncoding.GetTypeName(typeof(IReadOnlyList <string>))); Assert.Equal("(Int32=>Boolean)", UnionEncoding.GetTypeName(typeof(IReadOnlyDictionary <int, bool>))); Assert.Equal("(Int32=>[Boolean])", UnionEncoding.GetTypeName(typeof(IReadOnlyDictionary <int, IReadOnlyList <bool> >))); Assert.Equal("[(Int32=>Boolean)]", UnionEncoding.GetTypeName(typeof(IReadOnlyList <IReadOnlyDictionary <int, bool> >))); // Use UnionTagAttribute Assert.Equal("A", UnionEncoding.GetTypeName(typeof(TaggedA))); Assert.Equal("B", UnionEncoding.GetTypeName(typeof(TaggedB))); }
internal UnionWriteContract(Type type, ContractCollection contractCollection) { if (!CanProcess(type)) { throw new ArgumentException($"Type {type} must be a union.", nameof(type)); } Members = Union.GetTypes(type) .Select(t => new Member(UnionEncoding.GetTypeName(t), contractCollection.GetOrAddWriteContract(t))) .OrderBy(m => m.Id, StringComparer.OrdinalIgnoreCase) .ToArray(); }