-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
210 additions
and
38 deletions.
There are no files selected for viewing
27 changes: 19 additions & 8 deletions
27
src/ExtendedXmlSerializer/ContentModel/Members/MappedAllowedMemberValues.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,29 @@ | ||
using ExtendedXmlSerializer.Core.Sources; | ||
using System.Collections.Generic; | ||
using System.Reflection; | ||
using ExtendedXmlSerializer.Core.Sources; | ||
|
||
namespace ExtendedXmlSerializer.ContentModel.Members | ||
{ | ||
sealed class MappedAllowedMemberValues | ||
: TableSource<MemberInfo, IAllowedValueSpecification>, | ||
IAllowedMemberValues | ||
sealed class MappedAllowedMemberValues : IAllowedMemberValues | ||
{ | ||
public MappedAllowedMemberValues(IDictionary<MemberInfo, IAllowedValueSpecification> store) : base(store) {} | ||
readonly Table _primary; | ||
|
||
public MappedAllowedMemberValues(IDictionary<MemberInfo, IAllowedValueSpecification> primary) | ||
: this(new Table(new TableSource<MemberInfo, IAllowedValueSpecification>(primary))) {} | ||
|
||
MappedAllowedMemberValues(Table primary) => _primary = primary; | ||
|
||
public IAllowedValueSpecification Get(MemberInfo parameter) | ||
=> _primary.Get(parameter); | ||
|
||
sealed class Table : IParameterizedSource<MemberDescriptor, IAllowedValueSpecification> | ||
{ | ||
readonly IParameterizedSource<MemberInfo, IAllowedValueSpecification> _source; | ||
|
||
public override IAllowedValueSpecification Get(MemberInfo parameter) => From(parameter); | ||
public Table(IParameterizedSource<MemberInfo, IAllowedValueSpecification> source) => _source = source; | ||
|
||
IAllowedValueSpecification From(MemberDescriptor parameter) | ||
=> base.Get(parameter.Metadata) ?? base.Get(parameter.MemberType); | ||
public IAllowedValueSpecification Get(MemberDescriptor parameter) | ||
=> _source.Get(parameter.Metadata) ?? _source.Get(parameter.MemberType); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
using ExtendedXmlSerializer.ReflectionModel; | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
|
||
namespace ExtendedXmlSerializer.Core | ||
{ | ||
class MetadataDictionary<T> : IDictionary<MemberInfo, T> where T : class | ||
{ | ||
readonly IDictionary<MemberInfo, T> _primary, _secondary; | ||
|
||
public MetadataDictionary(IDictionary<MemberInfo, T> primary) | ||
: this(primary, primary.ToDictionary(x => x.Key, x => x.Value, InheritedMemberComparer.Default.Get())) {} | ||
|
||
public MetadataDictionary(IDictionary<MemberInfo, T> primary, IDictionary<MemberInfo, T> secondary) | ||
{ | ||
_primary = primary; | ||
_secondary = secondary; | ||
} | ||
|
||
public IEnumerator<KeyValuePair<MemberInfo, T>> GetEnumerator() => _primary.GetEnumerator(); | ||
|
||
IEnumerator IEnumerable.GetEnumerator() => ((IEnumerable)_primary).GetEnumerator(); | ||
|
||
public void Add(KeyValuePair<MemberInfo, T> item) | ||
{ | ||
throw new InvalidOperationException("Not supported."); | ||
} | ||
|
||
public void Clear() | ||
{ | ||
throw new InvalidOperationException("Not supported."); | ||
} | ||
|
||
public bool Contains(KeyValuePair<MemberInfo, T> item) => _primary.Contains(item) || _secondary.Contains(item); | ||
|
||
public void CopyTo(KeyValuePair<MemberInfo, T>[] array, int arrayIndex) | ||
{ | ||
_primary.CopyTo(array, arrayIndex); | ||
} | ||
|
||
public bool Remove(KeyValuePair<MemberInfo, T> item) => throw new InvalidOperationException("Not supported."); | ||
|
||
public int Count => _primary.Count; | ||
|
||
public bool IsReadOnly => _primary.IsReadOnly; | ||
|
||
public void Add(MemberInfo key, T value) | ||
{ | ||
throw new InvalidOperationException("Not supported."); | ||
} | ||
|
||
public bool ContainsKey(MemberInfo key) => _primary.ContainsKey(key) || _secondary.ContainsKey(key); | ||
|
||
public bool Remove(MemberInfo key) => throw new InvalidOperationException("Not supported."); | ||
|
||
public bool TryGetValue(MemberInfo key, out T value) | ||
=> _primary.TryGetValue(key, out value) || _secondary.TryGetValue(key, out value); | ||
|
||
public T this[MemberInfo key] | ||
{ | ||
get => _primary[key] ?? _secondary[key]; | ||
set => throw new InvalidOperationException("Not supported."); | ||
} | ||
|
||
public ICollection<MemberInfo> Keys => _primary.Keys; | ||
|
||
public ICollection<T> Values => _primary.Values; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,16 @@ | ||
using ExtendedXmlSerializer.ExtensionModel.References; | ||
using ExtendedXmlSerializer.ReflectionModel; | ||
using System.Reflection; | ||
|
||
namespace ExtendedXmlSerializer.ExtensionModel | ||
{ | ||
static class Defaults | ||
{ | ||
public static TypeInfo Reference { get; } = typeof(ReferenceIdentity).GetTypeInfo(); | ||
|
||
public static ITypeComparer TypeComparer { get; } | ||
= new CompositeTypeComparer(ImplementedTypeComparer.Default, | ||
TypeIdentityComparer.Default, | ||
InheritedTypeComparer.Default); | ||
} | ||
} |
9 changes: 2 additions & 7 deletions
9
src/ExtendedXmlSerializer/ExtensionModel/Xml/CustomSerializers.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
src/ExtendedXmlSerializer/ReflectionModel/InheritedMemberComparer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using ExtendedXmlSerializer.Core.Sources; | ||
using System.Collections.Generic; | ||
using System.Reflection; | ||
|
||
namespace ExtendedXmlSerializer.ReflectionModel | ||
{ | ||
sealed class InheritedMemberComparer : FixedInstanceSource<IEqualityComparer<MemberInfo>> | ||
{ | ||
public static InheritedMemberComparer Default { get; } = new InheritedMemberComparer(); | ||
|
||
InheritedMemberComparer() : base(new MemberComparer(InheritedTypeComparer.Default)) {} | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
test/ExtendedXmlSerializer.Tests.ReportedIssues/Issue411Tests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using ExtendedXmlSerializer.Configuration; | ||
using ExtendedXmlSerializer.Tests.ReportedIssues.Support; | ||
using FluentAssertions; | ||
using Xunit; | ||
|
||
namespace ExtendedXmlSerializer.Tests.ReportedIssues | ||
{ | ||
public sealed class Issue411Tests | ||
{ | ||
[Fact] | ||
public void Verify() | ||
{ | ||
var serializer = new ConfigurationContainer().Type<SubjectBase>() | ||
.Member(x => x.Message) | ||
.EmitWhen(x => false) | ||
.Create() | ||
.ForTesting(); | ||
|
||
serializer.Cycle(new Subject {Message = "Hello World!"}).Message.Should().BeNull(); | ||
} | ||
|
||
|
||
[Fact] | ||
public void VerifyInstance() | ||
{ | ||
var serializer = new ConfigurationContainer().Type<SubjectBase>() | ||
.Member(x => x.Message) | ||
.EmitWhenInstance(x => false) | ||
.Create() | ||
.ForTesting(); | ||
|
||
serializer.Cycle(new Subject {Message = "Hello World!"}).Message.Should().BeNull(); | ||
} | ||
|
||
sealed class Subject : SubjectBase | ||
{ | ||
public override string Message { get; set; } | ||
} | ||
|
||
public abstract class SubjectBase | ||
{ | ||
public abstract string Message { get; set; } | ||
} | ||
} | ||
} |