Skip to content

Commit 7980ae8

Browse files
committed
Fix AOT trimming
1 parent 12f69d2 commit 7980ae8

File tree

3 files changed

+7
-6
lines changed

3 files changed

+7
-6
lines changed

src/Tomlyn/Serialization/Internal/TomlTypeInfoResolverPipeline.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ internal static class TomlTypeInfoResolverPipeline
2020
private const string ReflectionBasedSerializationMessage =
2121
"Reflection-based TOML serialization is not compatible with trimming/NativeAOT. " +
2222
"Use a source-generated TomlSerializerContext or pass a TomlTypeInfo instance.";
23+
private const string ReflectionSwitchName = TomlSerializerFeatureSwitches.ReflectionSwitchName;
2324

2425
private static readonly ConditionalWeakTable<TomlSerializerOptions, ConcurrentDictionary<Type, TomlTypeInfo>> CacheByOptions = new();
2526

@@ -81,7 +82,7 @@ private static TomlTypeInfo ResolveUncached(TomlSerializerOptions options, Type
8182
$"Dictionaries must have string keys to be representable as TOML tables. Type '{type.FullName}' is not supported without a custom converter.");
8283
}
8384

84-
if (TomlSerializerFeatureSwitches.IsReflectionEnabledByDefaultCalculated)
85+
if (TomlSerializer.IsReflectionEnabledByDefault)
8586
{
8687
var typeInfo = ResolveFromReflection(options, type);
8788
return TomlPolymorphicTypeInfo.TryWrap(typeInfo);
@@ -94,8 +95,9 @@ private static TomlTypeInfo ResolveUncached(TomlSerializerOptions options, Type
9495
}
9596

9697
throw new TomlException(
97-
$"No TOML metadata is available for type '{type.FullName}'. " +
98-
$"Provide {nameof(TomlSerializerOptions)}.{nameof(TomlSerializerOptions.TypeInfoResolver)} (source generation) or a custom resolver.");
98+
$"Reflection serialization is disabled and no TOML metadata was found for type '{type.FullName}'. " +
99+
$"Provide {nameof(TomlSerializerOptions)}.{nameof(TomlSerializerOptions.TypeInfoResolver)} (source generation) or a custom resolver, " +
100+
$"or enable the '{ReflectionSwitchName}' AppContext switch.");
99101
}
100102

101103
[RequiresUnreferencedCode(ReflectionBasedSerializationMessage)]

src/Tomlyn/Serialization/TomlSerializerFeatureSwitches.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ internal static class TomlSerializerFeatureSwitches
1616
[FeatureSwitchDefinition(ReflectionSwitchName)]
1717
#endif
1818
public static bool IsReflectionEnabledByDefault
19-
=> AppContext.TryGetSwitch(ReflectionSwitchName, out var enabled) ? enabled : true;
19+
=> !AppContext.TryGetSwitch(ReflectionSwitchName, out var enabled) || enabled;
2020

2121
public static readonly bool IsReflectionEnabledByDefaultCalculated = IsReflectionEnabledByDefault;
2222
}

src/Tomlyn/TomlSerializer.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,12 @@ public static class TomlSerializer
2727
"Reflection-based TOML serialization is not compatible with trimming/NativeAOT. " +
2828
"Use a source-generated TomlSerializerContext or pass a TomlTypeInfo instance.";
2929

30-
private static readonly bool ReflectionEnabledByDefault = TomlSerializerFeatureSwitches.IsReflectionEnabledByDefaultCalculated;
3130
private static readonly Encoding DefaultStreamEncoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: true);
3231

3332
/// <summary>
3433
/// Gets a value indicating whether reflection-based serialization is enabled by default.
3534
/// </summary>
36-
public static bool IsReflectionEnabledByDefault => ReflectionEnabledByDefault;
35+
public static bool IsReflectionEnabledByDefault => TomlSerializerFeatureSwitches.IsReflectionEnabledByDefaultCalculated;
3736

3837
/// <summary>
3938
/// Serializes a value into TOML text.

0 commit comments

Comments
 (0)