From cad5ecabcc06e8368fbab8e51e81144e212ded18 Mon Sep 17 00:00:00 2001 From: Mike-E Date: Sat, 31 Oct 2020 02:43:03 -0400 Subject: [PATCH] Added check for "unspeakable" types. - https://stackoverflow.com/a/9256695/10340424 --- .../Reflection/AssemblyTypePartitions.cs | 3 +- .../ContentModel/Reflection/GenericTypes.cs | 4 +-- .../ApplicationTypeSpecification.cs | 7 ++-- .../ReflectionModel/IsUnspeakable.cs | 15 ++++++++ .../Issue470Tests.cs | 34 +++++++++++++++++++ 5 files changed, 57 insertions(+), 6 deletions(-) create mode 100644 src/ExtendedXmlSerializer/ReflectionModel/IsUnspeakable.cs create mode 100644 test/ExtendedXmlSerializer.Tests.ReportedIssues/Issue470Tests.cs diff --git a/src/ExtendedXmlSerializer/ContentModel/Reflection/AssemblyTypePartitions.cs b/src/ExtendedXmlSerializer/ContentModel/Reflection/AssemblyTypePartitions.cs index 4b3f76824..d567dd95b 100644 --- a/src/ExtendedXmlSerializer/ContentModel/Reflection/AssemblyTypePartitions.cs +++ b/src/ExtendedXmlSerializer/ContentModel/Reflection/AssemblyTypePartitions.cs @@ -23,7 +23,8 @@ public AssemblyTypePartitions(IPartitionedTypeSpecification specification, IType : this(specification, formatter.Get) {} public AssemblyTypePartitions(ISpecification specification, Func formatter) - : this(specification.IsSatisfiedBy, formatter, ApplicationTypes, x => x.Namespace) {} + : this(specification.And(ApplicationTypeSpecification.Default).IsSatisfiedBy, formatter, ApplicationTypes, + x => x.Namespace) {} // ReSharper disable once TooManyDependencies public AssemblyTypePartitions(Func specification, Func formatter, diff --git a/src/ExtendedXmlSerializer/ContentModel/Reflection/GenericTypes.cs b/src/ExtendedXmlSerializer/ContentModel/Reflection/GenericTypes.cs index 28ba17877..bdf0773b3 100644 --- a/src/ExtendedXmlSerializer/ContentModel/Reflection/GenericTypes.cs +++ b/src/ExtendedXmlSerializer/ContentModel/Reflection/GenericTypes.cs @@ -1,10 +1,10 @@ -using System.Collections.Immutable; -using System.Reflection; using ExtendedXmlSerializer.ContentModel.Identification; using ExtendedXmlSerializer.Core.Sources; using ExtendedXmlSerializer.Core.Specifications; using ExtendedXmlSerializer.ReflectionModel; using JetBrains.Annotations; +using System.Collections.Immutable; +using System.Reflection; namespace ExtendedXmlSerializer.ContentModel.Reflection { diff --git a/src/ExtendedXmlSerializer/ReflectionModel/ApplicationTypeSpecification.cs b/src/ExtendedXmlSerializer/ReflectionModel/ApplicationTypeSpecification.cs index 8f7e5c9d9..794536f96 100644 --- a/src/ExtendedXmlSerializer/ReflectionModel/ApplicationTypeSpecification.cs +++ b/src/ExtendedXmlSerializer/ReflectionModel/ApplicationTypeSpecification.cs @@ -1,13 +1,14 @@ +using ExtendedXmlSerializer.Core.Specifications; using System.Reflection; using System.Runtime.CompilerServices; -using ExtendedXmlSerializer.Core.Specifications; namespace ExtendedXmlSerializer.ReflectionModel { - class ApplicationTypeSpecification : InverseSpecification + sealed class ApplicationTypeSpecification : InverseSpecification { public static ApplicationTypeSpecification Default { get; } = new ApplicationTypeSpecification(); - ApplicationTypeSpecification() : base(IsDefinedSpecification.Default) {} + ApplicationTypeSpecification() + : base(IsDefinedSpecification.Default.Or(IsUnspeakable.Default)) {} } } \ No newline at end of file diff --git a/src/ExtendedXmlSerializer/ReflectionModel/IsUnspeakable.cs b/src/ExtendedXmlSerializer/ReflectionModel/IsUnspeakable.cs new file mode 100644 index 000000000..1cf114c98 --- /dev/null +++ b/src/ExtendedXmlSerializer/ReflectionModel/IsUnspeakable.cs @@ -0,0 +1,15 @@ +using ExtendedXmlSerializer.Core.Specifications; +using System.Reflection; + +namespace ExtendedXmlSerializer.ReflectionModel +{ + /// + /// Reference: https://stackoverflow.com/a/9256695/10340424 + /// + sealed class IsUnspeakable : DelegatedSpecification + { + public static IsUnspeakable Default { get; } = new IsUnspeakable(); + + IsUnspeakable() : base(x => x.Name.StartsWith("<")) {} + } +} \ No newline at end of file diff --git a/test/ExtendedXmlSerializer.Tests.ReportedIssues/Issue470Tests.cs b/test/ExtendedXmlSerializer.Tests.ReportedIssues/Issue470Tests.cs new file mode 100644 index 000000000..69a44ff13 --- /dev/null +++ b/test/ExtendedXmlSerializer.Tests.ReportedIssues/Issue470Tests.cs @@ -0,0 +1,34 @@ +using ExtendedXmlSerializer.Configuration; +using ExtendedXmlSerializer.Tests.ReportedIssues.Support; +using FluentAssertions; +using JetBrains.Annotations; +using System; +using System.Threading.Tasks; +using Xunit; + +namespace ExtendedXmlSerializer.Tests.ReportedIssues +{ + public sealed class Issue470Tests + { + [Fact] + public void Verify() + { + var instance = new Foo(); + new ConfigurationContainer().Create().ForTesting().Cycle(instance).Should().BeEquivalentTo(instance); + } + + public class Foo + { + public string Bar { get; set; } + } + + public class Something + { + [UsedImplicitly] + Func Check(Func data) + { + return async o => await data((T)o); // this causes it to bomb out + } + } + } +}