Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 010d649

Browse files
committed
Add support for treating a non-abstract type as abstract
1 parent e875d95 commit 010d649

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

src/ServiceStack.Text/JsConfig.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,8 @@ public static DeserializationErrorDelegate OnDeserializationError
538538

539539
public static HashSet<Type> TreatValueAsRefTypes = new HashSet<Type>();
540540

541+
public static HashSet<Type> TreatAsAbstractType = new HashSet<Type>();
542+
541543
private static bool? sPreferInterfaces;
542544
/// <summary>
543545
/// If set to true, Interface types will be prefered over concrete types when serializing.
@@ -713,6 +715,7 @@ public static void Reset()
713715
HasSerializeFn = new HashSet<Type>();
714716
HasIncludeDefaultValue = new HashSet<Type>();
715717
TreatValueAsRefTypes = new HashSet<Type> { typeof(KeyValuePair<,>) };
718+
TreatAsAbstractType = new HashSet<Type>();
716719
sPropertyConvention = null;
717720
sExcludePropertyReferences = null;
718721
sExcludeTypes = new HashSet<Type> { typeof(Stream) };
@@ -769,6 +772,23 @@ public class JsConfig<T>
769772
/// </summary>
770773
public static bool? ExcludeTypeInfo = null;
771774

775+
/// <summary>
776+
/// Treat non-abstract type like an abstract type
777+
/// </summary>
778+
public static bool TreatAsAbstractType
779+
{
780+
get { return JsConfig.TreatAsAbstractType.Contains(typeof(T)); }
781+
set
782+
{
783+
if (value)
784+
JsConfig.TreatAsAbstractType.Add(typeof(T));
785+
else
786+
JsConfig.TreatAsAbstractType.Remove(typeof(T));
787+
788+
ClearFnCaches();
789+
}
790+
}
791+
772792
/// <summary>
773793
/// <see langword="true"/> if the <see cref="ITypeSerializer"/> is configured
774794
/// to take advantage of <see cref="CLSCompliantAttribute"/> specification,

src/ServiceStack.Text/ReflectionExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1539,9 +1539,9 @@ public static bool IsStandardClass(this Type type)
15391539
public static bool IsAbstract(this Type type)
15401540
{
15411541
#if (NETFX_CORE || PCL)
1542-
return type.GetTypeInfo().IsAbstract;
1542+
return type.GetTypeInfo().IsAbstract || JsConfig.TreatAsAbstractType.Contains(type);
15431543
#else
1544-
return type.IsAbstract;
1544+
return type.IsAbstract || JsConfig.TreatAsAbstractType.Contains(type);
15451545
#endif
15461546
}
15471547

0 commit comments

Comments
 (0)