diff --git a/.github/workflows/BuildAndPack.yml b/.github/workflows/BuildAndPack.yml
index 63f51e4..57e50ee 100644
--- a/.github/workflows/BuildAndPack.yml
+++ b/.github/workflows/BuildAndPack.yml
@@ -64,9 +64,7 @@ jobs:
.nuke/temp
~/.nuget/packages
!~/.nuget/packages/netescapades.enumgenerators
- !~/.nuget/packages/netescapades.enumgenerators.attributes
!~/.nuget/packages/netescapades.enumgenerators.interceptors
- !~/.nuget/packages/netescapades.enumgenerators.interceptors.attributes
key: ${{ runner.os }}-${{ hashFiles('**/global.json', '**/*.csproj') }}
- name: Run './build.cmd Clean Test TestPackage PushToNuGet
diff --git a/build/Build.cs b/build/Build.cs
index 5b6efa6..b744ad4 100644
--- a/build/Build.cs
+++ b/build/Build.cs
@@ -101,7 +101,6 @@ class Build : NukeBuild
Target TestPackage => _ => _
.DependsOn(Pack)
.After(Test)
- .Produces(ArtifactsDirectory)
.Executes(() =>
{
var projectFiles = new[]
diff --git a/src/NetEscapades.EnumGenerators.Attributes/EnumExtensionsAttribute.cs b/src/NetEscapades.EnumGenerators.Attributes/EnumExtensionsAttribute.cs
index a7a45f1..7182773 100644
--- a/src/NetEscapades.EnumGenerators.Attributes/EnumExtensionsAttribute.cs
+++ b/src/NetEscapades.EnumGenerators.Attributes/EnumExtensionsAttribute.cs
@@ -9,18 +9,27 @@ public class EnumExtensionsAttribute : System.Attribute
{
///
/// The namespace to generate the extension class.
- /// If not provided, the namespace of the enum will be used
+ /// If not provided, the namespace of the enum will be used.
///
public string? ExtensionClassNamespace { get; set; }
///
/// The name to use for the extension class.
- /// If not provided, the enum name with ""Extensions"" will be used.
- /// For example for an Enum called StatusCodes, the default name
- /// will be StatusCodesExtensions
+ /// If not provided, the enum name with an Extensions suffix will be used.
+ /// For example for an Enum called StatusCodes, the default name
+ /// will be StatusCodesExtensions.
///
public string? ExtensionClassName { get; set; }
+ ///
+ /// The metadata source to use when serializing and deserializing using
+ /// ToStringFast() and TryParse(). If not provided
+ /// will be
+ /// used to provide the values. Alternatively, you can disable this feature
+ /// entirely by using .
+ ///
+ public MetadataSource MetadataSource { get; set; } = MetadataSource.EnumMemberAttribute;
+
///
/// By default, when used with NetEscapades.EnumGenerators.Interceptors
/// any interceptable usages of the enum will be replaced by usages of
@@ -48,11 +57,20 @@ public class EnumExtensionsAttribute : System.Attribute
///
/// The name to use for the extension class.
/// If not provided, the enum name with an Extensions suffix will be used.
- /// For example for an Enum called StatusCodes, the default name
- /// will be StatusCodesExtensions.
+ /// For example for an Enum called StatusCodes, the default name
+ /// will be StatusCodesExtensions.
///
public string? ExtensionClassName { get; set; }
+ ///
+ /// The metadata source to use when serializing and deserializing using
+ /// ToStringFast() and TryParse(). If not provided, the
+ /// will be
+ /// used to provide the values. Alternatively, you can disable this feature
+ /// entirely by using .
+ ///
+ public MetadataSource MetadataSource { get; set; } = MetadataSource.EnumMemberAttribute;
+
///
/// By default, when used with NetEscapades.EnumGenerators.Interceptors
/// any interceptable usages of the enum will be replaced by usages of
diff --git a/src/NetEscapades.EnumGenerators.Attributes/MetadataSource.cs b/src/NetEscapades.EnumGenerators.Attributes/MetadataSource.cs
new file mode 100644
index 0000000..92ccfe8
--- /dev/null
+++ b/src/NetEscapades.EnumGenerators.Attributes/MetadataSource.cs
@@ -0,0 +1,39 @@
+namespace NetEscapades.EnumGenerators
+{
+ ///
+ /// Defines where to obtain metadata for serializing and deserializing the enum
+ ///
+ public enum MetadataSource
+ {
+ ///
+ /// Don't use attributes applied to enum members as a source of metadata for
+ /// ToStringFast() and TryParse(). The name of the enum member
+ /// will always be used for serialization.
+ ///
+ None,
+
+ ///
+ /// Use values provided in System.ComponentModel.DataAnnotations.DisplayAttribute for
+ /// determining the value to use for ToStringFast() and TryParse().
+ /// The value of the attribute will be used if available, otherwise the
+ /// name of the enum member will be used for serialization.
+ ///
+ DisplayAttribute,
+
+ ///
+ /// Use values provided in for
+ /// determining the value to use for ToStringFast() and TryParse().
+ /// The value of the attribute will be used if available, otherwise the
+ /// name of the enum member will be used for serialization.
+ ///
+ DescriptionAttribute,
+
+ ///
+ /// Use values provided in for
+ /// determining the value to use for ToStringFast() and TryParse().
+ /// The value of the attribute will be used if available, otherwise the
+ /// name of the enum member will be used for serialization.
+ ///
+ EnumMemberAttribute,
+ }
+}
\ No newline at end of file
diff --git a/src/NetEscapades.EnumGenerators/Attributes.cs b/src/NetEscapades.EnumGenerators/Attributes.cs
index c542908..40c056b 100644
--- a/src/NetEscapades.EnumGenerators/Attributes.cs
+++ b/src/NetEscapades.EnumGenerators/Attributes.cs
@@ -4,6 +4,8 @@ internal static class Attributes
{
public const string DisplayAttribute = "System.ComponentModel.DataAnnotations.DisplayAttribute";
public const string DescriptionAttribute = "System.ComponentModel.DescriptionAttribute";
+ public const string EnumMemberAttribute = "System.Runtime.Serialization.EnumMemberAttribute";
+
public const string EnumExtensionsAttribute = "NetEscapades.EnumGenerators.EnumExtensionsAttribute";
public const string ExternalEnumExtensionsAttribute = "NetEscapades.EnumGenerators.EnumExtensionsAttribute`1";
public const string FlagsAttribute = "System.FlagsAttribute";
diff --git a/src/NetEscapades.EnumGenerators/Constants.cs b/src/NetEscapades.EnumGenerators/Constants.cs
index 7726681..024b570 100644
--- a/src/NetEscapades.EnumGenerators/Constants.cs
+++ b/src/NetEscapades.EnumGenerators/Constants.cs
@@ -3,4 +3,5 @@ namespace NetEscapades.EnumGenerators;
public static class Constants
{
public const string Version = "1.0.0-beta14";
+ public const string MetadataSourcePropertyName = "EnumGenerator_EnumMetadataSource";
}
\ No newline at end of file
diff --git a/src/NetEscapades.EnumGenerators/Diagnostics/DuplicateExtensionClassAnalyzer.cs b/src/NetEscapades.EnumGenerators/Diagnostics/DuplicateExtensionClassAnalyzer.cs
index e5b6474..fcc5eb2 100644
--- a/src/NetEscapades.EnumGenerators/Diagnostics/DuplicateExtensionClassAnalyzer.cs
+++ b/src/NetEscapades.EnumGenerators/Diagnostics/DuplicateExtensionClassAnalyzer.cs
@@ -48,6 +48,7 @@ public override void Initialize(AnalysisContext context)
Location? location = null;
string? ns = null;
string? name = null;
+ MetadataSource? source = null;
foreach (var attributeData in enumSymbol.GetAttributes())
{
if (ct.IsCancellationRequested)
@@ -55,7 +56,7 @@ public override void Initialize(AnalysisContext context)
return;
}
- if (EnumGenerator.TryGetExtensionAttributeDetails(attributeData, ref ns, ref name))
+ if (EnumGenerator.TryGetExtensionAttributeDetails(attributeData, ref ns, ref name, ref source))
{
location = attributeData.ApplicationSyntaxReference?.GetSyntax(ct).GetLocation()
?? enumSymbol.Locations[0];
diff --git a/src/NetEscapades.EnumGenerators/EnumGenerator.cs b/src/NetEscapades.EnumGenerators/EnumGenerator.cs
index 21515e3..8b2b771 100644
--- a/src/NetEscapades.EnumGenerators/EnumGenerator.cs
+++ b/src/NetEscapades.EnumGenerators/EnumGenerator.cs
@@ -3,6 +3,7 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
+using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Operations;
using Microsoft.CodeAnalysis.Text;
@@ -13,11 +14,16 @@ public class EnumGenerator : IIncrementalGenerator
{
public void Initialize(IncrementalGeneratorInitializationContext context)
{
+ var defaultMetadataSource = context.AnalyzerConfigOptionsProvider
+ .Select(GetDefaultMetadataSource);
+
var csharp14IsSupported = context.CompilationProvider
.Select((x,_) => x is CSharpCompilation
{
LanguageVersion: LanguageVersion.Preview or >= (LanguageVersion)1400 // C#14
});
+
+ var defaults = csharp14IsSupported.Combine(defaultMetadataSource);
IncrementalValuesProvider enumsToGenerate = context.SyntaxProvider
.ForAttributeWithMetadataName(Attributes.EnumExtensionsAttribute,
@@ -37,16 +43,35 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
.SelectMany(static (m, _) => m!.Value)
.WithTrackingName(TrackingNames.InitialExternalExtraction);
- context.RegisterSourceOutput(enumsToGenerate.Combine(csharp14IsSupported),
- static (spc, enumToGenerate) => Execute(in enumToGenerate.Left, enumToGenerate.Right, spc));
+ context.RegisterSourceOutput(enumsToGenerate.Combine(defaults),
+ static (spc, enumToGenerate) => Execute(in enumToGenerate.Left, enumToGenerate.Right.Left, enumToGenerate.Right.Right, spc));
- context.RegisterSourceOutput(externalEnums.Combine(csharp14IsSupported),
- static (spc, enumToGenerate) => Execute(in enumToGenerate.Left, enumToGenerate.Right, spc));
+ context.RegisterSourceOutput(externalEnums.Combine(defaults),
+ static (spc, enumToGenerate) => Execute(in enumToGenerate.Left, enumToGenerate.Right.Left, enumToGenerate.Right.Right, spc));
}
- static void Execute(in EnumToGenerate enumToGenerate, bool csharp14IsSupported, SourceProductionContext context)
+ private static MetadataSource GetDefaultMetadataSource(AnalyzerConfigOptionsProvider configOptions, CancellationToken ct)
{
- var (result, filename) = SourceGenerationHelper.GenerateExtensionClass(in enumToGenerate, csharp14IsSupported);
+ const MetadataSource defaultValue = MetadataSource.EnumMemberAttribute;
+ if (configOptions.GlobalOptions.TryGetValue($"build_property.{Constants.MetadataSourcePropertyName}",
+ out var source))
+ {
+ return source switch
+ {
+ nameof(MetadataSource.None) => MetadataSource.None,
+ nameof(MetadataSource.DisplayAttribute) => MetadataSource.DisplayAttribute,
+ nameof(MetadataSource.DescriptionAttribute) => MetadataSource.DescriptionAttribute,
+ nameof(MetadataSource.EnumMemberAttribute) => MetadataSource.EnumMemberAttribute,
+ _ => defaultValue,
+ };
+ }
+
+ return defaultValue;
+ }
+
+ static void Execute(in EnumToGenerate enumToGenerate, bool csharp14IsSupported, MetadataSource source, SourceProductionContext context)
+ {
+ var (result, filename) = SourceGenerationHelper.GenerateExtensionClass(in enumToGenerate, csharp14IsSupported, source);
context.AddSource(filename, SourceText.From(result, Encoding.UTF8));
}
@@ -74,6 +99,7 @@ static void Execute(in EnumToGenerate enumToGenerate, bool csharp14IsSupported,
bool hasFlags = false;
string? name = null;
string? nameSpace = null;
+ MetadataSource? source = null;
foreach (KeyValuePair namedArgument in attribute.NamedArguments)
{
@@ -89,6 +115,12 @@ static void Execute(in EnumToGenerate enumToGenerate, bool csharp14IsSupported,
{
name = n;
}
+
+ if (namedArgument.Key == "MetadataSource"
+ && namedArgument.Value is { Kind: TypedConstantKind.Enum, Value: { } ms })
+ {
+ source = (MetadataSource)(int)ms;
+ }
}
foreach (var attrData in enumSymbol.GetAttributes())
@@ -102,7 +134,7 @@ static void Execute(in EnumToGenerate enumToGenerate, bool csharp14IsSupported,
}
}
- var enumToGenerate = TryExtractEnumSymbol(enumSymbol, name, nameSpace, hasFlags);
+ var enumToGenerate = TryExtractEnumSymbol(enumSymbol, name, nameSpace, source, hasFlags);
if (enumToGenerate is not null)
{
enums ??= new();
@@ -135,6 +167,7 @@ static void Execute(in EnumToGenerate enumToGenerate, bool csharp14IsSupported,
var hasFlags = false;
string? nameSpace = null;
string? name = null;
+ MetadataSource? metadataSource = null;
foreach (AttributeData attributeData in enumSymbol.GetAttributes())
{
@@ -146,13 +179,17 @@ static void Execute(in EnumToGenerate enumToGenerate, bool csharp14IsSupported,
continue;
}
- TryGetExtensionAttributeDetails(attributeData, ref nameSpace, ref name);
+ TryGetExtensionAttributeDetails(attributeData, ref nameSpace, ref name, ref metadataSource);
}
- return TryExtractEnumSymbol(enumSymbol, name, nameSpace, hasFlags);
+ return TryExtractEnumSymbol(enumSymbol, name, nameSpace, metadataSource, hasFlags);
}
- internal static bool TryGetExtensionAttributeDetails(AttributeData attributeData, ref string? nameSpace, ref string? name)
+ internal static bool TryGetExtensionAttributeDetails(
+ AttributeData attributeData,
+ ref string? nameSpace,
+ ref string? name,
+ ref MetadataSource? source)
{
if (attributeData.AttributeClass?.Name != "EnumExtensionsAttribute" ||
attributeData.AttributeClass.ToDisplayString() != Attributes.EnumExtensionsAttribute)
@@ -174,6 +211,12 @@ internal static bool TryGetExtensionAttributeDetails(AttributeData attributeData
{
name = n;
}
+
+ if (namedArgument.Key == "MetadataSource"
+ && namedArgument.Value is { Kind: TypedConstantKind.Enum, Value: { } ms })
+ {
+ source = (MetadataSource)(int)ms;
+ }
}
return true;
@@ -185,7 +228,12 @@ internal static string GetEnumExtensionNamespace(INamedTypeSymbol enumSymbol)
internal static string GetEnumExtensionName(INamedTypeSymbol enumSymbol)
=> enumSymbol.Name + "Extensions";
- static EnumToGenerate? TryExtractEnumSymbol(INamedTypeSymbol enumSymbol, string? name, string? nameSpace, bool hasFlags)
+ static EnumToGenerate? TryExtractEnumSymbol(
+ INamedTypeSymbol enumSymbol,
+ string? name,
+ string? nameSpace,
+ MetadataSource? metadataSource,
+ bool hasFlags)
{
name ??= GetEnumExtensionName(enumSymbol);
nameSpace ??= GetEnumExtensionNamespace(enumSymbol);
@@ -195,8 +243,6 @@ internal static string GetEnumExtensionName(INamedTypeSymbol enumSymbol)
var enumMembers = enumSymbol.GetMembers();
var members = new List<(string, EnumValueOption)>(enumMembers.Length);
- HashSet? displayNames = null;
- var isDisplayNameTheFirstPresence = false;
foreach (var member in enumMembers)
{
@@ -206,6 +252,8 @@ internal static string GetEnumExtensionName(INamedTypeSymbol enumSymbol)
}
string? displayName = null;
+ string? description = null;
+ string? enumMemberValue = null;
foreach (var attribute in member.GetAttributes())
{
if (attribute.AttributeClass?.Name == "DisplayAttribute" &&
@@ -215,9 +263,7 @@ internal static string GetEnumExtensionName(INamedTypeSymbol enumSymbol)
{
if (namedArgument.Key == "Name" && namedArgument.Value.Value?.ToString() is { } dn)
{
- // found display attribute, all done
displayName = dn;
- goto addDisplayName;
}
}
}
@@ -228,22 +274,24 @@ internal static string GetEnumExtensionName(INamedTypeSymbol enumSymbol)
{
if (attribute.ConstructorArguments[0].Value?.ToString() is { } dn)
{
- // found display attribute, all done
- // Handle cases where contains a quote or a backslash
- displayName = dn;
- goto addDisplayName;
+ description = dn;
}
}
- }
- addDisplayName:
- if (displayName is not null)
- {
- displayNames ??= new();
- isDisplayNameTheFirstPresence = displayNames.Add(displayName);
+ if (attribute.AttributeClass?.Name == "EnumMemberAttribute" &&
+ attribute.AttributeClass.ToDisplayString() == Attributes.EnumMemberAttribute)
+ {
+ foreach (var namedArgument in attribute.NamedArguments)
+ {
+ if (namedArgument.Key == "Value" && namedArgument.Value.Value?.ToString() is { } dn)
+ {
+ enumMemberValue = dn;
+ }
+ }
+ }
}
-
- members.Add((member.Name, new EnumValueOption(displayName, isDisplayNameTheFirstPresence, constantValue)));
+
+ members.Add((member.Name, new EnumValueOption(displayName, description, enumMemberValue, constantValue)));
}
return new EnumToGenerate(
@@ -254,7 +302,7 @@ internal static string GetEnumExtensionName(INamedTypeSymbol enumSymbol)
isPublic: enumSymbol.DeclaredAccessibility == Accessibility.Public,
hasFlags: hasFlags,
names: members,
- isDisplayAttributeUsed: displayNames?.Count > 0);
+ metadataSource: metadataSource);
}
}
diff --git a/src/NetEscapades.EnumGenerators/EnumToGenerate.cs b/src/NetEscapades.EnumGenerators/EnumToGenerate.cs
index 92a58e2..f36ff21 100644
--- a/src/NetEscapades.EnumGenerators/EnumToGenerate.cs
+++ b/src/NetEscapades.EnumGenerators/EnumToGenerate.cs
@@ -11,14 +11,13 @@ public readonly record struct EnumToGenerate
public readonly bool IsPublic;
public readonly bool HasFlags;
public readonly string UnderlyingType;
+ public readonly MetadataSource? MetadataSource;
///
/// Key is the enum name.
///
public readonly EquatableArray<(string Key, EnumValueOption Value)> Names;
- public readonly bool IsDisplayAttributeUsed;
-
public EnumToGenerate(
string name,
string ns,
@@ -27,7 +26,7 @@ public EnumToGenerate(
bool isPublic,
List<(string Key, EnumValueOption Value)> names,
bool hasFlags,
- bool isDisplayAttributeUsed)
+ MetadataSource? metadataSource)
{
Name = name;
Namespace = ns;
@@ -36,6 +35,6 @@ public EnumToGenerate(
HasFlags = hasFlags;
IsPublic = isPublic;
FullyQualifiedName = fullyQualifiedName;
- IsDisplayAttributeUsed = isDisplayAttributeUsed;
+ MetadataSource = metadataSource;
}
}
\ No newline at end of file
diff --git a/src/NetEscapades.EnumGenerators/EnumValueOption.cs b/src/NetEscapades.EnumGenerators/EnumValueOption.cs
index 5d37cb3..a57fd17 100644
--- a/src/NetEscapades.EnumGenerators/EnumValueOption.cs
+++ b/src/NetEscapades.EnumGenerators/EnumValueOption.cs
@@ -5,21 +5,36 @@
///
/// Custom name set by the [Display(Name)] attribute.
///
- public string? DisplayName { get; }
- public bool IsDisplayNameTheFirstPresence { get; }
+ private readonly string? _displayName;
+ private readonly string? _description;
+ private readonly string? _enumMemberValue;
public object ConstantValue { get; }
- public EnumValueOption(string? displayName, bool isDisplayNameTheFirstPresence, object constantValue)
+ public EnumValueOption(string? displayName, string? description, string? enumMemberValue, object constantValue)
{
- DisplayName = displayName;
- IsDisplayNameTheFirstPresence = isDisplayNameTheFirstPresence;
+ _displayName = displayName;
ConstantValue = constantValue;
+ _description = description;
+ _enumMemberValue = enumMemberValue;
}
public bool Equals(EnumValueOption other)
{
- return DisplayName == other.DisplayName &&
- IsDisplayNameTheFirstPresence == other.IsDisplayNameTheFirstPresence &&
+ return _displayName == other._displayName &&
+ _description == other._description &&
+ _enumMemberValue == other._enumMemberValue &&
Equals(ConstantValue, other.ConstantValue);
}
+
+ public string? GetMetadataName(MetadataSource metadataSource)
+ => metadataSource switch
+ {
+ MetadataSource.DisplayAttribute => _displayName,
+ MetadataSource.DescriptionAttribute => _description,
+ MetadataSource.EnumMemberAttribute => _enumMemberValue,
+ _ => null,
+ };
+
+ public static EnumValueOption CreateWithoutAttributes(object constantValue)
+ => new(null, null, null, constantValue);
}
diff --git a/src/NetEscapades.EnumGenerators/NetEscapades.EnumGenerators.csproj b/src/NetEscapades.EnumGenerators/NetEscapades.EnumGenerators.csproj
index 147426e..4092213 100644
--- a/src/NetEscapades.EnumGenerators/NetEscapades.EnumGenerators.csproj
+++ b/src/NetEscapades.EnumGenerators/NetEscapades.EnumGenerators.csproj
@@ -33,5 +33,6 @@
+
\ No newline at end of file
diff --git a/src/NetEscapades.EnumGenerators/NetEscapades.EnumGenerators.props b/src/NetEscapades.EnumGenerators/NetEscapades.EnumGenerators.props
new file mode 100644
index 0000000..a30e539
--- /dev/null
+++ b/src/NetEscapades.EnumGenerators/NetEscapades.EnumGenerators.props
@@ -0,0 +1,8 @@
+
+
+ EnumMemberAttribute
+
+
+
+
+
\ No newline at end of file
diff --git a/src/NetEscapades.EnumGenerators/SourceGenerationHelper.cs b/src/NetEscapades.EnumGenerators/SourceGenerationHelper.cs
index 83394e0..d22c141 100644
--- a/src/NetEscapades.EnumGenerators/SourceGenerationHelper.cs
+++ b/src/NetEscapades.EnumGenerators/SourceGenerationHelper.cs
@@ -20,11 +20,24 @@ public static class SourceGenerationHelper
""";
- public static (string Content, string HintName) GenerateExtensionClass(in EnumToGenerate enumToGenerate, bool csharp14IsSupported)
+ public static (string Content, string HintName) GenerateExtensionClass(in EnumToGenerate enumToGenerate, bool csharp14IsSupported, MetadataSource defaultMetadataSource)
{
+ var metadataSource = enumToGenerate.MetadataSource ?? defaultMetadataSource;
+ var isMetadataSourcesEnabled = metadataSource != MetadataSource.None;
+ var attributeName = metadataSource switch
+ {
+ MetadataSource.DisplayAttribute => Attributes.DisplayAttribute,
+ MetadataSource.DescriptionAttribute => Attributes.DescriptionAttribute,
+ MetadataSource.EnumMemberAttribute => Attributes.EnumMemberAttribute,
+ _ => null,
+ };
+
+ HashSet? metadataNames = null;
+
var constantValues = new HashSet
public const int Length = 2;
- ///
- /// Returns the string representation of the value.
- /// If the attribute is decorated with a [Display] or [Description]attribute, then
- /// uses the provided value. Otherwise uses the name of the member, equivalent to
- /// calling ToString() on .
- ///
- /// The value to retrieve the string value for
- /// If uses the value provided in the
- /// [Display] or [Description]attribute as the string representation of the member.
- /// If , always uses the name of the member, the same as if ToString() was called.
- /// The string representation of the value
- public static string ToStringFast(this global::MyEnum value, bool useMetadataAttributes)
- => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast();
-
///
/// Returns the string representation of the value.
/// Directly equivalent to calling ToString() on .
@@ -51,6 +37,20 @@
_ => value.AsUnderlyingType().ToString(),
};
+ ///
+ /// Returns the string representation of the value.
+ /// If the member is decorated with the attribute
+ /// then that value is returned. Otherwise returns uses the name of the member,
+ /// equivalent to calling ToString() on .
+ ///
+ /// The value to retrieve the string value for
+ /// If uses the value provided in the
+ /// attribute as the string representation of the member.
+ /// If , always uses the name of the member, the same as if ToString() was called.
+ /// The string representation of the value
+ public static string ToStringFast(this global::MyEnum value, bool useMetadataAttributes)
+ => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast();
+
private static string ToStringFastWithMetadata(this global::MyEnum value)
=> value.ToStringFast();
@@ -82,26 +82,27 @@
///
/// The name to check if it's defined
/// if a member with the name exists in the enumeration, otherwise
- public static bool IsDefined(string name) => IsDefined(name, allowMatchingMetadataAttribute: false);
+ public static bool IsDefined(string name)
+ => name switch
+ {
+ nameof(global::MyEnum.First) => true,
+ nameof(global::MyEnum.Second) => true,
+ _ => false,
+ };
///
/// Returns a boolean telling whether an enum with the given name exists in the enumeration,
- /// or if a member decorated with a [Display] attribute
+ /// or if a member decorated with
/// with the required name exists.
///
/// The name to check if it's defined
- /// If , considers the value of metadata attributes,otherwise ignores them
+ /// If ,
+ /// considers the value of
+ /// instead of the member name, otherwise ignores them
/// if a member with the name exists in the enumeration, or a member is decorated
/// with a [Display] attribute with the name, otherwise
public static bool IsDefined(string name, bool allowMatchingMetadataAttribute)
- {
- return name switch
- {
- nameof(global::MyEnum.First) => true,
- nameof(global::MyEnum.Second) => true,
- _ => false,
- };
- }
+ => IsDefined(name);
#if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0
///
@@ -109,27 +110,25 @@
///
/// The name to check if it's defined
/// if a member with the name exists in the enumeration, otherwise
- public static bool IsDefined(in global::System.ReadOnlySpan name) => IsDefined(name, allowMatchingMetadataAttribute: false);
+ public static bool IsDefined(in global::System.ReadOnlySpan name)
+ => name switch
+ {
+ global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyEnum.First), global::System.StringComparison.Ordinal) => true,
+ global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyEnum.Second), global::System.StringComparison.Ordinal) => true,
+ _ => false,
+ };
///
/// Returns a boolean telling whether an enum with the given name exists in the enumeration,
- /// or optionally if a member decorated with a [Display] attribute
- /// with the required name exists.
- /// Slower then the overload, but doesn't allocate memory./>
+ /// or optionally if a member decorated with
+ /// exists. Slower then the overload, but doesn't allocate memory./>
///
/// The name to check if it's defined
/// If , considers the value of metadata attributes,otherwise ignores them
/// if a member with the name exists in the enumeration, or a member is decorated
/// with a [Display] attribute with the name, otherwise
public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute)
- {
- return name switch
- {
- global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyEnum.First), global::System.StringComparison.Ordinal) => true,
- global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyEnum.Second), global::System.StringComparison.Ordinal) => true,
- _ => false,
- };
- }
+ => IsDefined(name);
#endif
///
@@ -241,8 +240,9 @@
/// If the parse operation fails, contains the default value of the underlying type
/// of . This parameter is passed uninitialized.
/// to read value in case insensitive mode; to read value in case sensitive mode.
- /// If , considers the value included in metadata attributes such as
- /// [Display] attribute when parsing, otherwise only considers the member names.
+ /// If ,
+ /// considers the value included in attribute
+ /// when parsing, otherwise only considers the member names.
/// if the value parameter was converted successfully; otherwise, .
public static bool TryParse(
#if NETCOREAPP3_0_OR_GREATER
@@ -264,7 +264,6 @@
out global::MyEnum value,
bool allowMatchingMetadataAttribute)
{
-
switch (name)
{
case string s when s.Equals(nameof(global::MyEnum.First), global::System.StringComparison.OrdinalIgnoreCase):
@@ -290,7 +289,6 @@
out global::MyEnum value,
bool allowMatchingMetadataAttribute)
{
-
switch (name)
{
case nameof(global::MyEnum.First):
@@ -345,8 +343,8 @@
///
/// The case-sensitive string representation of the enumeration name or underlying value to convert
/// to read value in case insensitive mode; to read value in case sensitive mode.
- /// If , considers the value included in metadata attributes such as
- /// [Display] attribute when parsing, otherwise only considers the member names.
+ /// If , considers the value included in
+ /// attribute when parsing, otherwise only considers the member names.
/// An object of type whose
/// value is represented by
public static global::MyEnum Parse(
@@ -412,8 +410,8 @@
/// If the parse operation fails, contains the default value of the underlying type
/// of . This parameter is passed uninitialized.
/// to read value in case insensitive mode; to read value in case sensitive mode.
- /// If , considers the value included in metadata attributes such as
- /// [Display] attribute when parsing, otherwise only considers the member names.
+ /// If , considers the value included in
+ /// attribute when parsing, otherwise only considers the member names.
/// if the value parameter was converted successfully; otherwise, .
public static bool TryParse(
#if NETCOREAPP3_0_OR_GREATER
diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsInGlobalNamespace_CSharp14.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsInGlobalNamespace_CSharp14.verified.txt
index 726928f..92e6472 100644
--- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsInGlobalNamespace_CSharp14.verified.txt
+++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsInGlobalNamespace_CSharp14.verified.txt
@@ -23,20 +23,6 @@
///
public const int Length = 2;
- ///
- /// Returns the string representation of the value.
- /// If the attribute is decorated with a [Display] or [Description]attribute, then
- /// uses the provided value. Otherwise uses the name of the member, equivalent to
- /// calling ToString() on .
- ///
- /// The value to retrieve the string value for
- /// If uses the value provided in the
- /// [Display] or [Description]attribute as the string representation of the member.
- /// If , always uses the name of the member, the same as if ToString() was called.
- /// The string representation of the value
- public static string ToStringFast(this global::MyEnum value, bool useMetadataAttributes)
- => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast();
-
///
/// Returns the string representation of the value.
/// Directly equivalent to calling ToString() on .
@@ -51,6 +37,20 @@
_ => value.AsUnderlyingType().ToString(),
};
+ ///
+ /// Returns the string representation of the value.
+ /// If the member is decorated with the attribute
+ /// then that value is returned. Otherwise returns uses the name of the member,
+ /// equivalent to calling ToString() on .
+ ///
+ /// The value to retrieve the string value for
+ /// If uses the value provided in the
+ /// attribute as the string representation of the member.
+ /// If , always uses the name of the member, the same as if ToString() was called.
+ /// The string representation of the value
+ public static string ToStringFast(this global::MyEnum value, bool useMetadataAttributes)
+ => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast();
+
private static string ToStringFastWithMetadata(this global::MyEnum value)
=> value.ToStringFast();
@@ -86,26 +86,27 @@
///
/// The name to check if it's defined
/// if a member with the name exists in the enumeration, otherwise
- public static bool IsDefined(string name) => IsDefined(name, allowMatchingMetadataAttribute: false);
+ public static bool IsDefined(string name)
+ => name switch
+ {
+ nameof(global::MyEnum.First) => true,
+ nameof(global::MyEnum.Second) => true,
+ _ => false,
+ };
///
/// Returns a boolean telling whether an enum with the given name exists in the enumeration,
- /// or if a member decorated with a [Display] attribute
+ /// or if a member decorated with
/// with the required name exists.
///
/// The name to check if it's defined
- /// If , considers the value of metadata attributes,otherwise ignores them
+ /// If ,
+ /// considers the value of
+ /// instead of the member name, otherwise ignores them
/// if a member with the name exists in the enumeration, or a member is decorated
/// with a [Display] attribute with the name, otherwise
public static bool IsDefined(string name, bool allowMatchingMetadataAttribute)
- {
- return name switch
- {
- nameof(global::MyEnum.First) => true,
- nameof(global::MyEnum.Second) => true,
- _ => false,
- };
- }
+ => IsDefined(name);
#if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0
///
@@ -113,27 +114,25 @@
///
/// The name to check if it's defined
/// if a member with the name exists in the enumeration, otherwise
- public static bool IsDefined(in global::System.ReadOnlySpan name) => IsDefined(name, allowMatchingMetadataAttribute: false);
+ public static bool IsDefined(in global::System.ReadOnlySpan name)
+ => name switch
+ {
+ global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyEnum.First), global::System.StringComparison.Ordinal) => true,
+ global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyEnum.Second), global::System.StringComparison.Ordinal) => true,
+ _ => false,
+ };
///
/// Returns a boolean telling whether an enum with the given name exists in the enumeration,
- /// or optionally if a member decorated with a [Display] attribute
- /// with the required name exists.
- /// Slower then the overload, but doesn't allocate memory./>
+ /// or optionally if a member decorated with
+ /// exists. Slower then the overload, but doesn't allocate memory./>
///
/// The name to check if it's defined
/// If , considers the value of metadata attributes,otherwise ignores them
/// if a member with the name exists in the enumeration, or a member is decorated
/// with a [Display] attribute with the name, otherwise
public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute)
- {
- return name switch
- {
- global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyEnum.First), global::System.StringComparison.Ordinal) => true,
- global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyEnum.Second), global::System.StringComparison.Ordinal) => true,
- _ => false,
- };
- }
+ => IsDefined(name);
#endif
///
@@ -245,8 +244,9 @@
/// If the parse operation fails, contains the default value of the underlying type
/// of . This parameter is passed uninitialized.
/// to read value in case insensitive mode; to read value in case sensitive mode.
- /// If , considers the value included in metadata attributes such as
- /// [Display] attribute when parsing, otherwise only considers the member names.
+ /// If ,
+ /// considers the value included in attribute
+ /// when parsing, otherwise only considers the member names.
/// if the value parameter was converted successfully; otherwise, .
public static bool TryParse(
#if NETCOREAPP3_0_OR_GREATER
@@ -268,7 +268,6 @@
out global::MyEnum value,
bool allowMatchingMetadataAttribute)
{
-
switch (name)
{
case string s when s.Equals(nameof(global::MyEnum.First), global::System.StringComparison.OrdinalIgnoreCase):
@@ -294,7 +293,6 @@
out global::MyEnum value,
bool allowMatchingMetadataAttribute)
{
-
switch (name)
{
case nameof(global::MyEnum.First):
@@ -349,8 +347,8 @@
///
/// The case-sensitive string representation of the enumeration name or underlying value to convert
/// to read value in case insensitive mode; to read value in case sensitive mode.
- /// If , considers the value included in metadata attributes such as
- /// [Display] attribute when parsing, otherwise only considers the member names.
+ /// If , considers the value included in
+ /// attribute when parsing, otherwise only considers the member names.
/// An object of type whose
/// value is represented by
public static global::MyEnum Parse(
@@ -416,8 +414,8 @@
/// If the parse operation fails, contains the default value of the underlying type
/// of . This parameter is passed uninitialized.
/// to read value in case insensitive mode; to read value in case sensitive mode.
- /// If , considers the value included in metadata attributes such as
- /// [Display] attribute when parsing, otherwise only considers the member names.
+ /// If , considers the value included in
+ /// attribute when parsing, otherwise only considers the member names.
/// if the value parameter was converted successfully; otherwise, .
public static bool TryParse(
#if NETCOREAPP3_0_OR_GREATER
diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsInNestedClass.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsInNestedClass.verified.txt
index a7fd3b4..defcc91 100644
--- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsInNestedClass.verified.txt
+++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsInNestedClass.verified.txt
@@ -1,4 +1,4 @@
-//------------------------------------------------------------------------------
+//------------------------------------------------------------------------------
//
// This code was generated by the NetEscapades.EnumGenerators source generator
//
@@ -25,20 +25,6 @@ namespace MyTestNameSpace
///
public const int Length = 2;
- ///
- /// Returns the string representation of the value.
- /// If the attribute is decorated with a [Display] or [Description]attribute, then
- /// uses the provided value. Otherwise uses the name of the member, equivalent to
- /// calling ToString() on .
- ///
- /// The value to retrieve the string value for
- /// If uses the value provided in the
- /// [Display] or [Description]attribute as the string representation of the member.
- /// If , always uses the name of the member, the same as if ToString() was called.
- /// The string representation of the value
- public static string ToStringFast(this global::MyTestNameSpace.InnerClass.MyEnum value, bool useMetadataAttributes)
- => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast();
-
///
/// Returns the string representation of the value.
/// Directly equivalent to calling ToString() on .
@@ -53,6 +39,20 @@ namespace MyTestNameSpace
_ => value.AsUnderlyingType().ToString(),
};
+ ///
+ /// Returns the string representation of the value.
+ /// If the member is decorated with the attribute
+ /// then that value is returned. Otherwise returns uses the name of the member,
+ /// equivalent to calling ToString() on .
+ ///
+ /// The value to retrieve the string value for
+ /// If uses the value provided in the
+ /// attribute as the string representation of the member.
+ /// If , always uses the name of the member, the same as if ToString() was called.
+ /// The string representation of the value
+ public static string ToStringFast(this global::MyTestNameSpace.InnerClass.MyEnum value, bool useMetadataAttributes)
+ => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast();
+
private static string ToStringFastWithMetadata(this global::MyTestNameSpace.InnerClass.MyEnum value)
=> value.ToStringFast();
@@ -84,26 +84,27 @@ namespace MyTestNameSpace
///
/// The name to check if it's defined
/// if a member with the name exists in the enumeration, otherwise
- public static bool IsDefined(string name) => IsDefined(name, allowMatchingMetadataAttribute: false);
+ public static bool IsDefined(string name)
+ => name switch
+ {
+ nameof(global::MyTestNameSpace.InnerClass.MyEnum.First) => true,
+ nameof(global::MyTestNameSpace.InnerClass.MyEnum.Second) => true,
+ _ => false,
+ };
///
/// Returns a boolean telling whether an enum with the given name exists in the enumeration,
- /// or if a member decorated with a [Display] attribute
+ /// or if a member decorated with
/// with the required name exists.
///
/// The name to check if it's defined
- /// If , considers the value of metadata attributes,otherwise ignores them
+ /// If ,
+ /// considers the value of
+ /// instead of the member name, otherwise ignores them
/// if a member with the name exists in the enumeration, or a member is decorated
/// with a [Display] attribute with the name, otherwise
public static bool IsDefined(string name, bool allowMatchingMetadataAttribute)
- {
- return name switch
- {
- nameof(global::MyTestNameSpace.InnerClass.MyEnum.First) => true,
- nameof(global::MyTestNameSpace.InnerClass.MyEnum.Second) => true,
- _ => false,
- };
- }
+ => IsDefined(name);
#if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0
///
@@ -111,27 +112,25 @@ namespace MyTestNameSpace
///
/// The name to check if it's defined
/// if a member with the name exists in the enumeration, otherwise
- public static bool IsDefined(in global::System.ReadOnlySpan name) => IsDefined(name, allowMatchingMetadataAttribute: false);
+ public static bool IsDefined(in global::System.ReadOnlySpan name)
+ => name switch
+ {
+ global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.InnerClass.MyEnum.First), global::System.StringComparison.Ordinal) => true,
+ global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.InnerClass.MyEnum.Second), global::System.StringComparison.Ordinal) => true,
+ _ => false,
+ };
///
/// Returns a boolean telling whether an enum with the given name exists in the enumeration,
- /// or optionally if a member decorated with a [Display] attribute
- /// with the required name exists.
- /// Slower then the overload, but doesn't allocate memory./>
+ /// or optionally if a member decorated with
+ /// exists. Slower then the overload, but doesn't allocate memory./>
///
/// The name to check if it's defined
/// If , considers the value of metadata attributes,otherwise ignores them
/// if a member with the name exists in the enumeration, or a member is decorated
/// with a [Display] attribute with the name, otherwise
public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute)
- {
- return name switch
- {
- global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.InnerClass.MyEnum.First), global::System.StringComparison.Ordinal) => true,
- global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.InnerClass.MyEnum.Second), global::System.StringComparison.Ordinal) => true,
- _ => false,
- };
- }
+ => IsDefined(name);
#endif
///
@@ -243,8 +242,9 @@ namespace MyTestNameSpace
/// If the parse operation fails, contains the default value of the underlying type
/// of . This parameter is passed uninitialized.
/// to read value in case insensitive mode; to read value in case sensitive mode.
- /// If , considers the value included in metadata attributes such as
- /// [Display] attribute when parsing, otherwise only considers the member names.
+ /// If ,
+ /// considers the value included in attribute
+ /// when parsing, otherwise only considers the member names.
/// if the value parameter was converted successfully; otherwise, .
public static bool TryParse(
#if NETCOREAPP3_0_OR_GREATER
@@ -266,7 +266,6 @@ namespace MyTestNameSpace
out global::MyTestNameSpace.InnerClass.MyEnum value,
bool allowMatchingMetadataAttribute)
{
-
switch (name)
{
case string s when s.Equals(nameof(global::MyTestNameSpace.InnerClass.MyEnum.First), global::System.StringComparison.OrdinalIgnoreCase):
@@ -292,7 +291,6 @@ namespace MyTestNameSpace
out global::MyTestNameSpace.InnerClass.MyEnum value,
bool allowMatchingMetadataAttribute)
{
-
switch (name)
{
case nameof(global::MyTestNameSpace.InnerClass.MyEnum.First):
@@ -347,8 +345,8 @@ namespace MyTestNameSpace
///
/// The case-sensitive string representation of the enumeration name or underlying value to convert
/// to read value in case insensitive mode; to read value in case sensitive mode.
- /// If , considers the value included in metadata attributes such as
- /// [Display] attribute when parsing, otherwise only considers the member names.
+ /// If , considers the value included in
+ /// attribute when parsing, otherwise only considers the member names.
/// An object of type whose
/// value is represented by
public static global::MyTestNameSpace.InnerClass.MyEnum Parse(
@@ -414,8 +412,8 @@ namespace MyTestNameSpace
/// If the parse operation fails, contains the default value of the underlying type
/// of . This parameter is passed uninitialized.
/// to read value in case insensitive mode; to read value in case sensitive mode.
- /// If , considers the value included in metadata attributes such as
- /// [Display] attribute when parsing, otherwise only considers the member names.
+ /// If , considers the value included in
+ /// attribute when parsing, otherwise only considers the member names.
/// if the value parameter was converted successfully; otherwise, .
public static bool TryParse(
#if NETCOREAPP3_0_OR_GREATER
diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithCustomName.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithCustomName.verified.txt
index 5dab336..20a3553 100644
--- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithCustomName.verified.txt
+++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithCustomName.verified.txt
@@ -25,20 +25,6 @@ namespace MyTestNameSpace
///
public const int Length = 2;
- ///
- /// Returns the string representation of the value.
- /// If the attribute is decorated with a [Display] or [Description]attribute, then
- /// uses the provided value. Otherwise uses the name of the member, equivalent to
- /// calling ToString() on .
- ///
- /// The value to retrieve the string value for
- /// If uses the value provided in the
- /// [Display] or [Description]attribute as the string representation of the member.
- /// If , always uses the name of the member, the same as if ToString() was called.
- /// The string representation of the value
- public static string ToStringFast(this global::MyTestNameSpace.MyEnum value, bool useMetadataAttributes)
- => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast();
-
///
/// Returns the string representation of the value.
/// Directly equivalent to calling ToString() on .
@@ -53,6 +39,20 @@ namespace MyTestNameSpace
_ => value.AsUnderlyingType().ToString(),
};
+ ///
+ /// Returns the string representation of the value.
+ /// If the member is decorated with the attribute
+ /// then that value is returned. Otherwise returns uses the name of the member,
+ /// equivalent to calling ToString() on .
+ ///
+ /// The value to retrieve the string value for
+ /// If uses the value provided in the
+ /// attribute as the string representation of the member.
+ /// If , always uses the name of the member, the same as if ToString() was called.
+ /// The string representation of the value
+ public static string ToStringFast(this global::MyTestNameSpace.MyEnum value, bool useMetadataAttributes)
+ => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast();
+
private static string ToStringFastWithMetadata(this global::MyTestNameSpace.MyEnum value)
=> value.ToStringFast();
@@ -84,26 +84,27 @@ namespace MyTestNameSpace
///
/// The name to check if it's defined
/// if a member with the name exists in the enumeration, otherwise
- public static bool IsDefined(string name) => IsDefined(name, allowMatchingMetadataAttribute: false);
+ public static bool IsDefined(string name)
+ => name switch
+ {
+ nameof(global::MyTestNameSpace.MyEnum.First) => true,
+ nameof(global::MyTestNameSpace.MyEnum.Second) => true,
+ _ => false,
+ };
///
/// Returns a boolean telling whether an enum with the given name exists in the enumeration,
- /// or if a member decorated with a [Display] attribute
+ /// or if a member decorated with
/// with the required name exists.
///
/// The name to check if it's defined
- /// If , considers the value of metadata attributes,otherwise ignores them
+ /// If ,
+ /// considers the value of
+ /// instead of the member name, otherwise ignores them
/// if a member with the name exists in the enumeration, or a member is decorated
/// with a [Display] attribute with the name, otherwise
public static bool IsDefined(string name, bool allowMatchingMetadataAttribute)
- {
- return name switch
- {
- nameof(global::MyTestNameSpace.MyEnum.First) => true,
- nameof(global::MyTestNameSpace.MyEnum.Second) => true,
- _ => false,
- };
- }
+ => IsDefined(name);
#if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0
///
@@ -111,27 +112,25 @@ namespace MyTestNameSpace
///
/// The name to check if it's defined
/// if a member with the name exists in the enumeration, otherwise
- public static bool IsDefined(in global::System.ReadOnlySpan name) => IsDefined(name, allowMatchingMetadataAttribute: false);
+ public static bool IsDefined(in global::System.ReadOnlySpan name)
+ => name switch
+ {
+ global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.Ordinal) => true,
+ global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Second), global::System.StringComparison.Ordinal) => true,
+ _ => false,
+ };
///
/// Returns a boolean telling whether an enum with the given name exists in the enumeration,
- /// or optionally if a member decorated with a [Display] attribute
- /// with the required name exists.
- /// Slower then the overload, but doesn't allocate memory./>
+ /// or optionally if a member decorated with
+ /// exists. Slower then the overload, but doesn't allocate memory./>
///
/// The name to check if it's defined
/// If , considers the value of metadata attributes,otherwise ignores them
/// if a member with the name exists in the enumeration, or a member is decorated
/// with a [Display] attribute with the name, otherwise
public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute)
- {
- return name switch
- {
- global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.Ordinal) => true,
- global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Second), global::System.StringComparison.Ordinal) => true,
- _ => false,
- };
- }
+ => IsDefined(name);
#endif
///
@@ -243,8 +242,9 @@ namespace MyTestNameSpace
/// If the parse operation fails, contains the default value of the underlying type
/// of . This parameter is passed uninitialized.
/// to read value in case insensitive mode; to read value in case sensitive mode.
- /// If , considers the value included in metadata attributes such as
- /// [Display] attribute when parsing, otherwise only considers the member names.
+ /// If ,
+ /// considers the value included in attribute
+ /// when parsing, otherwise only considers the member names.
/// if the value parameter was converted successfully; otherwise, .
public static bool TryParse(
#if NETCOREAPP3_0_OR_GREATER
@@ -266,7 +266,6 @@ namespace MyTestNameSpace
out global::MyTestNameSpace.MyEnum value,
bool allowMatchingMetadataAttribute)
{
-
switch (name)
{
case string s when s.Equals(nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.OrdinalIgnoreCase):
@@ -292,7 +291,6 @@ namespace MyTestNameSpace
out global::MyTestNameSpace.MyEnum value,
bool allowMatchingMetadataAttribute)
{
-
switch (name)
{
case nameof(global::MyTestNameSpace.MyEnum.First):
@@ -347,8 +345,8 @@ namespace MyTestNameSpace
///
/// The case-sensitive string representation of the enumeration name or underlying value to convert
/// to read value in case insensitive mode; to read value in case sensitive mode.
- /// If , considers the value included in metadata attributes such as
- /// [Display] attribute when parsing, otherwise only considers the member names.
+ /// If , considers the value included in
+ /// attribute when parsing, otherwise only considers the member names.
/// An object of type whose
/// value is represented by
public static global::MyTestNameSpace.MyEnum Parse(
@@ -414,8 +412,8 @@ namespace MyTestNameSpace
/// If the parse operation fails, contains the default value of the underlying type
/// of . This parameter is passed uninitialized.
/// to read value in case insensitive mode; to read value in case sensitive mode.
- /// If , considers the value included in metadata attributes such as
- /// [Display] attribute when parsing, otherwise only considers the member names.
+ /// If , considers the value included in
+ /// attribute when parsing, otherwise only considers the member names.
/// if the value parameter was converted successfully; otherwise, .
public static bool TryParse(
#if NETCOREAPP3_0_OR_GREATER
diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithCustomNames.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithCustomNames__.verified.txt
similarity index 92%
rename from tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithCustomNames.verified.txt
rename to tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithCustomNames__.verified.txt
index 79fa5fa..c9e3693 100644
--- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithCustomNames.verified.txt
+++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithCustomNames__.verified.txt
@@ -25,20 +25,6 @@ namespace MyTestNameSpace
///
public const int Length = 4;
- ///
- /// Returns the string representation of the value.
- /// If the attribute is decorated with a [Display] or [Description]attribute, then
- /// uses the provided value. Otherwise uses the name of the member, equivalent to
- /// calling ToString() on .
- ///
- /// The value to retrieve the string value for
- /// If uses the value provided in the
- /// [Display] or [Description]attribute as the string representation of the member.
- /// If , always uses the name of the member, the same as if ToString() was called.
- /// The string representation of the value
- public static string ToStringFast(this global::MyTestNameSpace.MyEnum value, bool useMetadataAttributes)
- => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast();
-
///
/// Returns the string representation of the value.
/// Directly equivalent to calling ToString() on .
@@ -55,6 +41,20 @@ namespace MyTestNameSpace
_ => value.AsUnderlyingType().ToString(),
};
+ ///
+ /// Returns the string representation of the value.
+ /// If the member is decorated with the attribute
+ /// then that value is returned. Otherwise returns uses the name of the member,
+ /// equivalent to calling ToString() on .
+ ///
+ /// The value to retrieve the string value for
+ /// If uses the value provided in the
+ /// attribute as the string representation of the member.
+ /// If , always uses the name of the member, the same as if ToString() was called.
+ /// The string representation of the value
+ public static string ToStringFast(this global::MyTestNameSpace.MyEnum value, bool useMetadataAttributes)
+ => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast();
+
private static string ToStringFastWithMetadata(this global::MyTestNameSpace.MyEnum value)
=> value switch
{
@@ -95,44 +95,37 @@ namespace MyTestNameSpace
///
/// The name to check if it's defined
/// if a member with the name exists in the enumeration, otherwise
- public static bool IsDefined(string name) => IsDefined(name, allowMatchingMetadataAttribute: false);
+ public static bool IsDefined(string name)
+ => name switch
+ {
+ nameof(global::MyTestNameSpace.MyEnum.First) => true,
+ nameof(global::MyTestNameSpace.MyEnum.Second) => true,
+ nameof(global::MyTestNameSpace.MyEnum.Third) => true,
+ nameof(global::MyTestNameSpace.MyEnum.Fourth) => true,
+ _ => false,
+ };
///
/// Returns a boolean telling whether an enum with the given name exists in the enumeration,
- /// or if a member decorated with a [Display] attribute
+ /// or if a member decorated with
/// with the required name exists.
///
/// The name to check if it's defined
- /// If , considers the value of metadata attributes,otherwise ignores them
+ /// If ,
+ /// considers the value of
+ /// instead of the member name, otherwise ignores them
/// if a member with the name exists in the enumeration, or a member is decorated
/// with a [Display] attribute with the name, otherwise
public static bool IsDefined(string name, bool allowMatchingMetadataAttribute)
- {
- var isDefinedInDisplayAttribute = false;
- if (allowMatchingMetadataAttribute)
- {
- isDefinedInDisplayAttribute = name switch
- {
- "2nd" => true,
- "4th" => true,
- _ => false,
- };
- }
-
- if (isDefinedInDisplayAttribute)
- {
- return true;
- }
+ => allowMatchingMetadataAttribute ? IsMetadataNameDefined(name) : IsDefined(name);
- return name switch
+ private static bool IsMetadataNameDefined(string name)
+ => name switch
{
- nameof(global::MyTestNameSpace.MyEnum.First) => true,
- nameof(global::MyTestNameSpace.MyEnum.Second) => true,
- nameof(global::MyTestNameSpace.MyEnum.Third) => true,
- nameof(global::MyTestNameSpace.MyEnum.Fourth) => true,
- _ => false,
+ "2nd" => true,
+ "4th" => true,
+ _ => IsDefined(name),
};
- }
#if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0
///
@@ -140,45 +133,34 @@ namespace MyTestNameSpace
///
/// The name to check if it's defined
/// if a member with the name exists in the enumeration, otherwise
- public static bool IsDefined(in global::System.ReadOnlySpan name) => IsDefined(name, allowMatchingMetadataAttribute: false);
+ public static bool IsDefined(in global::System.ReadOnlySpan name)
+ => name switch
+ {
+ global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.Ordinal) => true,
+ global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Second), global::System.StringComparison.Ordinal) => true,
+ global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Third), global::System.StringComparison.Ordinal) => true,
+ global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Fourth), global::System.StringComparison.Ordinal) => true,
+ _ => false,
+ };
///
/// Returns a boolean telling whether an enum with the given name exists in the enumeration,
- /// or optionally if a member decorated with a [Display] attribute
- /// with the required name exists.
- /// Slower then the overload, but doesn't allocate memory./>
+ /// or optionally if a member decorated with
+ /// exists. Slower then the overload, but doesn't allocate memory./>
///
/// The name to check if it's defined
/// If , considers the value of metadata attributes,otherwise ignores them
/// if a member with the name exists in the enumeration, or a member is decorated
/// with a [Display] attribute with the name, otherwise
- public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute)
- {
- var isDefinedInDisplayAttribute = false;
- if (allowMatchingMetadataAttribute)
- {
- isDefinedInDisplayAttribute = name switch
- {
- global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, "2nd", global::System.StringComparison.Ordinal) => true,
- global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, "4th", global::System.StringComparison.Ordinal) => true,
- _ => false,
- };
- }
-
- if (isDefinedInDisplayAttribute)
- {
- return true;
- }
+ public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute) => allowMatchingMetadataAttribute ? IsMetadataNameDefined(in name) : IsDefined(in name);
- return name switch
+ private static bool IsMetadataNameDefined(in global::System.ReadOnlySpan name)
+ => name switch
{
- global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.Ordinal) => true,
- global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Second), global::System.StringComparison.Ordinal) => true,
- global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Third), global::System.StringComparison.Ordinal) => true,
- global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Fourth), global::System.StringComparison.Ordinal) => true,
- _ => false,
+ global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, "2nd", global::System.StringComparison.Ordinal) => true,
+ global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, "4th", global::System.StringComparison.Ordinal) => true,
+ _ => IsDefined(name),
};
- }
#endif
///
@@ -290,8 +272,9 @@ namespace MyTestNameSpace
/// If the parse operation fails, contains the default value of the underlying type
/// of . This parameter is passed uninitialized.
/// to read value in case insensitive mode; to read value in case sensitive mode.
- /// If , considers the value included in metadata attributes such as
- /// [Display] attribute when parsing, otherwise only considers the member names.
+ /// If ,
+ /// considers the value included in attribute
+ /// when parsing, otherwise only considers the member names.
/// if the value parameter was converted successfully; otherwise, .
public static bool TryParse(
#if NETCOREAPP3_0_OR_GREATER
@@ -434,8 +417,8 @@ namespace MyTestNameSpace
///
/// The case-sensitive string representation of the enumeration name or underlying value to convert
/// to read value in case insensitive mode; to read value in case sensitive mode.
- /// If , considers the value included in metadata attributes such as
- /// [Display] attribute when parsing, otherwise only considers the member names.
+ /// If , considers the value included in
+ /// attribute when parsing, otherwise only considers the member names.
/// An object of type whose
/// value is represented by
public static global::MyTestNameSpace.MyEnum Parse(
@@ -501,8 +484,8 @@ namespace MyTestNameSpace
/// If the parse operation fails, contains the default value of the underlying type
/// of . This parameter is passed uninitialized.
/// to read value in case insensitive mode; to read value in case sensitive mode.
- /// If , considers the value included in metadata attributes such as
- /// [Display] attribute when parsing, otherwise only considers the member names.
+ /// If , considers the value included in
+ /// attribute when parsing, otherwise only considers the member names.
/// if the value parameter was converted successfully; otherwise, .
public static bool TryParse(
#if NETCOREAPP3_0_OR_GREATER
diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithCustomNamespace.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithCustomNamespace.verified.txt
index f570524..d3045eb 100644
--- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithCustomNamespace.verified.txt
+++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithCustomNamespace.verified.txt
@@ -25,20 +25,6 @@ namespace A.B
///
public const int Length = 2;
- ///
- /// Returns the string representation of the value.
- /// If the attribute is decorated with a [Display] or [Description]attribute, then
- /// uses the provided value. Otherwise uses the name of the member, equivalent to
- /// calling ToString() on .
- ///
- /// The value to retrieve the string value for
- /// If uses the value provided in the
- /// [Display] or [Description]attribute as the string representation of the member.
- /// If , always uses the name of the member, the same as if ToString() was called.
- /// The string representation of the value
- public static string ToStringFast(this global::MyTestNameSpace.MyEnum value, bool useMetadataAttributes)
- => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast();
-
///
/// Returns the string representation of the value.
/// Directly equivalent to calling ToString() on .
@@ -53,6 +39,20 @@ namespace A.B
_ => value.AsUnderlyingType().ToString(),
};
+ ///
+ /// Returns the string representation of the value.
+ /// If the member is decorated with the attribute
+ /// then that value is returned. Otherwise returns uses the name of the member,
+ /// equivalent to calling ToString() on .
+ ///
+ /// The value to retrieve the string value for
+ /// If uses the value provided in the
+ /// attribute as the string representation of the member.
+ /// If , always uses the name of the member, the same as if ToString() was called.
+ /// The string representation of the value
+ public static string ToStringFast(this global::MyTestNameSpace.MyEnum value, bool useMetadataAttributes)
+ => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast();
+
private static string ToStringFastWithMetadata(this global::MyTestNameSpace.MyEnum value)
=> value.ToStringFast();
@@ -84,26 +84,27 @@ namespace A.B
///
/// The name to check if it's defined
/// if a member with the name exists in the enumeration, otherwise
- public static bool IsDefined(string name) => IsDefined(name, allowMatchingMetadataAttribute: false);
+ public static bool IsDefined(string name)
+ => name switch
+ {
+ nameof(global::MyTestNameSpace.MyEnum.First) => true,
+ nameof(global::MyTestNameSpace.MyEnum.Second) => true,
+ _ => false,
+ };
///
/// Returns a boolean telling whether an enum with the given name exists in the enumeration,
- /// or if a member decorated with a [Display] attribute
+ /// or if a member decorated with
/// with the required name exists.
///
/// The name to check if it's defined
- /// If , considers the value of metadata attributes,otherwise ignores them
+ /// If ,
+ /// considers the value of
+ /// instead of the member name, otherwise ignores them
/// if a member with the name exists in the enumeration, or a member is decorated
/// with a [Display] attribute with the name, otherwise
public static bool IsDefined(string name, bool allowMatchingMetadataAttribute)
- {
- return name switch
- {
- nameof(global::MyTestNameSpace.MyEnum.First) => true,
- nameof(global::MyTestNameSpace.MyEnum.Second) => true,
- _ => false,
- };
- }
+ => IsDefined(name);
#if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0
///
@@ -111,27 +112,25 @@ namespace A.B
///
/// The name to check if it's defined
/// if a member with the name exists in the enumeration, otherwise
- public static bool IsDefined(in global::System.ReadOnlySpan name) => IsDefined(name, allowMatchingMetadataAttribute: false);
+ public static bool IsDefined(in global::System.ReadOnlySpan name)
+ => name switch
+ {
+ global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.Ordinal) => true,
+ global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Second), global::System.StringComparison.Ordinal) => true,
+ _ => false,
+ };
///
/// Returns a boolean telling whether an enum with the given name exists in the enumeration,
- /// or optionally if a member decorated with a [Display] attribute
- /// with the required name exists.
- /// Slower then the overload, but doesn't allocate memory./>
+ /// or optionally if a member decorated with
+ /// exists. Slower then the overload, but doesn't allocate memory./>
///
/// The name to check if it's defined
/// If , considers the value of metadata attributes,otherwise ignores them
/// if a member with the name exists in the enumeration, or a member is decorated
/// with a [Display] attribute with the name, otherwise
public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute)
- {
- return name switch
- {
- global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.Ordinal) => true,
- global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Second), global::System.StringComparison.Ordinal) => true,
- _ => false,
- };
- }
+ => IsDefined(name);
#endif
///
@@ -243,8 +242,9 @@ namespace A.B
/// If the parse operation fails, contains the default value of the underlying type
/// of . This parameter is passed uninitialized.
/// to read value in case insensitive mode; to read value in case sensitive mode.
- /// If , considers the value included in metadata attributes such as
- /// [Display] attribute when parsing, otherwise only considers the member names.
+ /// If ,
+ /// considers the value included in attribute
+ /// when parsing, otherwise only considers the member names.
/// if the value parameter was converted successfully; otherwise, .
public static bool TryParse(
#if NETCOREAPP3_0_OR_GREATER
@@ -266,7 +266,6 @@ namespace A.B
out global::MyTestNameSpace.MyEnum value,
bool allowMatchingMetadataAttribute)
{
-
switch (name)
{
case string s when s.Equals(nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.OrdinalIgnoreCase):
@@ -292,7 +291,6 @@ namespace A.B
out global::MyTestNameSpace.MyEnum value,
bool allowMatchingMetadataAttribute)
{
-
switch (name)
{
case nameof(global::MyTestNameSpace.MyEnum.First):
@@ -347,8 +345,8 @@ namespace A.B
///
/// The case-sensitive string representation of the enumeration name or underlying value to convert
/// to read value in case insensitive mode; to read value in case sensitive mode.
- /// If , considers the value included in metadata attributes such as
- /// [Display] attribute when parsing, otherwise only considers the member names.
+ /// If , considers the value included in
+ /// attribute when parsing, otherwise only considers the member names.
/// An object of type whose
/// value is represented by
public static global::MyTestNameSpace.MyEnum Parse(
@@ -414,8 +412,8 @@ namespace A.B
/// If the parse operation fails, contains the default value of the underlying type
/// of . This parameter is passed uninitialized.
/// to read value in case insensitive mode; to read value in case sensitive mode.
- /// If , considers the value included in metadata attributes such as
- /// [Display] attribute when parsing, otherwise only considers the member names.
+ /// If , considers the value included in
+ /// attribute when parsing, otherwise only considers the member names.
/// if the value parameter was converted successfully; otherwise, .
public static bool TryParse(
#if NETCOREAPP3_0_OR_GREATER
diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithCustomNamespaceAndName.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithCustomNamespaceAndName.verified.txt
index fe48913..58dbc32 100644
--- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithCustomNamespaceAndName.verified.txt
+++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithCustomNamespaceAndName.verified.txt
@@ -1,4 +1,4 @@
-//------------------------------------------------------------------------------
+//------------------------------------------------------------------------------
//
// This code was generated by the NetEscapades.EnumGenerators source generator
//
@@ -25,20 +25,6 @@ namespace A.B
///
public const int Length = 2;
- ///
- /// Returns the string representation of the value.
- /// If the attribute is decorated with a [Display] or [Description]attribute, then
- /// uses the provided value. Otherwise uses the name of the member, equivalent to
- /// calling ToString() on .
- ///
- /// The value to retrieve the string value for
- /// If uses the value provided in the
- /// [Display] or [Description]attribute as the string representation of the member.
- /// If , always uses the name of the member, the same as if ToString() was called.
- /// The string representation of the value
- public static string ToStringFast(this global::MyTestNameSpace.MyEnum value, bool useMetadataAttributes)
- => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast();
-
///
/// Returns the string representation of the value.
/// Directly equivalent to calling ToString() on .
@@ -53,6 +39,20 @@ namespace A.B
_ => value.AsUnderlyingType().ToString(),
};
+ ///
+ /// Returns the string representation of the value.
+ /// If the member is decorated with the attribute
+ /// then that value is returned. Otherwise returns uses the name of the member,
+ /// equivalent to calling ToString() on .
+ ///
+ /// The value to retrieve the string value for
+ /// If uses the value provided in the
+ /// attribute as the string representation of the member.
+ /// If , always uses the name of the member, the same as if ToString() was called.
+ /// The string representation of the value
+ public static string ToStringFast(this global::MyTestNameSpace.MyEnum value, bool useMetadataAttributes)
+ => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast();
+
private static string ToStringFastWithMetadata(this global::MyTestNameSpace.MyEnum value)
=> value.ToStringFast();
@@ -84,26 +84,27 @@ namespace A.B
///
/// The name to check if it's defined
/// if a member with the name exists in the enumeration, otherwise
- public static bool IsDefined(string name) => IsDefined(name, allowMatchingMetadataAttribute: false);
+ public static bool IsDefined(string name)
+ => name switch
+ {
+ nameof(global::MyTestNameSpace.MyEnum.First) => true,
+ nameof(global::MyTestNameSpace.MyEnum.Second) => true,
+ _ => false,
+ };
///
/// Returns a boolean telling whether an enum with the given name exists in the enumeration,
- /// or if a member decorated with a [Display] attribute
+ /// or if a member decorated with
/// with the required name exists.
///
/// The name to check if it's defined
- /// If , considers the value of metadata attributes,otherwise ignores them
+ /// If ,
+ /// considers the value of
+ /// instead of the member name, otherwise ignores them
/// if a member with the name exists in the enumeration, or a member is decorated
/// with a [Display] attribute with the name, otherwise
public static bool IsDefined(string name, bool allowMatchingMetadataAttribute)
- {
- return name switch
- {
- nameof(global::MyTestNameSpace.MyEnum.First) => true,
- nameof(global::MyTestNameSpace.MyEnum.Second) => true,
- _ => false,
- };
- }
+ => IsDefined(name);
#if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0
///
@@ -111,27 +112,25 @@ namespace A.B
///
/// The name to check if it's defined
/// if a member with the name exists in the enumeration, otherwise
- public static bool IsDefined(in global::System.ReadOnlySpan name) => IsDefined(name, allowMatchingMetadataAttribute: false);
+ public static bool IsDefined(in global::System.ReadOnlySpan name)
+ => name switch
+ {
+ global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.Ordinal) => true,
+ global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Second), global::System.StringComparison.Ordinal) => true,
+ _ => false,
+ };
///
/// Returns a boolean telling whether an enum with the given name exists in the enumeration,
- /// or optionally if a member decorated with a [Display] attribute
- /// with the required name exists.
- /// Slower then the overload, but doesn't allocate memory./>
+ /// or optionally if a member decorated with
+ /// exists. Slower then the overload, but doesn't allocate memory./>
///
/// The name to check if it's defined
/// If , considers the value of metadata attributes,otherwise ignores them
/// if a member with the name exists in the enumeration, or a member is decorated
/// with a [Display] attribute with the name, otherwise
public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute)
- {
- return name switch
- {
- global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.Ordinal) => true,
- global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Second), global::System.StringComparison.Ordinal) => true,
- _ => false,
- };
- }
+ => IsDefined(name);
#endif
///
@@ -243,8 +242,9 @@ namespace A.B
/// If the parse operation fails, contains the default value of the underlying type
/// of . This parameter is passed uninitialized.
/// to read value in case insensitive mode; to read value in case sensitive mode.
- /// If , considers the value included in metadata attributes such as
- /// [Display] attribute when parsing, otherwise only considers the member names.
+ /// If ,
+ /// considers the value included in attribute
+ /// when parsing, otherwise only considers the member names.
/// if the value parameter was converted successfully; otherwise, .
public static bool TryParse(
#if NETCOREAPP3_0_OR_GREATER
@@ -266,7 +266,6 @@ namespace A.B
out global::MyTestNameSpace.MyEnum value,
bool allowMatchingMetadataAttribute)
{
-
switch (name)
{
case string s when s.Equals(nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.OrdinalIgnoreCase):
@@ -292,7 +291,6 @@ namespace A.B
out global::MyTestNameSpace.MyEnum value,
bool allowMatchingMetadataAttribute)
{
-
switch (name)
{
case nameof(global::MyTestNameSpace.MyEnum.First):
@@ -347,8 +345,8 @@ namespace A.B
///
/// The case-sensitive string representation of the enumeration name or underlying value to convert
/// to read value in case insensitive mode; to read value in case sensitive mode.
- /// If , considers the value included in metadata attributes such as
- /// [Display] attribute when parsing, otherwise only considers the member names.
+ /// If , considers the value included in
+ /// attribute when parsing, otherwise only considers the member names.
/// An object of type whose
/// value is represented by
public static global::MyTestNameSpace.MyEnum Parse(
@@ -414,8 +412,8 @@ namespace A.B
/// If the parse operation fails, contains the default value of the underlying type
/// of . This parameter is passed uninitialized.
/// to read value in case insensitive mode; to read value in case sensitive mode.
- /// If , considers the value included in metadata attributes such as
- /// [Display] attribute when parsing, otherwise only considers the member names.
+ /// If , considers the value included in
+ /// attribute when parsing, otherwise only considers the member names.
/// if the value parameter was converted successfully; otherwise, .
public static bool TryParse(
#if NETCOREAPP3_0_OR_GREATER
diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithMetadataName_IgnoringOthers__.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithMetadataName_IgnoringOthers__.verified.txt
new file mode 100644
index 0000000..c9e3693
--- /dev/null
+++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithMetadataName_IgnoringOthers__.verified.txt
@@ -0,0 +1,651 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the NetEscapades.EnumGenerators source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#nullable enable
+
+namespace MyTestNameSpace
+{
+#pragma warning disable CS0612 // Ignore usages of obsolete members or enums
+#pragma warning disable CS0618 // Ignore usages of obsolete members or enums
+ ///
+ /// Extension methods for
+ ///
+ [global::System.CodeDom.Compiler.GeneratedCodeAttribute("NetEscapades.EnumGenerators", "FIXED_VERSION")]
+ public static partial class MyEnumExtensions
+ {
+ ///
+ /// The number of members in the enum.
+ /// This is a non-distinct count of defined names.
+ ///
+ public const int Length = 4;
+
+ ///
+ /// Returns the string representation of the value.
+ /// Directly equivalent to calling ToString() on .
+ ///
+ /// The value to retrieve the string value for
+ /// The string representation of the value, the same as that returned by ToString()
+ public static string ToStringFast(this global::MyTestNameSpace.MyEnum value)
+ => value switch
+ {
+ global::MyTestNameSpace.MyEnum.First => nameof(global::MyTestNameSpace.MyEnum.First),
+ global::MyTestNameSpace.MyEnum.Second => nameof(global::MyTestNameSpace.MyEnum.Second),
+ global::MyTestNameSpace.MyEnum.Third => nameof(global::MyTestNameSpace.MyEnum.Third),
+ global::MyTestNameSpace.MyEnum.Fourth => nameof(global::MyTestNameSpace.MyEnum.Fourth),
+ _ => value.AsUnderlyingType().ToString(),
+ };
+
+ ///
+ /// Returns the string representation of the value.
+ /// If the member is decorated with the attribute
+ /// then that value is returned. Otherwise returns uses the name of the member,
+ /// equivalent to calling ToString() on .
+ ///
+ /// The value to retrieve the string value for
+ /// If uses the value provided in the
+ /// attribute as the string representation of the member.
+ /// If , always uses the name of the member, the same as if ToString() was called.
+ /// The string representation of the value
+ public static string ToStringFast(this global::MyTestNameSpace.MyEnum value, bool useMetadataAttributes)
+ => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast();
+
+ private static string ToStringFastWithMetadata(this global::MyTestNameSpace.MyEnum value)
+ => value switch
+ {
+ global::MyTestNameSpace.MyEnum.First => nameof(global::MyTestNameSpace.MyEnum.First),
+ global::MyTestNameSpace.MyEnum.Second => "2nd",
+ global::MyTestNameSpace.MyEnum.Third => nameof(global::MyTestNameSpace.MyEnum.Third),
+ global::MyTestNameSpace.MyEnum.Fourth => "4th",
+ _ => value.AsUnderlyingType().ToString(),
+ };
+
+ ///
+ /// Cast a value of to the underlying type (int).
+ /// This is mainly a convenience method.
+ ///
+ /// The value of cast to the underlying type.
+ public static int AsUnderlyingType(this global::MyTestNameSpace.MyEnum value)
+ {
+ return (int) value;
+ }
+
+ ///
+ /// Returns a boolean telling whether the given enum value exists in the enumeration.
+ ///
+ /// The value to check if it's defined
+ /// if the value exists in the enumeration, otherwise
+ public static bool IsDefined(global::MyTestNameSpace.MyEnum value)
+ => value switch
+ {
+ global::MyTestNameSpace.MyEnum.First => true,
+ global::MyTestNameSpace.MyEnum.Second => true,
+ global::MyTestNameSpace.MyEnum.Third => true,
+ global::MyTestNameSpace.MyEnum.Fourth => true,
+ _ => false,
+ };
+
+ ///
+ /// Returns a boolean telling whether an enum with the given name exists in the enumeration.
+ ///
+ /// The name to check if it's defined
+ /// if a member with the name exists in the enumeration, otherwise
+ public static bool IsDefined(string name)
+ => name switch
+ {
+ nameof(global::MyTestNameSpace.MyEnum.First) => true,
+ nameof(global::MyTestNameSpace.MyEnum.Second) => true,
+ nameof(global::MyTestNameSpace.MyEnum.Third) => true,
+ nameof(global::MyTestNameSpace.MyEnum.Fourth) => true,
+ _ => false,
+ };
+
+ ///
+ /// Returns a boolean telling whether an enum with the given name exists in the enumeration,
+ /// or if a member decorated with
+ /// with the required name exists.
+ ///
+ /// The name to check if it's defined
+ /// If ,
+ /// considers the value of
+ /// instead of the member name, otherwise ignores them
+ /// if a member with the name exists in the enumeration, or a member is decorated
+ /// with a [Display] attribute with the name, otherwise
+ public static bool IsDefined(string name, bool allowMatchingMetadataAttribute)
+ => allowMatchingMetadataAttribute ? IsMetadataNameDefined(name) : IsDefined(name);
+
+ private static bool IsMetadataNameDefined(string name)
+ => name switch
+ {
+ "2nd" => true,
+ "4th" => true,
+ _ => IsDefined(name),
+ };
+
+#if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0
+ ///
+ /// Returns a boolean telling whether an enum with the given name exists in the enumeration
+ ///
+ /// The name to check if it's defined
+ /// if a member with the name exists in the enumeration, otherwise
+ public static bool IsDefined(in global::System.ReadOnlySpan name)
+ => name switch
+ {
+ global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.Ordinal) => true,
+ global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Second), global::System.StringComparison.Ordinal) => true,
+ global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Third), global::System.StringComparison.Ordinal) => true,
+ global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Fourth), global::System.StringComparison.Ordinal) => true,
+ _ => false,
+ };
+
+ ///
+ /// Returns a boolean telling whether an enum with the given name exists in the enumeration,
+ /// or optionally if a member decorated with
+ /// exists. Slower then the overload, but doesn't allocate memory./>
+ ///
+ /// The name to check if it's defined
+ /// If , considers the value of metadata attributes,otherwise ignores them
+ /// if a member with the name exists in the enumeration, or a member is decorated
+ /// with a [Display] attribute with the name, otherwise
+ public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute) => allowMatchingMetadataAttribute ? IsMetadataNameDefined(in name) : IsDefined(in name);
+
+ private static bool IsMetadataNameDefined(in global::System.ReadOnlySpan name)
+ => name switch
+ {
+ global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, "2nd", global::System.StringComparison.Ordinal) => true,
+ global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, "4th", global::System.StringComparison.Ordinal) => true,
+ _ => IsDefined(name),
+ };
+#endif
+
+ ///
+ /// Converts the string representation of the name or numeric value of
+ /// an to the equivalent instance.
+ ///
+ /// The case-sensitive string representation of the enumeration name or underlying value to convert
+ /// An object of type whose
+ /// value is represented by
+ public static global::MyTestNameSpace.MyEnum Parse(
+#if NETCOREAPP3_0_OR_GREATER
+ [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
+#endif
+ string? name)
+ => TryParse(name, out var value, false, false) ? value : ThrowValueNotFound(name);
+
+ ///
+ /// Converts the string representation of the name or numeric value of
+ /// an to the equivalent instance.
+ ///
+ /// The case-sensitive string representation of the enumeration name or underlying value to convert
+ /// to read value in case insensitive mode; to read value in case sensitive mode.
+ /// An object of type whose
+ /// value is represented by
+ public static global::MyTestNameSpace.MyEnum Parse(
+#if NETCOREAPP3_0_OR_GREATER
+ [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
+#endif
+ string? name,
+ bool ignoreCase)
+ => TryParse(name, out var value, ignoreCase, false) ? value : ThrowValueNotFound(name);
+
+ ///
+ /// Converts the string representation of the name or numeric value of
+ /// an to the equivalent instance.
+ ///
+ /// The case-sensitive string representation of the enumeration name or underlying value to convert
+ /// to read value in case insensitive mode; to read value in case sensitive mode.
+ /// If , considers the value included in metadata attributes such as
+ /// [Display] attribute when parsing, otherwise only considers the member names.
+ /// An object of type whose
+ /// value is represented by
+ public static global::MyTestNameSpace.MyEnum Parse(
+#if NETCOREAPP3_0_OR_GREATER
+ [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
+#endif
+ string? name,
+ bool ignoreCase,
+ bool allowMatchingMetadataAttribute)
+ => TryParse(name, out var value, ignoreCase, allowMatchingMetadataAttribute) ? value : ThrowValueNotFound(name);
+
+#if NETCOREAPP3_0_OR_GREATER
+ [global::System.Diagnostics.CodeAnalysis.DoesNotReturn]
+#endif
+ private static global::MyTestNameSpace.MyEnum ThrowValueNotFound(string? name)
+ => throw new global::System.ArgumentException($"Requested value '{name}' was not found.");
+
+ ///
+ /// Converts the string representation of the name or numeric value of
+ /// an to the equivalent instance.
+ /// The return value indicates whether the conversion succeeded.
+ ///
+ /// The case-sensitive string representation of the enumeration name or underlying value to convert
+ /// When this method returns, contains an object of type
+ /// whose
+ /// value is represented by if the parse operation succeeds.
+ /// If the parse operation fails, contains the default value of the underlying type
+ /// of . This parameter is passed uninitialized.
+ /// if the value parameter was converted successfully; otherwise, .
+ public static bool TryParse(
+#if NETCOREAPP3_0_OR_GREATER
+ [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
+#endif
+ string? name,
+ out global::MyTestNameSpace.MyEnum value)
+ => TryParse(name, out value, false, false);
+
+ ///
+ /// Converts the string representation of the name or numeric value of
+ /// an to the equivalent instance.
+ /// The return value indicates whether the conversion succeeded.
+ ///
+ /// The string representation of the enumeration name or underlying value to convert
+ /// When this method returns, contains an object of type
+ /// whose
+ /// value is represented by if the parse operation succeeds.
+ /// If the parse operation fails, contains the default value of the underlying type
+ /// of . This parameter is passed uninitialized.
+ /// to read value in case insensitive mode; to read value in case sensitive mode.
+ /// if the value parameter was converted successfully; otherwise, .
+ public static bool TryParse(
+#if NETCOREAPP3_0_OR_GREATER
+ [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
+#endif
+ string? name,
+ out global::MyTestNameSpace.MyEnum value,
+ bool ignoreCase)
+ => TryParse(name, out value, ignoreCase, false);
+
+ ///
+ /// Converts the string representation of the name or numeric value of
+ /// an to the equivalent instance.
+ /// The return value indicates whether the conversion succeeded.
+ ///
+ /// The string representation of the enumeration name or underlying value to convert
+ /// When this method returns, contains an object of type
+ /// whose
+ /// value is represented by if the parse operation succeeds.
+ /// If the parse operation fails, contains the default value of the underlying type
+ /// of . This parameter is passed uninitialized.
+ /// to read value in case insensitive mode; to read value in case sensitive mode.
+ /// If ,
+ /// considers the value included in attribute
+ /// when parsing, otherwise only considers the member names.
+ /// if the value parameter was converted successfully; otherwise, .
+ public static bool TryParse(
+#if NETCOREAPP3_0_OR_GREATER
+ [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
+#endif
+ string? name,
+ out global::MyTestNameSpace.MyEnum value,
+ bool ignoreCase,
+ bool allowMatchingMetadataAttribute)
+ => ignoreCase
+ ? TryParseIgnoreCase(name, out value, allowMatchingMetadataAttribute)
+ : TryParseWithCase(name, out value, allowMatchingMetadataAttribute);
+
+ private static bool TryParseIgnoreCase(
+#if NETCOREAPP3_0_OR_GREATER
+ [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
+#endif
+ string? name,
+ out global::MyTestNameSpace.MyEnum value,
+ bool allowMatchingMetadataAttribute)
+ {
+ if (allowMatchingMetadataAttribute)
+ {
+ switch (name)
+ {
+ case string s when s.Equals("2nd", global::System.StringComparison.OrdinalIgnoreCase):
+ value = global::MyTestNameSpace.MyEnum.Second;
+ return true;
+ case string s when s.Equals("4th", global::System.StringComparison.OrdinalIgnoreCase):
+ value = global::MyTestNameSpace.MyEnum.Fourth;
+ return true;
+ default:
+ break;
+ };
+ }
+
+ switch (name)
+ {
+ case string s when s.Equals(nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.OrdinalIgnoreCase):
+ value = global::MyTestNameSpace.MyEnum.First;
+ return true;
+ case string s when s.Equals(nameof(global::MyTestNameSpace.MyEnum.Second), global::System.StringComparison.OrdinalIgnoreCase):
+ value = global::MyTestNameSpace.MyEnum.Second;
+ return true;
+ case string s when s.Equals(nameof(global::MyTestNameSpace.MyEnum.Third), global::System.StringComparison.OrdinalIgnoreCase):
+ value = global::MyTestNameSpace.MyEnum.Third;
+ return true;
+ case string s when s.Equals(nameof(global::MyTestNameSpace.MyEnum.Fourth), global::System.StringComparison.OrdinalIgnoreCase):
+ value = global::MyTestNameSpace.MyEnum.Fourth;
+ return true;
+ case string s when int.TryParse(name, out var val):
+ value = (global::MyTestNameSpace.MyEnum)val;
+ return true;
+ default:
+ value = default;
+ return false;
+ }
+ }
+
+ private static bool TryParseWithCase(
+#if NETCOREAPP3_0_OR_GREATER
+ [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
+#endif
+ string? name,
+ out global::MyTestNameSpace.MyEnum value,
+ bool allowMatchingMetadataAttribute)
+ {
+ if (allowMatchingMetadataAttribute)
+ {
+ switch (name)
+ {
+ case "2nd":
+ value = global::MyTestNameSpace.MyEnum.Second;
+ return true;
+ case "4th":
+ value = global::MyTestNameSpace.MyEnum.Fourth;
+ return true;
+ default:
+ break;
+ };
+ }
+
+ switch (name)
+ {
+ case nameof(global::MyTestNameSpace.MyEnum.First):
+ value = global::MyTestNameSpace.MyEnum.First;
+ return true;
+ case nameof(global::MyTestNameSpace.MyEnum.Second):
+ value = global::MyTestNameSpace.MyEnum.Second;
+ return true;
+ case nameof(global::MyTestNameSpace.MyEnum.Third):
+ value = global::MyTestNameSpace.MyEnum.Third;
+ return true;
+ case nameof(global::MyTestNameSpace.MyEnum.Fourth):
+ value = global::MyTestNameSpace.MyEnum.Fourth;
+ return true;
+ case string s when int.TryParse(name, out var val):
+ value = (global::MyTestNameSpace.MyEnum)val;
+ return true;
+ default:
+ value = default;
+ return false;
+ }
+ }
+
+#if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0
+ ///
+ /// Converts the string representation of the name or numeric value of
+ /// an to the equivalent instance.
+ ///
+ /// The case-sensitive string representation of the enumeration name or underlying value to convert
+ /// An object of type whose
+ /// value is represented by
+ public static global::MyTestNameSpace.MyEnum Parse(
+#if NETCOREAPP3_0_OR_GREATER
+ [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
+#endif
+ in global::System.ReadOnlySpan name)
+ => TryParse(name, out var value, false, false) ? value : ThrowValueNotFound(name.ToString());
+
+ ///
+ /// Converts the string representation of the name or numeric value of
+ /// an to the equivalent instance.
+ ///
+ /// The case-sensitive string representation of the enumeration name or underlying value to convert
+ /// to read value in case insensitive mode; to read value in case sensitive mode.
+ /// An object of type whose
+ /// value is represented by
+ public static global::MyTestNameSpace.MyEnum Parse(
+#if NETCOREAPP3_0_OR_GREATER
+ [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
+#endif
+ in global::System.ReadOnlySpan name,
+ bool ignoreCase)
+ => TryParse(name, out var value, ignoreCase, false) ? value : ThrowValueNotFound(name.ToString());
+
+ ///
+ /// Converts the string representation of the name or numeric value of
+ /// an to the equivalent instance.
+ ///
+ /// The case-sensitive string representation of the enumeration name or underlying value to convert
+ /// to read value in case insensitive mode; to read value in case sensitive mode.
+ /// If , considers the value included in
+ /// attribute when parsing, otherwise only considers the member names.
+ /// An object of type whose
+ /// value is represented by
+ public static global::MyTestNameSpace.MyEnum Parse(
+#if NETCOREAPP3_0_OR_GREATER
+ [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
+#endif
+ in global::System.ReadOnlySpan name,
+ bool ignoreCase,
+ bool allowMatchingMetadataAttribute)
+ => TryParse(name, out var value, ignoreCase, allowMatchingMetadataAttribute) ? value : ThrowValueNotFound(name.ToString());
+
+ ///
+ /// Converts the span representation of the name or numeric value of
+ /// an to the equivalent instance.
+ /// The return value indicates whether the conversion succeeded.
+ ///
+ /// The span representation of the enumeration name or underlying value to convert
+ /// When this method returns, contains an object of type
+ /// whose
+ /// value is represented by if the parse operation succeeds.
+ /// If the parse operation fails, contains the default value of the underlying type
+ /// of . This parameter is passed uninitialized.
+ /// if the value parameter was converted successfully; otherwise, .
+ public static bool TryParse(
+#if NETCOREAPP3_0_OR_GREATER
+ [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
+#endif
+ in global::System.ReadOnlySpan name,
+ out global::MyTestNameSpace.MyEnum value)
+ => TryParse(name, out value, false, false);
+
+ ///
+ /// Converts the span representation of the name or numeric value of
+ /// an to the equivalent instance.
+ /// The return value indicates whether the conversion succeeded.
+ ///
+ /// The span representation of the enumeration name or underlying value to convert
+ /// When this method returns, contains an object of type
+ /// whose
+ /// value is represented by if the parse operation succeeds.
+ /// If the parse operation fails, contains the default value of the underlying type
+ /// of . This parameter is passed uninitialized.
+ /// to read value in case insensitive mode; to read value in case sensitive mode.
+ /// if the value parameter was converted successfully; otherwise, .
+ public static bool TryParse(
+#if NETCOREAPP3_0_OR_GREATER
+ [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
+#endif
+ in global::System.ReadOnlySpan name,
+ out global::MyTestNameSpace.MyEnum value,
+ bool ignoreCase)
+ => TryParse(name, out value, ignoreCase, false);
+
+ ///
+ /// Converts the span representation of the name or numeric value of
+ /// an to the equivalent instance.
+ /// The return value indicates whether the conversion succeeded.
+ ///
+ /// The span representation of the enumeration name or underlying value to convert
+ /// When this method returns, contains an object of type
+ /// whose
+ /// value is represented by if the parse operation succeeds.
+ /// If the parse operation fails, contains the default value of the underlying type
+ /// of . This parameter is passed uninitialized.
+ /// to read value in case insensitive mode; to read value in case sensitive mode.
+ /// If , considers the value included in
+ /// attribute when parsing, otherwise only considers the member names.
+ /// if the value parameter was converted successfully; otherwise, .
+ public static bool TryParse(
+#if NETCOREAPP3_0_OR_GREATER
+ [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
+#endif
+ in global::System.ReadOnlySpan name,
+ out global::MyTestNameSpace.MyEnum result,
+ bool ignoreCase,
+ bool allowMatchingMetadataAttribute)
+ => ignoreCase
+ ? TryParseIgnoreCase(in name, out result, allowMatchingMetadataAttribute)
+ : TryParseWithCase(in name, out result, allowMatchingMetadataAttribute);
+
+ private static bool TryParseIgnoreCase(
+#if NETCOREAPP3_0_OR_GREATER
+ [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
+#endif
+ in global::System.ReadOnlySpan name,
+ out global::MyTestNameSpace.MyEnum result,
+ bool allowMatchingMetadataAttribute)
+ {
+ if (allowMatchingMetadataAttribute)
+ {
+ switch (name)
+ {
+ case global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, "2nd", global::System.StringComparison.OrdinalIgnoreCase):
+ result = global::MyTestNameSpace.MyEnum.Second;
+ return true;
+ case global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, "4th", global::System.StringComparison.OrdinalIgnoreCase):
+ result = global::MyTestNameSpace.MyEnum.Fourth;
+ return true;
+ default:
+ break;
+ };
+ }
+
+ switch (name)
+ {
+ case global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.OrdinalIgnoreCase):
+ result = global::MyTestNameSpace.MyEnum.First;
+ return true;
+ case global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Second), global::System.StringComparison.OrdinalIgnoreCase):
+ result = global::MyTestNameSpace.MyEnum.Second;
+ return true;
+ case global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Third), global::System.StringComparison.OrdinalIgnoreCase):
+ result = global::MyTestNameSpace.MyEnum.Third;
+ return true;
+ case global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Fourth), global::System.StringComparison.OrdinalIgnoreCase):
+ result = global::MyTestNameSpace.MyEnum.Fourth;
+ return true;
+ case global::System.ReadOnlySpan current when int.TryParse(name, out var numericResult):
+ result = (global::MyTestNameSpace.MyEnum)numericResult;
+ return true;
+ default:
+ result = default;
+ return false;
+ }
+ }
+
+ private static bool TryParseWithCase(
+#if NETCOREAPP3_0_OR_GREATER
+ [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
+#endif
+ in global::System.ReadOnlySpan name,
+ out global::MyTestNameSpace.MyEnum result,
+ bool allowMatchingMetadataAttribute)
+ {
+ if (allowMatchingMetadataAttribute)
+ {
+ switch (name)
+ {
+ case global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, "2nd", global::System.StringComparison.Ordinal):
+ result = global::MyTestNameSpace.MyEnum.Second;
+ return true;
+ case global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, "4th", global::System.StringComparison.Ordinal):
+ result = global::MyTestNameSpace.MyEnum.Fourth;
+ return true;
+ default:
+ break;
+ };
+ }
+
+ switch (name)
+ {
+ case global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.Ordinal):
+ result = global::MyTestNameSpace.MyEnum.First;
+ return true;
+ case global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Second), global::System.StringComparison.Ordinal):
+ result = global::MyTestNameSpace.MyEnum.Second;
+ return true;
+ case global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Third), global::System.StringComparison.Ordinal):
+ result = global::MyTestNameSpace.MyEnum.Third;
+ return true;
+ case global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Fourth), global::System.StringComparison.Ordinal):
+ result = global::MyTestNameSpace.MyEnum.Fourth;
+ return true;
+ case global::System.ReadOnlySpan current when int.TryParse(name, out var numericResult):
+ result = (global::MyTestNameSpace.MyEnum)numericResult;
+ return true;
+ default:
+ result = default;
+ return false;
+ }
+ }
+#endif
+
+ ///
+ /// Retrieves an array of the values of the members defined in
+ /// .
+ /// Note that this returns a new array with every invocation, so
+ /// should be cached if appropriate.
+ ///
+ /// An array of the values defined in
+ public static global::MyTestNameSpace.MyEnum[] GetValues()
+ {
+ return new[]
+ {
+ global::MyTestNameSpace.MyEnum.First,
+ global::MyTestNameSpace.MyEnum.Second,
+ global::MyTestNameSpace.MyEnum.Third,
+ global::MyTestNameSpace.MyEnum.Fourth,
+ };
+ }
+
+ ///
+ /// Retrieves an array of the underlying-values of the members defined in
+ /// .
+ /// Note that this returns a new array with every invocation, so
+ /// should be cached if appropriate.
+ ///
+ /// An array of the underlying-values defined in
+ public static int[] GetValuesAsUnderlyingType()
+ {
+ return new[]
+ {
+ (int) global::MyTestNameSpace.MyEnum.First,
+ (int) global::MyTestNameSpace.MyEnum.Second,
+ (int) global::MyTestNameSpace.MyEnum.Third,
+ (int) global::MyTestNameSpace.MyEnum.Fourth,
+ };
+ }
+
+ ///
+ /// Retrieves an array of the names of the members defined in
+ /// .
+ /// Note that this returns a new array with every invocation, so
+ /// should be cached if appropriate.
+ ///
+ /// An array of the names of the members defined in
+ public static string[] GetNames()
+ {
+ return new[]
+ {
+ nameof(global::MyTestNameSpace.MyEnum.First),
+ nameof(global::MyTestNameSpace.MyEnum.Second),
+ nameof(global::MyTestNameSpace.MyEnum.Third),
+ nameof(global::MyTestNameSpace.MyEnum.Fourth),
+ };
+ }
+ }
+#pragma warning restore CS0612 // Ignore usages of obsolete members or enums
+#pragma warning restore CS0618 // Ignore usages of obsolete members or enums
+}
\ No newline at end of file
diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithNoneMetadataName__.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithNoneMetadataName__.verified.txt
new file mode 100644
index 0000000..006df49
--- /dev/null
+++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithNoneMetadataName__.verified.txt
@@ -0,0 +1,457 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the NetEscapades.EnumGenerators source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#nullable enable
+
+namespace MyTestNameSpace
+{
+#pragma warning disable CS0612 // Ignore usages of obsolete members or enums
+#pragma warning disable CS0618 // Ignore usages of obsolete members or enums
+ ///
+ /// Extension methods for
+ ///
+ [global::System.CodeDom.Compiler.GeneratedCodeAttribute("NetEscapades.EnumGenerators", "FIXED_VERSION")]
+ public static partial class MyEnumExtensions
+ {
+ ///
+ /// The number of members in the enum.
+ /// This is a non-distinct count of defined names.
+ ///
+ public const int Length = 4;
+
+ ///
+ /// Returns the string representation of the value.
+ /// Directly equivalent to calling ToString() on .
+ ///
+ /// The value to retrieve the string value for
+ /// The string representation of the value, the same as that returned by ToString()
+ public static string ToStringFast(this global::MyTestNameSpace.MyEnum value)
+ => value switch
+ {
+ global::MyTestNameSpace.MyEnum.First => nameof(global::MyTestNameSpace.MyEnum.First),
+ global::MyTestNameSpace.MyEnum.Second => nameof(global::MyTestNameSpace.MyEnum.Second),
+ global::MyTestNameSpace.MyEnum.Third => nameof(global::MyTestNameSpace.MyEnum.Third),
+ global::MyTestNameSpace.MyEnum.Fourth => nameof(global::MyTestNameSpace.MyEnum.Fourth),
+ _ => value.AsUnderlyingType().ToString(),
+ };
+
+ ///
+ /// Cast a value of to the underlying type (int).
+ /// This is mainly a convenience method.
+ ///
+ /// The value of cast to the underlying type.
+ public static int AsUnderlyingType(this global::MyTestNameSpace.MyEnum value)
+ {
+ return (int) value;
+ }
+
+ ///
+ /// Returns a boolean telling whether the given enum value exists in the enumeration.
+ ///
+ /// The value to check if it's defined
+ /// if the value exists in the enumeration, otherwise
+ public static bool IsDefined(global::MyTestNameSpace.MyEnum value)
+ => value switch
+ {
+ global::MyTestNameSpace.MyEnum.First => true,
+ global::MyTestNameSpace.MyEnum.Second => true,
+ global::MyTestNameSpace.MyEnum.Third => true,
+ global::MyTestNameSpace.MyEnum.Fourth => true,
+ _ => false,
+ };
+
+ ///
+ /// Returns a boolean telling whether an enum with the given name exists in the enumeration.
+ ///
+ /// The name to check if it's defined
+ /// if a member with the name exists in the enumeration, otherwise
+ public static bool IsDefined(string name)
+ => name switch
+ {
+ nameof(global::MyTestNameSpace.MyEnum.First) => true,
+ nameof(global::MyTestNameSpace.MyEnum.Second) => true,
+ nameof(global::MyTestNameSpace.MyEnum.Third) => true,
+ nameof(global::MyTestNameSpace.MyEnum.Fourth) => true,
+ _ => false,
+ };
+
+#if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0
+ ///
+ /// Returns a boolean telling whether an enum with the given name exists in the enumeration
+ ///
+ /// The name to check if it's defined
+ /// if a member with the name exists in the enumeration, otherwise
+ public static bool IsDefined(in global::System.ReadOnlySpan name)
+ => name switch
+ {
+ global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.Ordinal) => true,
+ global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Second), global::System.StringComparison.Ordinal) => true,
+ global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Third), global::System.StringComparison.Ordinal) => true,
+ global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Fourth), global::System.StringComparison.Ordinal) => true,
+ _ => false,
+ };
+#endif
+
+ ///
+ /// Converts the string representation of the name or numeric value of
+ /// an to the equivalent instance.
+ ///
+ /// The case-sensitive string representation of the enumeration name or underlying value to convert
+ /// An object of type whose
+ /// value is represented by
+ public static global::MyTestNameSpace.MyEnum Parse(
+#if NETCOREAPP3_0_OR_GREATER
+ [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
+#endif
+ string? name)
+ => TryParse(name, out var value, false, false) ? value : ThrowValueNotFound(name);
+
+ ///
+ /// Converts the string representation of the name or numeric value of
+ /// an to the equivalent instance.
+ ///
+ /// The case-sensitive string representation of the enumeration name or underlying value to convert
+ /// to read value in case insensitive mode; to read value in case sensitive mode.
+ /// An object of type whose
+ /// value is represented by
+ public static global::MyTestNameSpace.MyEnum Parse(
+#if NETCOREAPP3_0_OR_GREATER
+ [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
+#endif
+ string? name,
+ bool ignoreCase)
+ => TryParse(name, out var value, ignoreCase, false) ? value : ThrowValueNotFound(name);
+
+#if NETCOREAPP3_0_OR_GREATER
+ [global::System.Diagnostics.CodeAnalysis.DoesNotReturn]
+#endif
+ private static global::MyTestNameSpace.MyEnum ThrowValueNotFound(string? name)
+ => throw new global::System.ArgumentException($"Requested value '{name}' was not found.");
+
+ ///
+ /// Converts the string representation of the name or numeric value of
+ /// an to the equivalent instance.
+ /// The return value indicates whether the conversion succeeded.
+ ///
+ /// The case-sensitive string representation of the enumeration name or underlying value to convert
+ /// When this method returns, contains an object of type
+ /// whose
+ /// value is represented by if the parse operation succeeds.
+ /// If the parse operation fails, contains the default value of the underlying type
+ /// of . This parameter is passed uninitialized.
+ /// if the value parameter was converted successfully; otherwise, .
+ public static bool TryParse(
+#if NETCOREAPP3_0_OR_GREATER
+ [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
+#endif
+ string? name,
+ out global::MyTestNameSpace.MyEnum value)
+ => TryParse(name, out value, false, false);
+
+ ///
+ /// Converts the string representation of the name or numeric value of
+ /// an to the equivalent instance.
+ /// The return value indicates whether the conversion succeeded.
+ ///
+ /// The string representation of the enumeration name or underlying value to convert
+ /// When this method returns, contains an object of type
+ /// whose
+ /// value is represented by if the parse operation succeeds.
+ /// If the parse operation fails, contains the default value of the underlying type
+ /// of . This parameter is passed uninitialized.
+ /// to read value in case insensitive mode; to read value in case sensitive mode.
+ /// if the value parameter was converted successfully; otherwise, .
+ public static bool TryParse(
+#if NETCOREAPP3_0_OR_GREATER
+ [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
+#endif
+ string? name,
+ out global::MyTestNameSpace.MyEnum value,
+ bool ignoreCase)
+ => TryParse(name, out value, ignoreCase, false);
+
+ private static bool TryParse(
+#if NETCOREAPP3_0_OR_GREATER
+ [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
+#endif
+ string? name,
+ out global::MyTestNameSpace.MyEnum value,
+ bool ignoreCase,
+ bool allowMatchingMetadataAttribute)
+ => ignoreCase
+ ? TryParseIgnoreCase(name, out value, allowMatchingMetadataAttribute)
+ : TryParseWithCase(name, out value, allowMatchingMetadataAttribute);
+
+ private static bool TryParseIgnoreCase(
+#if NETCOREAPP3_0_OR_GREATER
+ [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
+#endif
+ string? name,
+ out global::MyTestNameSpace.MyEnum value,
+ bool allowMatchingMetadataAttribute)
+ {
+ switch (name)
+ {
+ case string s when s.Equals(nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.OrdinalIgnoreCase):
+ value = global::MyTestNameSpace.MyEnum.First;
+ return true;
+ case string s when s.Equals(nameof(global::MyTestNameSpace.MyEnum.Second), global::System.StringComparison.OrdinalIgnoreCase):
+ value = global::MyTestNameSpace.MyEnum.Second;
+ return true;
+ case string s when s.Equals(nameof(global::MyTestNameSpace.MyEnum.Third), global::System.StringComparison.OrdinalIgnoreCase):
+ value = global::MyTestNameSpace.MyEnum.Third;
+ return true;
+ case string s when s.Equals(nameof(global::MyTestNameSpace.MyEnum.Fourth), global::System.StringComparison.OrdinalIgnoreCase):
+ value = global::MyTestNameSpace.MyEnum.Fourth;
+ return true;
+ case string s when int.TryParse(name, out var val):
+ value = (global::MyTestNameSpace.MyEnum)val;
+ return true;
+ default:
+ value = default;
+ return false;
+ }
+ }
+
+ private static bool TryParseWithCase(
+#if NETCOREAPP3_0_OR_GREATER
+ [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
+#endif
+ string? name,
+ out global::MyTestNameSpace.MyEnum value,
+ bool allowMatchingMetadataAttribute)
+ {
+ switch (name)
+ {
+ case nameof(global::MyTestNameSpace.MyEnum.First):
+ value = global::MyTestNameSpace.MyEnum.First;
+ return true;
+ case nameof(global::MyTestNameSpace.MyEnum.Second):
+ value = global::MyTestNameSpace.MyEnum.Second;
+ return true;
+ case nameof(global::MyTestNameSpace.MyEnum.Third):
+ value = global::MyTestNameSpace.MyEnum.Third;
+ return true;
+ case nameof(global::MyTestNameSpace.MyEnum.Fourth):
+ value = global::MyTestNameSpace.MyEnum.Fourth;
+ return true;
+ case string s when int.TryParse(name, out var val):
+ value = (global::MyTestNameSpace.MyEnum)val;
+ return true;
+ default:
+ value = default;
+ return false;
+ }
+ }
+
+#if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0
+ ///
+ /// Converts the string representation of the name or numeric value of
+ /// an to the equivalent instance.
+ ///
+ /// The case-sensitive string representation of the enumeration name or underlying value to convert
+ /// An object of type whose
+ /// value is represented by
+ public static global::MyTestNameSpace.MyEnum Parse(
+#if NETCOREAPP3_0_OR_GREATER
+ [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
+#endif
+ in global::System.ReadOnlySpan name)
+ => TryParse(name, out var value, false, false) ? value : ThrowValueNotFound(name.ToString());
+
+ ///
+ /// Converts the string representation of the name or numeric value of
+ /// an to the equivalent instance.
+ ///
+ /// The case-sensitive string representation of the enumeration name or underlying value to convert
+ /// to read value in case insensitive mode; to read value in case sensitive mode.
+ /// An object of type whose
+ /// value is represented by
+ public static global::MyTestNameSpace.MyEnum Parse(
+#if NETCOREAPP3_0_OR_GREATER
+ [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
+#endif
+ in global::System.ReadOnlySpan name,
+ bool ignoreCase)
+ => TryParse(name, out var value, ignoreCase, false) ? value : ThrowValueNotFound(name.ToString());
+
+ ///
+ /// Converts the span representation of the name or numeric value of
+ /// an to the equivalent instance.
+ /// The return value indicates whether the conversion succeeded.
+ ///
+ /// The span representation of the enumeration name or underlying value to convert
+ /// When this method returns, contains an object of type
+ /// whose
+ /// value is represented by if the parse operation succeeds.
+ /// If the parse operation fails, contains the default value of the underlying type
+ /// of . This parameter is passed uninitialized.
+ /// if the value parameter was converted successfully; otherwise, .
+ public static bool TryParse(
+#if NETCOREAPP3_0_OR_GREATER
+ [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
+#endif
+ in global::System.ReadOnlySpan name,
+ out global::MyTestNameSpace.MyEnum value)
+ => TryParse(name, out value, false, false);
+
+ ///
+ /// Converts the span representation of the name or numeric value of
+ /// an to the equivalent instance.
+ /// The return value indicates whether the conversion succeeded.
+ ///
+ /// The span representation of the enumeration name or underlying value to convert
+ /// When this method returns, contains an object of type
+ /// whose
+ /// value is represented by if the parse operation succeeds.
+ /// If the parse operation fails, contains the default value of the underlying type
+ /// of . This parameter is passed uninitialized.
+ /// to read value in case insensitive mode; to read value in case sensitive mode.
+ /// if the value parameter was converted successfully; otherwise, .
+ public static bool TryParse(
+#if NETCOREAPP3_0_OR_GREATER
+ [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
+#endif
+ in global::System.ReadOnlySpan name,
+ out global::MyTestNameSpace.MyEnum value,
+ bool ignoreCase)
+ => TryParse(name, out value, ignoreCase, false);
+
+ private static bool TryParse(
+#if NETCOREAPP3_0_OR_GREATER
+ [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
+#endif
+ in global::System.ReadOnlySpan name,
+ out global::MyTestNameSpace.MyEnum value,
+ bool ignoreCase,
+ bool allowMatchingMetadataAttribute)
+ => ignoreCase
+ ? TryParseIgnoreCase(in name, out value, allowMatchingMetadataAttribute)
+ : TryParseWithCase(in name, out value, allowMatchingMetadataAttribute);
+
+ private static bool TryParseIgnoreCase(
+#if NETCOREAPP3_0_OR_GREATER
+ [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
+#endif
+ in global::System.ReadOnlySpan name,
+ out global::MyTestNameSpace.MyEnum result,
+ bool allowMatchingMetadataAttribute)
+ {
+ switch (name)
+ {
+ case global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.OrdinalIgnoreCase):
+ result = global::MyTestNameSpace.MyEnum.First;
+ return true;
+ case global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Second), global::System.StringComparison.OrdinalIgnoreCase):
+ result = global::MyTestNameSpace.MyEnum.Second;
+ return true;
+ case global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Third), global::System.StringComparison.OrdinalIgnoreCase):
+ result = global::MyTestNameSpace.MyEnum.Third;
+ return true;
+ case global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Fourth), global::System.StringComparison.OrdinalIgnoreCase):
+ result = global::MyTestNameSpace.MyEnum.Fourth;
+ return true;
+ case global::System.ReadOnlySpan current when int.TryParse(name, out var numericResult):
+ result = (global::MyTestNameSpace.MyEnum)numericResult;
+ return true;
+ default:
+ result = default;
+ return false;
+ }
+ }
+
+ private static bool TryParseWithCase(
+#if NETCOREAPP3_0_OR_GREATER
+ [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
+#endif
+ in global::System.ReadOnlySpan name,
+ out global::MyTestNameSpace.MyEnum result,
+ bool allowMatchingMetadataAttribute)
+ {
+ switch (name)
+ {
+ case global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.Ordinal):
+ result = global::MyTestNameSpace.MyEnum.First;
+ return true;
+ case global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Second), global::System.StringComparison.Ordinal):
+ result = global::MyTestNameSpace.MyEnum.Second;
+ return true;
+ case global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Third), global::System.StringComparison.Ordinal):
+ result = global::MyTestNameSpace.MyEnum.Third;
+ return true;
+ case global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Fourth), global::System.StringComparison.Ordinal):
+ result = global::MyTestNameSpace.MyEnum.Fourth;
+ return true;
+ case global::System.ReadOnlySpan current when int.TryParse(name, out var numericResult):
+ result = (global::MyTestNameSpace.MyEnum)numericResult;
+ return true;
+ default:
+ result = default;
+ return false;
+ }
+ }
+#endif
+
+ ///
+ /// Retrieves an array of the values of the members defined in
+ /// .
+ /// Note that this returns a new array with every invocation, so
+ /// should be cached if appropriate.
+ ///
+ /// An array of the values defined in
+ public static global::MyTestNameSpace.MyEnum[] GetValues()
+ {
+ return new[]
+ {
+ global::MyTestNameSpace.MyEnum.First,
+ global::MyTestNameSpace.MyEnum.Second,
+ global::MyTestNameSpace.MyEnum.Third,
+ global::MyTestNameSpace.MyEnum.Fourth,
+ };
+ }
+
+ ///
+ /// Retrieves an array of the underlying-values of the members defined in
+ /// .
+ /// Note that this returns a new array with every invocation, so
+ /// should be cached if appropriate.
+ ///
+ /// An array of the underlying-values defined in
+ public static int[] GetValuesAsUnderlyingType()
+ {
+ return new[]
+ {
+ (int) global::MyTestNameSpace.MyEnum.First,
+ (int) global::MyTestNameSpace.MyEnum.Second,
+ (int) global::MyTestNameSpace.MyEnum.Third,
+ (int) global::MyTestNameSpace.MyEnum.Fourth,
+ };
+ }
+
+ ///
+ /// Retrieves an array of the names of the members defined in
+ /// .
+ /// Note that this returns a new array with every invocation, so
+ /// should be cached if appropriate.
+ ///
+ /// An array of the names of the members defined in
+ public static string[] GetNames()
+ {
+ return new[]
+ {
+ nameof(global::MyTestNameSpace.MyEnum.First),
+ nameof(global::MyTestNameSpace.MyEnum.Second),
+ nameof(global::MyTestNameSpace.MyEnum.Third),
+ nameof(global::MyTestNameSpace.MyEnum.Fourth),
+ };
+ }
+ }
+#pragma warning restore CS0612 // Ignore usages of obsolete members or enums
+#pragma warning restore CS0618 // Ignore usages of obsolete members or enums
+}
\ No newline at end of file
diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithSameDisplayName.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithSameDisplayName__.verified.txt
similarity index 92%
rename from tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithSameDisplayName.verified.txt
rename to tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithSameDisplayName__.verified.txt
index 1dec877..bcb62e3 100644
--- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithSameDisplayName.verified.txt
+++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithSameDisplayName__.verified.txt
@@ -1,4 +1,4 @@
-//------------------------------------------------------------------------------
+//------------------------------------------------------------------------------
//
// This code was generated by the NetEscapades.EnumGenerators source generator
//
@@ -25,20 +25,6 @@ namespace MyTestNameSpace
///
public const int Length = 4;
- ///
- /// Returns the string representation of the value.
- /// If the attribute is decorated with a [Display] or [Description]attribute, then
- /// uses the provided value. Otherwise uses the name of the member, equivalent to
- /// calling ToString() on .
- ///
- /// The value to retrieve the string value for
- /// If uses the value provided in the
- /// [Display] or [Description]attribute as the string representation of the member.
- /// If , always uses the name of the member, the same as if ToString() was called.
- /// The string representation of the value
- public static string ToStringFast(this global::MyTestNameSpace.MyEnum value, bool useMetadataAttributes)
- => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast();
-
///
/// Returns the string representation of the value.
/// Directly equivalent to calling ToString() on .
@@ -55,6 +41,20 @@ namespace MyTestNameSpace
_ => value.AsUnderlyingType().ToString(),
};
+ ///
+ /// Returns the string representation of the value.
+ /// If the member is decorated with the attribute
+ /// then that value is returned. Otherwise returns uses the name of the member,
+ /// equivalent to calling ToString() on .
+ ///
+ /// The value to retrieve the string value for
+ /// If uses the value provided in the
+ /// attribute as the string representation of the member.
+ /// If , always uses the name of the member, the same as if ToString() was called.
+ /// The string representation of the value
+ public static string ToStringFast(this global::MyTestNameSpace.MyEnum value, bool useMetadataAttributes)
+ => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast();
+
private static string ToStringFastWithMetadata(this global::MyTestNameSpace.MyEnum value)
=> value switch
{
@@ -95,43 +95,36 @@ namespace MyTestNameSpace
///
/// The name to check if it's defined
/// if a member with the name exists in the enumeration, otherwise
- public static bool IsDefined(string name) => IsDefined(name, allowMatchingMetadataAttribute: false);
+ public static bool IsDefined(string name)
+ => name switch
+ {
+ nameof(global::MyTestNameSpace.MyEnum.First) => true,
+ nameof(global::MyTestNameSpace.MyEnum.Second) => true,
+ nameof(global::MyTestNameSpace.MyEnum.Third) => true,
+ nameof(global::MyTestNameSpace.MyEnum.Fourth) => true,
+ _ => false,
+ };
///
/// Returns a boolean telling whether an enum with the given name exists in the enumeration,
- /// or if a member decorated with a [Display] attribute
+ /// or if a member decorated with
/// with the required name exists.
///
/// The name to check if it's defined
- /// If , considers the value of metadata attributes,otherwise ignores them
+ /// If ,
+ /// considers the value of
+ /// instead of the member name, otherwise ignores them
/// if a member with the name exists in the enumeration, or a member is decorated
/// with a [Display] attribute with the name, otherwise
public static bool IsDefined(string name, bool allowMatchingMetadataAttribute)
- {
- var isDefinedInDisplayAttribute = false;
- if (allowMatchingMetadataAttribute)
- {
- isDefinedInDisplayAttribute = name switch
- {
- "2nd" => true,
- _ => false,
- };
- }
-
- if (isDefinedInDisplayAttribute)
- {
- return true;
- }
+ => allowMatchingMetadataAttribute ? IsMetadataNameDefined(name) : IsDefined(name);
- return name switch
+ private static bool IsMetadataNameDefined(string name)
+ => name switch
{
- nameof(global::MyTestNameSpace.MyEnum.First) => true,
- nameof(global::MyTestNameSpace.MyEnum.Second) => true,
- nameof(global::MyTestNameSpace.MyEnum.Third) => true,
- nameof(global::MyTestNameSpace.MyEnum.Fourth) => true,
- _ => false,
+ "2nd" => true,
+ _ => IsDefined(name),
};
- }
#if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0
///
@@ -139,44 +132,33 @@ namespace MyTestNameSpace
///
/// The name to check if it's defined
/// if a member with the name exists in the enumeration, otherwise
- public static bool IsDefined(in global::System.ReadOnlySpan name) => IsDefined(name, allowMatchingMetadataAttribute: false);
+ public static bool IsDefined(in global::System.ReadOnlySpan name)
+ => name switch
+ {
+ global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.Ordinal) => true,
+ global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Second), global::System.StringComparison.Ordinal) => true,
+ global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Third), global::System.StringComparison.Ordinal) => true,
+ global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Fourth), global::System.StringComparison.Ordinal) => true,
+ _ => false,
+ };
///
/// Returns a boolean telling whether an enum with the given name exists in the enumeration,
- /// or optionally if a member decorated with a [Display] attribute
- /// with the required name exists.
- /// Slower then the overload, but doesn't allocate memory./>
+ /// or optionally if a member decorated with
+ /// exists. Slower then the overload, but doesn't allocate memory./>
///
/// The name to check if it's defined
/// If , considers the value of metadata attributes,otherwise ignores them
/// if a member with the name exists in the enumeration, or a member is decorated
/// with a [Display] attribute with the name, otherwise
- public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute)
- {
- var isDefinedInDisplayAttribute = false;
- if (allowMatchingMetadataAttribute)
- {
- isDefinedInDisplayAttribute = name switch
- {
- global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, "2nd", global::System.StringComparison.Ordinal) => true,
- _ => false,
- };
- }
-
- if (isDefinedInDisplayAttribute)
- {
- return true;
- }
+ public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute) => allowMatchingMetadataAttribute ? IsMetadataNameDefined(in name) : IsDefined(in name);
- return name switch
+ private static bool IsMetadataNameDefined(in global::System.ReadOnlySpan name)
+ => name switch
{
- global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.Ordinal) => true,
- global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Second), global::System.StringComparison.Ordinal) => true,
- global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Third), global::System.StringComparison.Ordinal) => true,
- global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Fourth), global::System.StringComparison.Ordinal) => true,
- _ => false,
+ global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, "2nd", global::System.StringComparison.Ordinal) => true,
+ _ => IsDefined(name),
};
- }
#endif
///
@@ -288,8 +270,9 @@ namespace MyTestNameSpace
/// If the parse operation fails, contains the default value of the underlying type
/// of . This parameter is passed uninitialized.
/// to read value in case insensitive mode; to read value in case sensitive mode.
- /// If , considers the value included in metadata attributes such as
- /// [Display] attribute when parsing, otherwise only considers the member names.
+ /// If ,
+ /// considers the value included in attribute
+ /// when parsing, otherwise only considers the member names.
/// if the value parameter was converted successfully; otherwise, .
public static bool TryParse(
#if NETCOREAPP3_0_OR_GREATER
@@ -426,8 +409,8 @@ namespace MyTestNameSpace
///
/// The case-sensitive string representation of the enumeration name or underlying value to convert
/// to read value in case insensitive mode; to read value in case sensitive mode.
- /// If , considers the value included in metadata attributes such as
- /// [Display] attribute when parsing, otherwise only considers the member names.
+ /// If , considers the value included in
+ /// attribute when parsing, otherwise only considers the member names.
/// An object of type whose
/// value is represented by
public static global::MyTestNameSpace.MyEnum Parse(
@@ -493,8 +476,8 @@ namespace MyTestNameSpace
/// If the parse operation fails, contains the default value of the underlying type
/// of . This parameter is passed uninitialized.
/// to read value in case insensitive mode; to read value in case sensitive mode.
- /// If , considers the value included in metadata attributes such as
- /// [Display] attribute when parsing, otherwise only considers the member names.
+ /// If , considers the value included in
+ /// attribute when parsing, otherwise only considers the member names.
/// if the value parameter was converted successfully; otherwise, .
public static bool TryParse(
#if NETCOREAPP3_0_OR_GREATER
diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithWrongMetadataName__.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithWrongMetadataName__.verified.txt
new file mode 100644
index 0000000..4f950e9
--- /dev/null
+++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateEnumExtensionsWithWrongMetadataName__.verified.txt
@@ -0,0 +1,569 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the NetEscapades.EnumGenerators source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#nullable enable
+
+namespace MyTestNameSpace
+{
+#pragma warning disable CS0612 // Ignore usages of obsolete members or enums
+#pragma warning disable CS0618 // Ignore usages of obsolete members or enums
+ ///
+ /// Extension methods for
+ ///
+ [global::System.CodeDom.Compiler.GeneratedCodeAttribute("NetEscapades.EnumGenerators", "FIXED_VERSION")]
+ public static partial class MyEnumExtensions
+ {
+ ///
+ /// The number of members in the enum.
+ /// This is a non-distinct count of defined names.
+ ///
+ public const int Length = 4;
+
+ ///
+ /// Returns the string representation of the value.
+ /// Directly equivalent to calling ToString() on .
+ ///
+ /// The value to retrieve the string value for
+ /// The string representation of the value, the same as that returned by ToString()
+ public static string ToStringFast(this global::MyTestNameSpace.MyEnum value)
+ => value switch
+ {
+ global::MyTestNameSpace.MyEnum.First => nameof(global::MyTestNameSpace.MyEnum.First),
+ global::MyTestNameSpace.MyEnum.Second => nameof(global::MyTestNameSpace.MyEnum.Second),
+ global::MyTestNameSpace.MyEnum.Third => nameof(global::MyTestNameSpace.MyEnum.Third),
+ global::MyTestNameSpace.MyEnum.Fourth => nameof(global::MyTestNameSpace.MyEnum.Fourth),
+ _ => value.AsUnderlyingType().ToString(),
+ };
+
+ ///
+ /// Returns the string representation of the value.
+ /// If the member is decorated with the attribute
+ /// then that value is returned. Otherwise returns uses the name of the member,
+ /// equivalent to calling ToString() on .
+ ///
+ /// The value to retrieve the string value for
+ /// If uses the value provided in the
+ /// attribute as the string representation of the member.
+ /// If , always uses the name of the member, the same as if ToString() was called.
+ /// The string representation of the value
+ public static string ToStringFast(this global::MyTestNameSpace.MyEnum value, bool useMetadataAttributes)
+ => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast();
+
+ private static string ToStringFastWithMetadata(this global::MyTestNameSpace.MyEnum value)
+ => value.ToStringFast();
+
+ ///
+ /// Cast a value of to the underlying type (int).
+ /// This is mainly a convenience method.
+ ///
+ /// The value of cast to the underlying type.
+ public static int AsUnderlyingType(this global::MyTestNameSpace.MyEnum value)
+ {
+ return (int) value;
+ }
+
+ ///
+ /// Returns a boolean telling whether the given enum value exists in the enumeration.
+ ///
+ /// The value to check if it's defined
+ /// if the value exists in the enumeration, otherwise
+ public static bool IsDefined(global::MyTestNameSpace.MyEnum value)
+ => value switch
+ {
+ global::MyTestNameSpace.MyEnum.First => true,
+ global::MyTestNameSpace.MyEnum.Second => true,
+ global::MyTestNameSpace.MyEnum.Third => true,
+ global::MyTestNameSpace.MyEnum.Fourth => true,
+ _ => false,
+ };
+
+ ///
+ /// Returns a boolean telling whether an enum with the given name exists in the enumeration.
+ ///
+ /// The name to check if it's defined
+ /// if a member with the name exists in the enumeration, otherwise
+ public static bool IsDefined(string name)
+ => name switch
+ {
+ nameof(global::MyTestNameSpace.MyEnum.First) => true,
+ nameof(global::MyTestNameSpace.MyEnum.Second) => true,
+ nameof(global::MyTestNameSpace.MyEnum.Third) => true,
+ nameof(global::MyTestNameSpace.MyEnum.Fourth) => true,
+ _ => false,
+ };
+
+ ///
+ /// Returns a boolean telling whether an enum with the given name exists in the enumeration,
+ /// or if a member decorated with
+ /// with the required name exists.
+ ///
+ /// The name to check if it's defined
+ /// If ,
+ /// considers the value of
+ /// instead of the member name, otherwise ignores them
+ /// if a member with the name exists in the enumeration, or a member is decorated
+ /// with a [Display] attribute with the name, otherwise
+ public static bool IsDefined(string name, bool allowMatchingMetadataAttribute)
+ => IsDefined(name);
+
+#if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0
+ ///
+ /// Returns a boolean telling whether an enum with the given name exists in the enumeration
+ ///
+ /// The name to check if it's defined
+ /// if a member with the name exists in the enumeration, otherwise
+ public static bool IsDefined(in global::System.ReadOnlySpan name)
+ => name switch
+ {
+ global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.Ordinal) => true,
+ global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Second), global::System.StringComparison.Ordinal) => true,
+ global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Third), global::System.StringComparison.Ordinal) => true,
+ global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Fourth), global::System.StringComparison.Ordinal) => true,
+ _ => false,
+ };
+
+ ///
+ /// Returns a boolean telling whether an enum with the given name exists in the enumeration,
+ /// or optionally if a member decorated with
+ /// exists. Slower then the overload, but doesn't allocate memory./>
+ ///
+ /// The name to check if it's defined
+ /// If , considers the value of metadata attributes,otherwise ignores them
+ /// if a member with the name exists in the enumeration, or a member is decorated
+ /// with a [Display] attribute with the name, otherwise
+ public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute)
+ => IsDefined(name);
+#endif
+
+ ///
+ /// Converts the string representation of the name or numeric value of
+ /// an to the equivalent instance.
+ ///
+ /// The case-sensitive string representation of the enumeration name or underlying value to convert
+ /// An object of type whose
+ /// value is represented by
+ public static global::MyTestNameSpace.MyEnum Parse(
+#if NETCOREAPP3_0_OR_GREATER
+ [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
+#endif
+ string? name)
+ => TryParse(name, out var value, false, false) ? value : ThrowValueNotFound(name);
+
+ ///
+ /// Converts the string representation of the name or numeric value of
+ /// an to the equivalent instance.
+ ///
+ /// The case-sensitive string representation of the enumeration name or underlying value to convert
+ /// to read value in case insensitive mode; to read value in case sensitive mode.
+ /// An object of type whose
+ /// value is represented by
+ public static global::MyTestNameSpace.MyEnum Parse(
+#if NETCOREAPP3_0_OR_GREATER
+ [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
+#endif
+ string? name,
+ bool ignoreCase)
+ => TryParse(name, out var value, ignoreCase, false) ? value : ThrowValueNotFound(name);
+
+ ///
+ /// Converts the string representation of the name or numeric value of
+ /// an to the equivalent instance.
+ ///
+ /// The case-sensitive string representation of the enumeration name or underlying value to convert
+ /// to read value in case insensitive mode; to read value in case sensitive mode.
+ /// If , considers the value included in metadata attributes such as
+ /// [Display] attribute when parsing, otherwise only considers the member names.
+ /// An object of type whose
+ /// value is represented by
+ public static global::MyTestNameSpace.MyEnum Parse(
+#if NETCOREAPP3_0_OR_GREATER
+ [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
+#endif
+ string? name,
+ bool ignoreCase,
+ bool allowMatchingMetadataAttribute)
+ => TryParse(name, out var value, ignoreCase, allowMatchingMetadataAttribute) ? value : ThrowValueNotFound(name);
+
+#if NETCOREAPP3_0_OR_GREATER
+ [global::System.Diagnostics.CodeAnalysis.DoesNotReturn]
+#endif
+ private static global::MyTestNameSpace.MyEnum ThrowValueNotFound(string? name)
+ => throw new global::System.ArgumentException($"Requested value '{name}' was not found.");
+
+ ///
+ /// Converts the string representation of the name or numeric value of
+ /// an to the equivalent instance.
+ /// The return value indicates whether the conversion succeeded.
+ ///
+ /// The case-sensitive string representation of the enumeration name or underlying value to convert
+ /// When this method returns, contains an object of type
+ /// whose
+ /// value is represented by if the parse operation succeeds.
+ /// If the parse operation fails, contains the default value of the underlying type
+ /// of . This parameter is passed uninitialized.
+ /// if the value parameter was converted successfully; otherwise, .
+ public static bool TryParse(
+#if NETCOREAPP3_0_OR_GREATER
+ [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
+#endif
+ string? name,
+ out global::MyTestNameSpace.MyEnum value)
+ => TryParse(name, out value, false, false);
+
+ ///
+ /// Converts the string representation of the name or numeric value of
+ /// an to the equivalent instance.
+ /// The return value indicates whether the conversion succeeded.
+ ///
+ /// The string representation of the enumeration name or underlying value to convert
+ /// When this method returns, contains an object of type
+ /// whose
+ /// value is represented by if the parse operation succeeds.
+ /// If the parse operation fails, contains the default value of the underlying type
+ /// of . This parameter is passed uninitialized.
+ /// to read value in case insensitive mode; to read value in case sensitive mode.
+ /// if the value parameter was converted successfully; otherwise, .
+ public static bool TryParse(
+#if NETCOREAPP3_0_OR_GREATER
+ [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
+#endif
+ string? name,
+ out global::MyTestNameSpace.MyEnum value,
+ bool ignoreCase)
+ => TryParse(name, out value, ignoreCase, false);
+
+ ///
+ /// Converts the string representation of the name or numeric value of
+ /// an to the equivalent instance.
+ /// The return value indicates whether the conversion succeeded.
+ ///
+ /// The string representation of the enumeration name or underlying value to convert
+ /// When this method returns, contains an object of type
+ /// whose
+ /// value is represented by if the parse operation succeeds.
+ /// If the parse operation fails, contains the default value of the underlying type
+ /// of . This parameter is passed uninitialized.
+ /// to read value in case insensitive mode; to read value in case sensitive mode.
+ /// If ,
+ /// considers the value included in attribute
+ /// when parsing, otherwise only considers the member names.
+ /// if the value parameter was converted successfully; otherwise, .
+ public static bool TryParse(
+#if NETCOREAPP3_0_OR_GREATER
+ [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
+#endif
+ string? name,
+ out global::MyTestNameSpace.MyEnum value,
+ bool ignoreCase,
+ bool allowMatchingMetadataAttribute)
+ => ignoreCase
+ ? TryParseIgnoreCase(name, out value, allowMatchingMetadataAttribute)
+ : TryParseWithCase(name, out value, allowMatchingMetadataAttribute);
+
+ private static bool TryParseIgnoreCase(
+#if NETCOREAPP3_0_OR_GREATER
+ [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
+#endif
+ string? name,
+ out global::MyTestNameSpace.MyEnum value,
+ bool allowMatchingMetadataAttribute)
+ {
+ switch (name)
+ {
+ case string s when s.Equals(nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.OrdinalIgnoreCase):
+ value = global::MyTestNameSpace.MyEnum.First;
+ return true;
+ case string s when s.Equals(nameof(global::MyTestNameSpace.MyEnum.Second), global::System.StringComparison.OrdinalIgnoreCase):
+ value = global::MyTestNameSpace.MyEnum.Second;
+ return true;
+ case string s when s.Equals(nameof(global::MyTestNameSpace.MyEnum.Third), global::System.StringComparison.OrdinalIgnoreCase):
+ value = global::MyTestNameSpace.MyEnum.Third;
+ return true;
+ case string s when s.Equals(nameof(global::MyTestNameSpace.MyEnum.Fourth), global::System.StringComparison.OrdinalIgnoreCase):
+ value = global::MyTestNameSpace.MyEnum.Fourth;
+ return true;
+ case string s when int.TryParse(name, out var val):
+ value = (global::MyTestNameSpace.MyEnum)val;
+ return true;
+ default:
+ value = default;
+ return false;
+ }
+ }
+
+ private static bool TryParseWithCase(
+#if NETCOREAPP3_0_OR_GREATER
+ [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
+#endif
+ string? name,
+ out global::MyTestNameSpace.MyEnum value,
+ bool allowMatchingMetadataAttribute)
+ {
+ switch (name)
+ {
+ case nameof(global::MyTestNameSpace.MyEnum.First):
+ value = global::MyTestNameSpace.MyEnum.First;
+ return true;
+ case nameof(global::MyTestNameSpace.MyEnum.Second):
+ value = global::MyTestNameSpace.MyEnum.Second;
+ return true;
+ case nameof(global::MyTestNameSpace.MyEnum.Third):
+ value = global::MyTestNameSpace.MyEnum.Third;
+ return true;
+ case nameof(global::MyTestNameSpace.MyEnum.Fourth):
+ value = global::MyTestNameSpace.MyEnum.Fourth;
+ return true;
+ case string s when int.TryParse(name, out var val):
+ value = (global::MyTestNameSpace.MyEnum)val;
+ return true;
+ default:
+ value = default;
+ return false;
+ }
+ }
+
+#if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0
+ ///
+ /// Converts the string representation of the name or numeric value of
+ /// an to the equivalent instance.
+ ///
+ /// The case-sensitive string representation of the enumeration name or underlying value to convert
+ /// An object of type whose
+ /// value is represented by
+ public static global::MyTestNameSpace.MyEnum Parse(
+#if NETCOREAPP3_0_OR_GREATER
+ [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
+#endif
+ in global::System.ReadOnlySpan name)
+ => TryParse(name, out var value, false, false) ? value : ThrowValueNotFound(name.ToString());
+
+ ///
+ /// Converts the string representation of the name or numeric value of
+ /// an to the equivalent instance.
+ ///
+ /// The case-sensitive string representation of the enumeration name or underlying value to convert
+ /// to read value in case insensitive mode; to read value in case sensitive mode.
+ /// An object of type whose
+ /// value is represented by
+ public static global::MyTestNameSpace.MyEnum Parse(
+#if NETCOREAPP3_0_OR_GREATER
+ [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
+#endif
+ in global::System.ReadOnlySpan name,
+ bool ignoreCase)
+ => TryParse(name, out var value, ignoreCase, false) ? value : ThrowValueNotFound(name.ToString());
+
+ ///
+ /// Converts the string representation of the name or numeric value of
+ /// an to the equivalent instance.
+ ///
+ /// The case-sensitive string representation of the enumeration name or underlying value to convert
+ /// to read value in case insensitive mode; to read value in case sensitive mode.
+ /// If , considers the value included in
+ /// attribute when parsing, otherwise only considers the member names.
+ /// An object of type whose
+ /// value is represented by
+ public static global::MyTestNameSpace.MyEnum Parse(
+#if NETCOREAPP3_0_OR_GREATER
+ [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
+#endif
+ in global::System.ReadOnlySpan name,
+ bool ignoreCase,
+ bool allowMatchingMetadataAttribute)
+ => TryParse(name, out var value, ignoreCase, allowMatchingMetadataAttribute) ? value : ThrowValueNotFound(name.ToString());
+
+ ///
+ /// Converts the span representation of the name or numeric value of
+ /// an to the equivalent instance.
+ /// The return value indicates whether the conversion succeeded.
+ ///
+ /// The span representation of the enumeration name or underlying value to convert
+ /// When this method returns, contains an object of type
+ /// whose
+ /// value is represented by if the parse operation succeeds.
+ /// If the parse operation fails, contains the default value of the underlying type
+ /// of . This parameter is passed uninitialized.
+ /// if the value parameter was converted successfully; otherwise, .
+ public static bool TryParse(
+#if NETCOREAPP3_0_OR_GREATER
+ [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
+#endif
+ in global::System.ReadOnlySpan name,
+ out global::MyTestNameSpace.MyEnum value)
+ => TryParse(name, out value, false, false);
+
+ ///
+ /// Converts the span representation of the name or numeric value of
+ /// an to the equivalent instance.
+ /// The return value indicates whether the conversion succeeded.
+ ///
+ /// The span representation of the enumeration name or underlying value to convert
+ /// When this method returns, contains an object of type
+ /// whose
+ /// value is represented by if the parse operation succeeds.
+ /// If the parse operation fails, contains the default value of the underlying type
+ /// of . This parameter is passed uninitialized.
+ /// to read value in case insensitive mode; to read value in case sensitive mode.
+ /// if the value parameter was converted successfully; otherwise, .
+ public static bool TryParse(
+#if NETCOREAPP3_0_OR_GREATER
+ [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
+#endif
+ in global::System.ReadOnlySpan name,
+ out global::MyTestNameSpace.MyEnum value,
+ bool ignoreCase)
+ => TryParse(name, out value, ignoreCase, false);
+
+ ///
+ /// Converts the span representation of the name or numeric value of
+ /// an to the equivalent instance.
+ /// The return value indicates whether the conversion succeeded.
+ ///
+ /// The span representation of the enumeration name or underlying value to convert
+ /// When this method returns, contains an object of type
+ /// whose
+ /// value is represented by if the parse operation succeeds.
+ /// If the parse operation fails, contains the default value of the underlying type
+ /// of . This parameter is passed uninitialized.
+ /// to read value in case insensitive mode; to read value in case sensitive mode.
+ /// If , considers the value included in
+ /// attribute when parsing, otherwise only considers the member names.
+ /// if the value parameter was converted successfully; otherwise, .
+ public static bool TryParse(
+#if NETCOREAPP3_0_OR_GREATER
+ [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
+#endif
+ in global::System.ReadOnlySpan name,
+ out global::MyTestNameSpace.MyEnum result,
+ bool ignoreCase,
+ bool allowMatchingMetadataAttribute)
+ => ignoreCase
+ ? TryParseIgnoreCase(in name, out result, allowMatchingMetadataAttribute)
+ : TryParseWithCase(in name, out result, allowMatchingMetadataAttribute);
+
+ private static bool TryParseIgnoreCase(
+#if NETCOREAPP3_0_OR_GREATER
+ [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
+#endif
+ in global::System.ReadOnlySpan name,
+ out global::MyTestNameSpace.MyEnum result,
+ bool allowMatchingMetadataAttribute)
+ {
+ switch (name)
+ {
+ case global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.OrdinalIgnoreCase):
+ result = global::MyTestNameSpace.MyEnum.First;
+ return true;
+ case global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Second), global::System.StringComparison.OrdinalIgnoreCase):
+ result = global::MyTestNameSpace.MyEnum.Second;
+ return true;
+ case global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Third), global::System.StringComparison.OrdinalIgnoreCase):
+ result = global::MyTestNameSpace.MyEnum.Third;
+ return true;
+ case global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Fourth), global::System.StringComparison.OrdinalIgnoreCase):
+ result = global::MyTestNameSpace.MyEnum.Fourth;
+ return true;
+ case global::System.ReadOnlySpan current when int.TryParse(name, out var numericResult):
+ result = (global::MyTestNameSpace.MyEnum)numericResult;
+ return true;
+ default:
+ result = default;
+ return false;
+ }
+ }
+
+ private static bool TryParseWithCase(
+#if NETCOREAPP3_0_OR_GREATER
+ [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
+#endif
+ in global::System.ReadOnlySpan name,
+ out global::MyTestNameSpace.MyEnum result,
+ bool allowMatchingMetadataAttribute)
+ {
+ switch (name)
+ {
+ case global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.Ordinal):
+ result = global::MyTestNameSpace.MyEnum.First;
+ return true;
+ case global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Second), global::System.StringComparison.Ordinal):
+ result = global::MyTestNameSpace.MyEnum.Second;
+ return true;
+ case global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Third), global::System.StringComparison.Ordinal):
+ result = global::MyTestNameSpace.MyEnum.Third;
+ return true;
+ case global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Fourth), global::System.StringComparison.Ordinal):
+ result = global::MyTestNameSpace.MyEnum.Fourth;
+ return true;
+ case global::System.ReadOnlySpan current when int.TryParse(name, out var numericResult):
+ result = (global::MyTestNameSpace.MyEnum)numericResult;
+ return true;
+ default:
+ result = default;
+ return false;
+ }
+ }
+#endif
+
+ ///
+ /// Retrieves an array of the values of the members defined in
+ /// .
+ /// Note that this returns a new array with every invocation, so
+ /// should be cached if appropriate.
+ ///
+ /// An array of the values defined in
+ public static global::MyTestNameSpace.MyEnum[] GetValues()
+ {
+ return new[]
+ {
+ global::MyTestNameSpace.MyEnum.First,
+ global::MyTestNameSpace.MyEnum.Second,
+ global::MyTestNameSpace.MyEnum.Third,
+ global::MyTestNameSpace.MyEnum.Fourth,
+ };
+ }
+
+ ///
+ /// Retrieves an array of the underlying-values of the members defined in
+ /// .
+ /// Note that this returns a new array with every invocation, so
+ /// should be cached if appropriate.
+ ///
+ /// An array of the underlying-values defined in
+ public static int[] GetValuesAsUnderlyingType()
+ {
+ return new[]
+ {
+ (int) global::MyTestNameSpace.MyEnum.First,
+ (int) global::MyTestNameSpace.MyEnum.Second,
+ (int) global::MyTestNameSpace.MyEnum.Third,
+ (int) global::MyTestNameSpace.MyEnum.Fourth,
+ };
+ }
+
+ ///
+ /// Retrieves an array of the names of the members defined in
+ /// .
+ /// Note that this returns a new array with every invocation, so
+ /// should be cached if appropriate.
+ ///
+ /// An array of the names of the members defined in
+ public static string[] GetNames()
+ {
+ return new[]
+ {
+ nameof(global::MyTestNameSpace.MyEnum.First),
+ nameof(global::MyTestNameSpace.MyEnum.Second),
+ nameof(global::MyTestNameSpace.MyEnum.Third),
+ nameof(global::MyTestNameSpace.MyEnum.Fourth),
+ };
+ }
+ }
+#pragma warning restore CS0612 // Ignore usages of obsolete members or enums
+#pragma warning restore CS0618 // Ignore usages of obsolete members or enums
+}
\ No newline at end of file
diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateExternalEnumExtensionsWithCustomName.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateExternalEnumExtensionsWithCustomName.verified.txt
index f8fe225..8d08534 100644
--- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateExternalEnumExtensionsWithCustomName.verified.txt
+++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateExternalEnumExtensionsWithCustomName.verified.txt
@@ -25,20 +25,6 @@ namespace System
///
public const int Length = 3;
- ///
- /// Returns the string representation of the value.
- /// If the attribute is decorated with a [Display] or [Description]attribute, then
- /// uses the provided value. Otherwise uses the name of the member, equivalent to
- /// calling ToString() on .
- ///
- /// The value to retrieve the string value for
- /// If uses the value provided in the
- /// [Display] or [Description]attribute as the string representation of the member.
- /// If , always uses the name of the member, the same as if ToString() was called.
- /// The string representation of the value
- public static string ToStringFast(this global::System.DateTimeKind value, bool useMetadataAttributes)
- => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast();
-
///
/// Returns the string representation of the value.
/// Directly equivalent to calling ToString() on .
@@ -54,6 +40,20 @@ namespace System
_ => value.AsUnderlyingType().ToString(),
};
+ ///
+ /// Returns the string representation of the value.
+ /// If the member is decorated with the attribute
+ /// then that value is returned. Otherwise returns uses the name of the member,
+ /// equivalent to calling ToString() on .
+ ///
+ /// The value to retrieve the string value for
+ /// If uses the value provided in the
+ /// attribute as the string representation of the member.
+ /// If , always uses the name of the member, the same as if ToString() was called.
+ /// The string representation of the value
+ public static string ToStringFast(this global::System.DateTimeKind value, bool useMetadataAttributes)
+ => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast();
+
private static string ToStringFastWithMetadata(this global::System.DateTimeKind value)
=> value.ToStringFast();
@@ -86,27 +86,28 @@ namespace System
///
/// The name to check if it's defined
/// if a member with the name exists in the enumeration, otherwise
- public static bool IsDefined(string name) => IsDefined(name, allowMatchingMetadataAttribute: false);
+ public static bool IsDefined(string name)
+ => name switch
+ {
+ nameof(global::System.DateTimeKind.Unspecified) => true,
+ nameof(global::System.DateTimeKind.Utc) => true,
+ nameof(global::System.DateTimeKind.Local) => true,
+ _ => false,
+ };
///
/// Returns a boolean telling whether an enum with the given name exists in the enumeration,
- /// or if a member decorated with a [Display] attribute
+ /// or if a member decorated with
/// with the required name exists.
///
/// The name to check if it's defined
- /// If , considers the value of metadata attributes,otherwise ignores them
+ /// If ,
+ /// considers the value of
+ /// instead of the member name, otherwise ignores them
/// if a member with the name exists in the enumeration, or a member is decorated
/// with a [Display] attribute with the name, otherwise
public static bool IsDefined(string name, bool allowMatchingMetadataAttribute)
- {
- return name switch
- {
- nameof(global::System.DateTimeKind.Unspecified) => true,
- nameof(global::System.DateTimeKind.Utc) => true,
- nameof(global::System.DateTimeKind.Local) => true,
- _ => false,
- };
- }
+ => IsDefined(name);
#if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0
///
@@ -114,28 +115,26 @@ namespace System
///
/// The name to check if it's defined
/// if a member with the name exists in the enumeration, otherwise
- public static bool IsDefined(in global::System.ReadOnlySpan name) => IsDefined(name, allowMatchingMetadataAttribute: false);
+ public static bool IsDefined(in global::System.ReadOnlySpan name)
+ => name switch
+ {
+ global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::System.DateTimeKind.Unspecified), global::System.StringComparison.Ordinal) => true,
+ global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::System.DateTimeKind.Utc), global::System.StringComparison.Ordinal) => true,
+ global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::System.DateTimeKind.Local), global::System.StringComparison.Ordinal) => true,
+ _ => false,
+ };
///
/// Returns a boolean telling whether an enum with the given name exists in the enumeration,
- /// or optionally if a member decorated with a [Display] attribute
- /// with the required name exists.
- /// Slower then the overload, but doesn't allocate memory./>
+ /// or optionally if a member decorated with
+ /// exists. Slower then the overload, but doesn't allocate memory./>
///
/// The name to check if it's defined
/// If , considers the value of metadata attributes,otherwise ignores them
/// if a member with the name exists in the enumeration, or a member is decorated
/// with a [Display] attribute with the name, otherwise
public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute)
- {
- return name switch
- {
- global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::System.DateTimeKind.Unspecified), global::System.StringComparison.Ordinal) => true,
- global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::System.DateTimeKind.Utc), global::System.StringComparison.Ordinal) => true,
- global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::System.DateTimeKind.Local), global::System.StringComparison.Ordinal) => true,
- _ => false,
- };
- }
+ => IsDefined(name);
#endif
///
@@ -247,8 +246,9 @@ namespace System
/// If the parse operation fails, contains the default value of the underlying type
/// of . This parameter is passed uninitialized.
/// to read value in case insensitive mode; to read value in case sensitive mode.
- /// If , considers the value included in metadata attributes such as
- /// [Display] attribute when parsing, otherwise only considers the member names.
+ /// If ,
+ /// considers the value included in attribute
+ /// when parsing, otherwise only considers the member names.
/// if the value parameter was converted successfully; otherwise, .
public static bool TryParse(
#if NETCOREAPP3_0_OR_GREATER
@@ -270,7 +270,6 @@ namespace System
out global::System.DateTimeKind value,
bool allowMatchingMetadataAttribute)
{
-
switch (name)
{
case string s when s.Equals(nameof(global::System.DateTimeKind.Unspecified), global::System.StringComparison.OrdinalIgnoreCase):
@@ -299,7 +298,6 @@ namespace System
out global::System.DateTimeKind value,
bool allowMatchingMetadataAttribute)
{
-
switch (name)
{
case nameof(global::System.DateTimeKind.Unspecified):
@@ -357,8 +355,8 @@ namespace System
///
/// The case-sensitive string representation of the enumeration name or underlying value to convert
/// to read value in case insensitive mode; to read value in case sensitive mode.
- /// If , considers the value included in metadata attributes such as
- /// [Display] attribute when parsing, otherwise only considers the member names.
+ /// If , considers the value included in
+ /// attribute when parsing, otherwise only considers the member names.
/// An object of type whose
/// value is represented by
public static global::System.DateTimeKind Parse(
@@ -424,8 +422,8 @@ namespace System
/// If the parse operation fails, contains the default value of the underlying type
/// of . This parameter is passed uninitialized.
/// to read value in case insensitive mode; to read value in case sensitive mode.
- /// If , considers the value included in metadata attributes such as
- /// [Display] attribute when parsing, otherwise only considers the member names.
+ /// If , considers the value included in
+ /// attribute when parsing, otherwise only considers the member names.
/// if the value parameter was converted successfully; otherwise, .
public static bool TryParse(
#if NETCOREAPP3_0_OR_GREATER
diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateExternalEnumExtensionsWithCustomNamespace.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateExternalEnumExtensionsWithCustomNamespace.verified.txt
index 3d85a85..9881f97 100644
--- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateExternalEnumExtensionsWithCustomNamespace.verified.txt
+++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateExternalEnumExtensionsWithCustomNamespace.verified.txt
@@ -1,4 +1,4 @@
-//------------------------------------------------------------------------------
+//------------------------------------------------------------------------------
//
// This code was generated by the NetEscapades.EnumGenerators source generator
//
@@ -25,20 +25,6 @@ namespace A.B
///
public const int Length = 3;
- ///
- /// Returns the string representation of the value.
- /// If the attribute is decorated with a [Display] or [Description]attribute, then
- /// uses the provided value. Otherwise uses the name of the member, equivalent to
- /// calling ToString() on .
- ///
- /// The value to retrieve the string value for
- /// If uses the value provided in the
- /// [Display] or [Description]attribute as the string representation of the member.
- /// If , always uses the name of the member, the same as if ToString() was called.
- /// The string representation of the value
- public static string ToStringFast(this global::System.DateTimeKind value, bool useMetadataAttributes)
- => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast();
-
///
/// Returns the string representation of the value.
/// Directly equivalent to calling ToString() on .
@@ -54,6 +40,20 @@ namespace A.B
_ => value.AsUnderlyingType().ToString(),
};
+ ///
+ /// Returns the string representation of the value.
+ /// If the member is decorated with the attribute
+ /// then that value is returned. Otherwise returns uses the name of the member,
+ /// equivalent to calling ToString() on .
+ ///
+ /// The value to retrieve the string value for
+ /// If uses the value provided in the
+ /// attribute as the string representation of the member.
+ /// If , always uses the name of the member, the same as if ToString() was called.
+ /// The string representation of the value
+ public static string ToStringFast(this global::System.DateTimeKind value, bool useMetadataAttributes)
+ => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast();
+
private static string ToStringFastWithMetadata(this global::System.DateTimeKind value)
=> value.ToStringFast();
@@ -86,27 +86,28 @@ namespace A.B
///
/// The name to check if it's defined
/// if a member with the name exists in the enumeration, otherwise
- public static bool IsDefined(string name) => IsDefined(name, allowMatchingMetadataAttribute: false);
+ public static bool IsDefined(string name)
+ => name switch
+ {
+ nameof(global::System.DateTimeKind.Unspecified) => true,
+ nameof(global::System.DateTimeKind.Utc) => true,
+ nameof(global::System.DateTimeKind.Local) => true,
+ _ => false,
+ };
///
/// Returns a boolean telling whether an enum with the given name exists in the enumeration,
- /// or if a member decorated with a [Display] attribute
+ /// or if a member decorated with
/// with the required name exists.
///
/// The name to check if it's defined
- /// If , considers the value of metadata attributes,otherwise ignores them
+ /// If ,
+ /// considers the value of
+ /// instead of the member name, otherwise ignores them
/// if a member with the name exists in the enumeration, or a member is decorated
/// with a [Display] attribute with the name, otherwise
public static bool IsDefined(string name, bool allowMatchingMetadataAttribute)
- {
- return name switch
- {
- nameof(global::System.DateTimeKind.Unspecified) => true,
- nameof(global::System.DateTimeKind.Utc) => true,
- nameof(global::System.DateTimeKind.Local) => true,
- _ => false,
- };
- }
+ => IsDefined(name);
#if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0
///
@@ -114,28 +115,26 @@ namespace A.B
///
/// The name to check if it's defined
/// if a member with the name exists in the enumeration, otherwise
- public static bool IsDefined(in global::System.ReadOnlySpan name) => IsDefined(name, allowMatchingMetadataAttribute: false);
+ public static bool IsDefined(in global::System.ReadOnlySpan name)
+ => name switch
+ {
+ global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::System.DateTimeKind.Unspecified), global::System.StringComparison.Ordinal) => true,
+ global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::System.DateTimeKind.Utc), global::System.StringComparison.Ordinal) => true,
+ global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::System.DateTimeKind.Local), global::System.StringComparison.Ordinal) => true,
+ _ => false,
+ };
///
/// Returns a boolean telling whether an enum with the given name exists in the enumeration,
- /// or optionally if a member decorated with a [Display] attribute
- /// with the required name exists.
- /// Slower then the overload, but doesn't allocate memory./>
+ /// or optionally if a member decorated with
+ /// exists. Slower then the overload, but doesn't allocate memory./>
///
/// The name to check if it's defined
/// If , considers the value of metadata attributes,otherwise ignores them
/// if a member with the name exists in the enumeration, or a member is decorated
/// with a [Display] attribute with the name, otherwise
public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute)
- {
- return name switch
- {
- global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::System.DateTimeKind.Unspecified), global::System.StringComparison.Ordinal) => true,
- global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::System.DateTimeKind.Utc), global::System.StringComparison.Ordinal) => true,
- global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::System.DateTimeKind.Local), global::System.StringComparison.Ordinal) => true,
- _ => false,
- };
- }
+ => IsDefined(name);
#endif
///
@@ -247,8 +246,9 @@ namespace A.B
/// If the parse operation fails, contains the default value of the underlying type
/// of . This parameter is passed uninitialized.
/// to read value in case insensitive mode; to read value in case sensitive mode.
- /// If , considers the value included in metadata attributes such as
- /// [Display] attribute when parsing, otherwise only considers the member names.
+ /// If ,
+ /// considers the value included in attribute
+ /// when parsing, otherwise only considers the member names.
/// if the value parameter was converted successfully; otherwise, .
public static bool TryParse(
#if NETCOREAPP3_0_OR_GREATER
@@ -270,7 +270,6 @@ namespace A.B
out global::System.DateTimeKind value,
bool allowMatchingMetadataAttribute)
{
-
switch (name)
{
case string s when s.Equals(nameof(global::System.DateTimeKind.Unspecified), global::System.StringComparison.OrdinalIgnoreCase):
@@ -299,7 +298,6 @@ namespace A.B
out global::System.DateTimeKind value,
bool allowMatchingMetadataAttribute)
{
-
switch (name)
{
case nameof(global::System.DateTimeKind.Unspecified):
@@ -357,8 +355,8 @@ namespace A.B
///
/// The case-sensitive string representation of the enumeration name or underlying value to convert
/// to read value in case insensitive mode; to read value in case sensitive mode.
- /// If , considers the value included in metadata attributes such as
- /// [Display] attribute when parsing, otherwise only considers the member names.
+ /// If , considers the value included in
+ /// attribute when parsing, otherwise only considers the member names.
/// An object of type whose
/// value is represented by
public static global::System.DateTimeKind Parse(
@@ -424,8 +422,8 @@ namespace A.B
/// If the parse operation fails, contains the default value of the underlying type
/// of . This parameter is passed uninitialized.
/// to read value in case insensitive mode; to read value in case sensitive mode.
- /// If , considers the value included in metadata attributes such as
- /// [Display] attribute when parsing, otherwise only considers the member names.
+ /// If , considers the value included in
+ /// attribute when parsing, otherwise only considers the member names.
/// if the value parameter was converted successfully; otherwise, .
public static bool TryParse(
#if NETCOREAPP3_0_OR_GREATER
diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateExternalEnumExtensionsWithCustomNamespaceAndName.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateExternalEnumExtensionsWithCustomNamespaceAndName.verified.txt
index 973e79d..9c0eaec 100644
--- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateExternalEnumExtensionsWithCustomNamespaceAndName.verified.txt
+++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateExternalEnumExtensionsWithCustomNamespaceAndName.verified.txt
@@ -25,20 +25,6 @@ namespace A.B
///
public const int Length = 3;
- ///
- /// Returns the string representation of the value.
- /// If the attribute is decorated with a [Display] or [Description]attribute, then
- /// uses the provided value. Otherwise uses the name of the member, equivalent to
- /// calling ToString() on .
- ///
- /// The value to retrieve the string value for
- /// If uses the value provided in the
- /// [Display] or [Description]attribute as the string representation of the member.
- /// If , always uses the name of the member, the same as if ToString() was called.
- /// The string representation of the value
- public static string ToStringFast(this global::System.DateTimeKind value, bool useMetadataAttributes)
- => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast();
-
///
/// Returns the string representation of the value.
/// Directly equivalent to calling ToString() on .
@@ -54,6 +40,20 @@ namespace A.B
_ => value.AsUnderlyingType().ToString(),
};
+ ///
+ /// Returns the string representation of the value.
+ /// If the member is decorated with the attribute
+ /// then that value is returned. Otherwise returns uses the name of the member,
+ /// equivalent to calling ToString() on .
+ ///
+ /// The value to retrieve the string value for
+ /// If uses the value provided in the
+ /// attribute as the string representation of the member.
+ /// If , always uses the name of the member, the same as if ToString() was called.
+ /// The string representation of the value
+ public static string ToStringFast(this global::System.DateTimeKind value, bool useMetadataAttributes)
+ => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast();
+
private static string ToStringFastWithMetadata(this global::System.DateTimeKind value)
=> value.ToStringFast();
@@ -86,27 +86,28 @@ namespace A.B
///
/// The name to check if it's defined
/// if a member with the name exists in the enumeration, otherwise
- public static bool IsDefined(string name) => IsDefined(name, allowMatchingMetadataAttribute: false);
+ public static bool IsDefined(string name)
+ => name switch
+ {
+ nameof(global::System.DateTimeKind.Unspecified) => true,
+ nameof(global::System.DateTimeKind.Utc) => true,
+ nameof(global::System.DateTimeKind.Local) => true,
+ _ => false,
+ };
///
/// Returns a boolean telling whether an enum with the given name exists in the enumeration,
- /// or if a member decorated with a [Display] attribute
+ /// or if a member decorated with
/// with the required name exists.
///
/// The name to check if it's defined
- /// If , considers the value of metadata attributes,otherwise ignores them
+ /// If ,
+ /// considers the value of
+ /// instead of the member name, otherwise ignores them
/// if a member with the name exists in the enumeration, or a member is decorated
/// with a [Display] attribute with the name, otherwise
public static bool IsDefined(string name, bool allowMatchingMetadataAttribute)
- {
- return name switch
- {
- nameof(global::System.DateTimeKind.Unspecified) => true,
- nameof(global::System.DateTimeKind.Utc) => true,
- nameof(global::System.DateTimeKind.Local) => true,
- _ => false,
- };
- }
+ => IsDefined(name);
#if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0
///
@@ -114,28 +115,26 @@ namespace A.B
///
/// The name to check if it's defined
/// if a member with the name exists in the enumeration, otherwise
- public static bool IsDefined(in global::System.ReadOnlySpan name) => IsDefined(name, allowMatchingMetadataAttribute: false);
+ public static bool IsDefined(in global::System.ReadOnlySpan name)
+ => name switch
+ {
+ global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::System.DateTimeKind.Unspecified), global::System.StringComparison.Ordinal) => true,
+ global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::System.DateTimeKind.Utc), global::System.StringComparison.Ordinal) => true,
+ global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::System.DateTimeKind.Local), global::System.StringComparison.Ordinal) => true,
+ _ => false,
+ };
///
/// Returns a boolean telling whether an enum with the given name exists in the enumeration,
- /// or optionally if a member decorated with a [Display] attribute
- /// with the required name exists.
- /// Slower then the overload, but doesn't allocate memory./>
+ /// or optionally if a member decorated with
+ /// exists. Slower then the overload, but doesn't allocate memory./>
///
/// The name to check if it's defined
/// If , considers the value of metadata attributes,otherwise ignores them
/// if a member with the name exists in the enumeration, or a member is decorated
/// with a [Display] attribute with the name, otherwise
public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute)
- {
- return name switch
- {
- global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::System.DateTimeKind.Unspecified), global::System.StringComparison.Ordinal) => true,
- global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::System.DateTimeKind.Utc), global::System.StringComparison.Ordinal) => true,
- global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::System.DateTimeKind.Local), global::System.StringComparison.Ordinal) => true,
- _ => false,
- };
- }
+ => IsDefined(name);
#endif
///
@@ -247,8 +246,9 @@ namespace A.B
/// If the parse operation fails, contains the default value of the underlying type
/// of . This parameter is passed uninitialized.
/// to read value in case insensitive mode; to read value in case sensitive mode.
- /// If , considers the value included in metadata attributes such as
- /// [Display] attribute when parsing, otherwise only considers the member names.
+ /// If ,
+ /// considers the value included in attribute
+ /// when parsing, otherwise only considers the member names.
/// if the value parameter was converted successfully; otherwise, .
public static bool TryParse(
#if NETCOREAPP3_0_OR_GREATER
@@ -270,7 +270,6 @@ namespace A.B
out global::System.DateTimeKind value,
bool allowMatchingMetadataAttribute)
{
-
switch (name)
{
case string s when s.Equals(nameof(global::System.DateTimeKind.Unspecified), global::System.StringComparison.OrdinalIgnoreCase):
@@ -299,7 +298,6 @@ namespace A.B
out global::System.DateTimeKind value,
bool allowMatchingMetadataAttribute)
{
-
switch (name)
{
case nameof(global::System.DateTimeKind.Unspecified):
@@ -357,8 +355,8 @@ namespace A.B
///
/// The case-sensitive string representation of the enumeration name or underlying value to convert
/// to read value in case insensitive mode; to read value in case sensitive mode.
- /// If , considers the value included in metadata attributes such as
- /// [Display] attribute when parsing, otherwise only considers the member names.
+ /// If , considers the value included in
+ /// attribute when parsing, otherwise only considers the member names.
/// An object of type whose
/// value is represented by
public static global::System.DateTimeKind Parse(
@@ -424,8 +422,8 @@ namespace A.B
/// If the parse operation fails, contains the default value of the underlying type
/// of . This parameter is passed uninitialized.
/// to read value in case insensitive mode; to read value in case sensitive mode.
- /// If , considers the value included in metadata attributes such as
- /// [Display] attribute when parsing, otherwise only considers the member names.
+ /// If , considers the value included in
+ /// attribute when parsing, otherwise only considers the member names.
/// if the value parameter was converted successfully; otherwise, .
public static bool TryParse(
#if NETCOREAPP3_0_OR_GREATER
diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateForExternalEnum.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateForExternalEnum.verified.txt
index 6b27c51..4623763 100644
--- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateForExternalEnum.verified.txt
+++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateForExternalEnum.verified.txt
@@ -1,4 +1,4 @@
-//------------------------------------------------------------------------------
+//------------------------------------------------------------------------------
//
// This code was generated by the NetEscapades.EnumGenerators source generator
//
@@ -25,20 +25,6 @@ namespace System
///
public const int Length = 6;
- ///
- /// Returns the string representation of the value.
- /// If the attribute is decorated with a [Display] or [Description]attribute, then
- /// uses the provided value. Otherwise uses the name of the member, equivalent to
- /// calling ToString() on .
- ///
- /// The value to retrieve the string value for
- /// If uses the value provided in the
- /// [Display] or [Description]attribute as the string representation of the member.
- /// If , always uses the name of the member, the same as if ToString() was called.
- /// The string representation of the value
- public static string ToStringFast(this global::System.StringComparison value, bool useMetadataAttributes)
- => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast();
-
///
/// Returns the string representation of the value.
/// Directly equivalent to calling ToString() on .
@@ -57,6 +43,20 @@ namespace System
_ => value.AsUnderlyingType().ToString(),
};
+ ///
+ /// Returns the string representation of the value.
+ /// If the member is decorated with the attribute
+ /// then that value is returned. Otherwise returns uses the name of the member,
+ /// equivalent to calling ToString() on .
+ ///
+ /// The value to retrieve the string value for
+ /// If uses the value provided in the
+ /// attribute as the string representation of the member.
+ /// If , always uses the name of the member, the same as if ToString() was called.
+ /// The string representation of the value
+ public static string ToStringFast(this global::System.StringComparison value, bool useMetadataAttributes)
+ => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast();
+
private static string ToStringFastWithMetadata(this global::System.StringComparison value)
=> value.ToStringFast();
@@ -92,30 +92,31 @@ namespace System
///
/// The name to check if it's defined
/// if a member with the name exists in the enumeration, otherwise
- public static bool IsDefined(string name) => IsDefined(name, allowMatchingMetadataAttribute: false);
+ public static bool IsDefined(string name)
+ => name switch
+ {
+ nameof(global::System.StringComparison.CurrentCulture) => true,
+ nameof(global::System.StringComparison.CurrentCultureIgnoreCase) => true,
+ nameof(global::System.StringComparison.InvariantCulture) => true,
+ nameof(global::System.StringComparison.InvariantCultureIgnoreCase) => true,
+ nameof(global::System.StringComparison.Ordinal) => true,
+ nameof(global::System.StringComparison.OrdinalIgnoreCase) => true,
+ _ => false,
+ };
///
/// Returns a boolean telling whether an enum with the given name exists in the enumeration,
- /// or if a member decorated with a [Display] attribute
+ /// or if a member decorated with
/// with the required name exists.
///
/// The name to check if it's defined
- /// If , considers the value of metadata attributes,otherwise ignores them
+ /// If ,
+ /// considers the value of
+ /// instead of the member name, otherwise ignores them
/// if a member with the name exists in the enumeration, or a member is decorated
/// with a [Display] attribute with the name, otherwise
public static bool IsDefined(string name, bool allowMatchingMetadataAttribute)
- {
- return name switch
- {
- nameof(global::System.StringComparison.CurrentCulture) => true,
- nameof(global::System.StringComparison.CurrentCultureIgnoreCase) => true,
- nameof(global::System.StringComparison.InvariantCulture) => true,
- nameof(global::System.StringComparison.InvariantCultureIgnoreCase) => true,
- nameof(global::System.StringComparison.Ordinal) => true,
- nameof(global::System.StringComparison.OrdinalIgnoreCase) => true,
- _ => false,
- };
- }
+ => IsDefined(name);
#if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0
///
@@ -123,21 +124,8 @@ namespace System
///
/// The name to check if it's defined
/// if a member with the name exists in the enumeration, otherwise
- public static bool IsDefined(in global::System.ReadOnlySpan name) => IsDefined(name, allowMatchingMetadataAttribute: false);
-
- ///
- /// Returns a boolean telling whether an enum with the given name exists in the enumeration,
- /// or optionally if a member decorated with a [Display] attribute
- /// with the required name exists.
- /// Slower then the overload, but doesn't allocate memory./>
- ///
- /// The name to check if it's defined
- /// If , considers the value of metadata attributes,otherwise ignores them
- /// if a member with the name exists in the enumeration, or a member is decorated
- /// with a [Display] attribute with the name, otherwise
- public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute)
- {
- return name switch
+ public static bool IsDefined(in global::System.ReadOnlySpan name)
+ => name switch
{
global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::System.StringComparison.CurrentCulture), global::System.StringComparison.Ordinal) => true,
global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::System.StringComparison.CurrentCultureIgnoreCase), global::System.StringComparison.Ordinal) => true,
@@ -147,7 +135,18 @@ namespace System
global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::System.StringComparison.OrdinalIgnoreCase), global::System.StringComparison.Ordinal) => true,
_ => false,
};
- }
+
+ ///
+ /// Returns a boolean telling whether an enum with the given name exists in the enumeration,
+ /// or optionally if a member decorated with
+ /// exists. Slower then the overload, but doesn't allocate memory./>
+ ///
+ /// The name to check if it's defined
+ /// If , considers the value of metadata attributes,otherwise ignores them
+ /// if a member with the name exists in the enumeration, or a member is decorated
+ /// with a [Display] attribute with the name, otherwise
+ public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute)
+ => IsDefined(name);
#endif
///
@@ -259,8 +258,9 @@ namespace System
/// If the parse operation fails, contains the default value of the underlying type
/// of . This parameter is passed uninitialized.
/// to read value in case insensitive mode; to read value in case sensitive mode.
- /// If , considers the value included in metadata attributes such as
- /// [Display] attribute when parsing, otherwise only considers the member names.
+ /// If ,
+ /// considers the value included in attribute
+ /// when parsing, otherwise only considers the member names.
/// if the value parameter was converted successfully; otherwise, .
public static bool TryParse(
#if NETCOREAPP3_0_OR_GREATER
@@ -282,7 +282,6 @@ namespace System
out global::System.StringComparison value,
bool allowMatchingMetadataAttribute)
{
-
switch (name)
{
case string s when s.Equals(nameof(global::System.StringComparison.CurrentCulture), global::System.StringComparison.OrdinalIgnoreCase):
@@ -320,7 +319,6 @@ namespace System
out global::System.StringComparison value,
bool allowMatchingMetadataAttribute)
{
-
switch (name)
{
case nameof(global::System.StringComparison.CurrentCulture):
@@ -387,8 +385,8 @@ namespace System
///
/// The case-sensitive string representation of the enumeration name or underlying value to convert
/// to read value in case insensitive mode; to read value in case sensitive mode.
- /// If , considers the value included in metadata attributes such as
- /// [Display] attribute when parsing, otherwise only considers the member names.
+ /// If , considers the value included in
+ /// attribute when parsing, otherwise only considers the member names.
/// An object of type whose
/// value is represented by
public static global::System.StringComparison Parse(
@@ -454,8 +452,8 @@ namespace System
/// If the parse operation fails, contains the default value of the underlying type
/// of . This parameter is passed uninitialized.
/// to read value in case insensitive mode; to read value in case sensitive mode.
- /// If , considers the value included in metadata attributes such as
- /// [Display] attribute when parsing, otherwise only considers the member names.
+ /// If , considers the value included in
+ /// attribute when parsing, otherwise only considers the member names.
/// if the value parameter was converted successfully; otherwise, .
public static bool TryParse(
#if NETCOREAPP3_0_OR_GREATER
diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateForExternalFlagsEnum.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateForExternalFlagsEnum.verified.txt
index f778f85..e1f1dbe 100644
--- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateForExternalFlagsEnum.verified.txt
+++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateForExternalFlagsEnum.verified.txt
@@ -1,4 +1,4 @@
-//------------------------------------------------------------------------------
+//------------------------------------------------------------------------------
//
// This code was generated by the NetEscapades.EnumGenerators source generator
//
@@ -25,20 +25,6 @@ namespace System.IO
///
public const int Length = 6;
- ///
- /// Returns the string representation of the value.
- /// If the attribute is decorated with a [Display] or [Description]attribute, then
- /// uses the provided value. Otherwise uses the name of the member, equivalent to
- /// calling ToString() on .
- ///
- /// The value to retrieve the string value for
- /// If uses the value provided in the
- /// [Display] or [Description]attribute as the string representation of the member.
- /// If , always uses the name of the member, the same as if ToString() was called.
- /// The string representation of the value
- public static string ToStringFast(this global::System.IO.FileShare value, bool useMetadataAttributes)
- => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast();
-
///
/// Returns the string representation of the value.
/// Directly equivalent to calling ToString() on .
@@ -57,6 +43,20 @@ namespace System.IO
_ => value.ToString(),
};
+ ///
+ /// Returns the string representation of the value.
+ /// If the member is decorated with the attribute
+ /// then that value is returned. Otherwise returns uses the name of the member,
+ /// equivalent to calling ToString() on .
+ ///
+ /// The value to retrieve the string value for
+ /// If uses the value provided in the
+ /// attribute as the string representation of the member.
+ /// If , always uses the name of the member, the same as if ToString() was called.
+ /// The string representation of the value
+ public static string ToStringFast(this global::System.IO.FileShare value, bool useMetadataAttributes)
+ => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast();
+
private static string ToStringFastWithMetadata(this global::System.IO.FileShare value)
=> value.ToStringFast();
@@ -104,30 +104,31 @@ namespace System.IO
///
/// The name to check if it's defined
/// if a member with the name exists in the enumeration, otherwise
- public static bool IsDefined(string name) => IsDefined(name, allowMatchingMetadataAttribute: false);
+ public static bool IsDefined(string name)
+ => name switch
+ {
+ nameof(global::System.IO.FileShare.None) => true,
+ nameof(global::System.IO.FileShare.Read) => true,
+ nameof(global::System.IO.FileShare.Write) => true,
+ nameof(global::System.IO.FileShare.ReadWrite) => true,
+ nameof(global::System.IO.FileShare.Delete) => true,
+ nameof(global::System.IO.FileShare.Inheritable) => true,
+ _ => false,
+ };
///
/// Returns a boolean telling whether an enum with the given name exists in the enumeration,
- /// or if a member decorated with a [Display] attribute
+ /// or if a member decorated with
/// with the required name exists.
///
/// The name to check if it's defined
- /// If , considers the value of metadata attributes,otherwise ignores them
+ /// If ,
+ /// considers the value of
+ /// instead of the member name, otherwise ignores them
/// if a member with the name exists in the enumeration, or a member is decorated
/// with a [Display] attribute with the name, otherwise
public static bool IsDefined(string name, bool allowMatchingMetadataAttribute)
- {
- return name switch
- {
- nameof(global::System.IO.FileShare.None) => true,
- nameof(global::System.IO.FileShare.Read) => true,
- nameof(global::System.IO.FileShare.Write) => true,
- nameof(global::System.IO.FileShare.ReadWrite) => true,
- nameof(global::System.IO.FileShare.Delete) => true,
- nameof(global::System.IO.FileShare.Inheritable) => true,
- _ => false,
- };
- }
+ => IsDefined(name);
#if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0
///
@@ -135,21 +136,8 @@ namespace System.IO
///
/// The name to check if it's defined
/// if a member with the name exists in the enumeration, otherwise
- public static bool IsDefined(in global::System.ReadOnlySpan name) => IsDefined(name, allowMatchingMetadataAttribute: false);
-
- ///
- /// Returns a boolean telling whether an enum with the given name exists in the enumeration,
- /// or optionally if a member decorated with a [Display] attribute
- /// with the required name exists.
- /// Slower then the overload, but doesn't allocate memory./>
- ///
- /// The name to check if it's defined
- /// If , considers the value of metadata attributes,otherwise ignores them
- /// if a member with the name exists in the enumeration, or a member is decorated
- /// with a [Display] attribute with the name, otherwise
- public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute)
- {
- return name switch
+ public static bool IsDefined(in global::System.ReadOnlySpan name)
+ => name switch
{
global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::System.IO.FileShare.None), global::System.StringComparison.Ordinal) => true,
global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::System.IO.FileShare.Read), global::System.StringComparison.Ordinal) => true,
@@ -159,7 +147,18 @@ namespace System.IO
global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::System.IO.FileShare.Inheritable), global::System.StringComparison.Ordinal) => true,
_ => false,
};
- }
+
+ ///
+ /// Returns a boolean telling whether an enum with the given name exists in the enumeration,
+ /// or optionally if a member decorated with
+ /// exists. Slower then the overload, but doesn't allocate memory./>
+ ///
+ /// The name to check if it's defined
+ /// If , considers the value of metadata attributes,otherwise ignores them
+ /// if a member with the name exists in the enumeration, or a member is decorated
+ /// with a [Display] attribute with the name, otherwise
+ public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute)
+ => IsDefined(name);
#endif
///
@@ -271,8 +270,9 @@ namespace System.IO
/// If the parse operation fails, contains the default value of the underlying type
/// of . This parameter is passed uninitialized.
/// to read value in case insensitive mode; to read value in case sensitive mode.
- /// If , considers the value included in metadata attributes such as
- /// [Display] attribute when parsing, otherwise only considers the member names.
+ /// If ,
+ /// considers the value included in attribute
+ /// when parsing, otherwise only considers the member names.
/// if the value parameter was converted successfully; otherwise, .
public static bool TryParse(
#if NETCOREAPP3_0_OR_GREATER
@@ -294,7 +294,6 @@ namespace System.IO
out global::System.IO.FileShare value,
bool allowMatchingMetadataAttribute)
{
-
switch (name)
{
case string s when s.Equals(nameof(global::System.IO.FileShare.None), global::System.StringComparison.OrdinalIgnoreCase):
@@ -332,7 +331,6 @@ namespace System.IO
out global::System.IO.FileShare value,
bool allowMatchingMetadataAttribute)
{
-
switch (name)
{
case nameof(global::System.IO.FileShare.None):
@@ -399,8 +397,8 @@ namespace System.IO
///
/// The case-sensitive string representation of the enumeration name or underlying value to convert
/// to read value in case insensitive mode; to read value in case sensitive mode.
- /// If , considers the value included in metadata attributes such as
- /// [Display] attribute when parsing, otherwise only considers the member names.
+ /// If , considers the value included in
+ /// attribute when parsing, otherwise only considers the member names.
/// An object of type whose
/// value is represented by
public static global::System.IO.FileShare Parse(
@@ -466,8 +464,8 @@ namespace System.IO
/// If the parse operation fails, contains the default value of the underlying type
/// of . This parameter is passed uninitialized.
/// to read value in case insensitive mode; to read value in case sensitive mode.
- /// If , considers the value included in metadata attributes such as
- /// [Display] attribute when parsing, otherwise only considers the member names.
+ /// If , considers the value included in
+ /// attribute when parsing, otherwise only considers the member names.
/// if the value parameter was converted successfully; otherwise, .
public static bool TryParse(
#if NETCOREAPP3_0_OR_GREATER
diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateForMultipleExternalEnums.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateForMultipleExternalEnums.verified.txt
index 577e496..849ef73 100644
--- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateForMultipleExternalEnums.verified.txt
+++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanGenerateForMultipleExternalEnums.verified.txt
@@ -1,4 +1,4 @@
-[
+[
//------------------------------------------------------------------------------
//
// This code was generated by the NetEscapades.EnumGenerators source generator
@@ -26,20 +26,6 @@ namespace System
///
public const int Length = 16;
- ///
- /// Returns the string representation of the value.
- /// If the attribute is decorated with a [Display] or [Description]attribute, then
- /// uses the provided value. Otherwise uses the name of the member, equivalent to
- /// calling ToString() on .
- ///
- /// The value to retrieve the string value for
- /// If uses the value provided in the
- /// [Display] or [Description]attribute as the string representation of the member.
- /// If , always uses the name of the member, the same as if ToString() was called.
- /// The string representation of the value
- public static string ToStringFast(this global::System.ConsoleColor value, bool useMetadataAttributes)
- => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast();
-
///
/// Returns the string representation of the value.
/// Directly equivalent to calling ToString() on .
@@ -68,6 +54,20 @@ namespace System
_ => value.AsUnderlyingType().ToString(),
};
+ ///
+ /// Returns the string representation of the value.
+ /// If the member is decorated with the attribute
+ /// then that value is returned. Otherwise returns uses the name of the member,
+ /// equivalent to calling ToString() on .
+ ///
+ /// The value to retrieve the string value for
+ /// If uses the value provided in the
+ /// attribute as the string representation of the member.
+ /// If , always uses the name of the member, the same as if ToString() was called.
+ /// The string representation of the value
+ public static string ToStringFast(this global::System.ConsoleColor value, bool useMetadataAttributes)
+ => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast();
+
private static string ToStringFastWithMetadata(this global::System.ConsoleColor value)
=> value.ToStringFast();
@@ -113,20 +113,8 @@ namespace System
///
/// The name to check if it's defined
/// if a member with the name exists in the enumeration, otherwise
- public static bool IsDefined(string name) => IsDefined(name, allowMatchingMetadataAttribute: false);
-
- ///
- /// Returns a boolean telling whether an enum with the given name exists in the enumeration,
- /// or if a member decorated with a [Display] attribute
- /// with the required name exists.
- ///
- /// The name to check if it's defined
- /// If , considers the value of metadata attributes,otherwise ignores them
- /// if a member with the name exists in the enumeration, or a member is decorated
- /// with a [Display] attribute with the name, otherwise
- public static bool IsDefined(string name, bool allowMatchingMetadataAttribute)
- {
- return name switch
+ public static bool IsDefined(string name)
+ => name switch
{
nameof(global::System.ConsoleColor.Black) => true,
nameof(global::System.ConsoleColor.DarkBlue) => true,
@@ -144,31 +132,31 @@ namespace System
nameof(global::System.ConsoleColor.Magenta) => true,
nameof(global::System.ConsoleColor.Yellow) => true,
nameof(global::System.ConsoleColor.White) => true,
- _ => false,
- };
- }
-
-#if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0
- ///
- /// Returns a boolean telling whether an enum with the given name exists in the enumeration
- ///
- /// The name to check if it's defined
- /// if a member with the name exists in the enumeration, otherwise
- public static bool IsDefined(in global::System.ReadOnlySpan name) => IsDefined(name, allowMatchingMetadataAttribute: false);
+ _ => false,
+ };
///
/// Returns a boolean telling whether an enum with the given name exists in the enumeration,
- /// or optionally if a member decorated with a [Display] attribute
+ /// or if a member decorated with
/// with the required name exists.
- /// Slower then the overload, but doesn't allocate memory./>
///
/// The name to check if it's defined
- /// If , considers the value of metadata attributes,otherwise ignores them
+ /// If ,
+ /// considers the value of
+ /// instead of the member name, otherwise ignores them
/// if a member with the name exists in the enumeration, or a member is decorated
/// with a [Display] attribute with the name, otherwise
- public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute)
- {
- return name switch
+ public static bool IsDefined(string name, bool allowMatchingMetadataAttribute)
+ => IsDefined(name);
+
+#if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0
+ ///
+ /// Returns a boolean telling whether an enum with the given name exists in the enumeration
+ ///
+ /// The name to check if it's defined
+ /// if a member with the name exists in the enumeration, otherwise
+ public static bool IsDefined(in global::System.ReadOnlySpan name)
+ => name switch
{
global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::System.ConsoleColor.Black), global::System.StringComparison.Ordinal) => true,
global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::System.ConsoleColor.DarkBlue), global::System.StringComparison.Ordinal) => true,
@@ -188,7 +176,18 @@ namespace System
global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::System.ConsoleColor.White), global::System.StringComparison.Ordinal) => true,
_ => false,
};
- }
+
+ ///
+ /// Returns a boolean telling whether an enum with the given name exists in the enumeration,
+ /// or optionally if a member decorated with
+ /// exists. Slower then the overload, but doesn't allocate memory./>
+ ///
+ /// The name to check if it's defined
+ /// If , considers the value of metadata attributes,otherwise ignores them
+ /// if a member with the name exists in the enumeration, or a member is decorated
+ /// with a [Display] attribute with the name, otherwise
+ public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute)
+ => IsDefined(name);
#endif
///
@@ -300,8 +299,9 @@ namespace System
/// If the parse operation fails, contains the default value of the underlying type
/// of . This parameter is passed uninitialized.
/// to read value in case insensitive mode; to read value in case sensitive mode.
- /// If , considers the value included in metadata attributes such as
- /// [Display] attribute when parsing, otherwise only considers the member names.
+ /// If ,
+ /// considers the value included in attribute
+ /// when parsing, otherwise only considers the member names.
/// if the value parameter was converted successfully; otherwise, .
public static bool TryParse(
#if NETCOREAPP3_0_OR_GREATER
@@ -323,7 +323,6 @@ namespace System
out global::System.ConsoleColor value,
bool allowMatchingMetadataAttribute)
{
-
switch (name)
{
case string s when s.Equals(nameof(global::System.ConsoleColor.Black), global::System.StringComparison.OrdinalIgnoreCase):
@@ -391,7 +390,6 @@ namespace System
out global::System.ConsoleColor value,
bool allowMatchingMetadataAttribute)
{
-
switch (name)
{
case nameof(global::System.ConsoleColor.Black):
@@ -488,8 +486,8 @@ namespace System
///
/// The case-sensitive string representation of the enumeration name or underlying value to convert
/// to read value in case insensitive mode; to read value in case sensitive mode.
- /// If , considers the value included in metadata attributes such as
- /// [Display] attribute when parsing, otherwise only considers the member names.
+ /// If , considers the value included in
+ /// attribute when parsing, otherwise only considers the member names.
/// An object of type whose
/// value is represented by
public static global::System.ConsoleColor Parse(
@@ -555,8 +553,8 @@ namespace System
/// If the parse operation fails, contains the default value of the underlying type
/// of . This parameter is passed uninitialized.
/// to read value in case insensitive mode; to read value in case sensitive mode.
- /// If , considers the value included in metadata attributes such as
- /// [Display] attribute when parsing, otherwise only considers the member names.
+ /// If , considers the value included in
+ /// attribute when parsing, otherwise only considers the member names.
/// if the value parameter was converted successfully; otherwise, .
public static bool TryParse(
#if NETCOREAPP3_0_OR_GREATER
@@ -826,20 +824,6 @@ namespace System
///
public const int Length = 3;
- ///
- /// Returns the string representation of the value.
- /// If the attribute is decorated with a [Display] or [Description]attribute, then
- /// uses the provided value. Otherwise uses the name of the member, equivalent to
- /// calling ToString() on .
- ///
- /// The value to retrieve the string value for
- /// If uses the value provided in the
- /// [Display] or [Description]attribute as the string representation of the member.
- /// If , always uses the name of the member, the same as if ToString() was called.
- /// The string representation of the value
- public static string ToStringFast(this global::System.DateTimeKind value, bool useMetadataAttributes)
- => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast();
-
///
/// Returns the string representation of the value.
/// Directly equivalent to calling ToString() on .
@@ -855,6 +839,20 @@ namespace System
_ => value.AsUnderlyingType().ToString(),
};
+ ///
+ /// Returns the string representation of the value.
+ /// If the member is decorated with the attribute
+ /// then that value is returned. Otherwise returns uses the name of the member,
+ /// equivalent to calling ToString() on .
+ ///
+ /// The value to retrieve the string value for
+ /// If uses the value provided in the
+ /// attribute as the string representation of the member.
+ /// If , always uses the name of the member, the same as if ToString() was called.
+ /// The string representation of the value
+ public static string ToStringFast(this global::System.DateTimeKind value, bool useMetadataAttributes)
+ => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast();
+
private static string ToStringFastWithMetadata(this global::System.DateTimeKind value)
=> value.ToStringFast();
@@ -887,27 +885,28 @@ namespace System
///
/// The name to check if it's defined
/// if a member with the name exists in the enumeration, otherwise
- public static bool IsDefined(string name) => IsDefined(name, allowMatchingMetadataAttribute: false);
+ public static bool IsDefined(string name)
+ => name switch
+ {
+ nameof(global::System.DateTimeKind.Unspecified) => true,
+ nameof(global::System.DateTimeKind.Utc) => true,
+ nameof(global::System.DateTimeKind.Local) => true,
+ _ => false,
+ };
///
/// Returns a boolean telling whether an enum with the given name exists in the enumeration,
- /// or if a member decorated with a [Display] attribute
+ /// or if a member decorated with
/// with the required name exists.
///
/// The name to check if it's defined
- /// If , considers the value of metadata attributes,otherwise ignores them
+ /// If ,
+ /// considers the value of
+ /// instead of the member name, otherwise ignores them
/// if a member with the name exists in the enumeration, or a member is decorated
/// with a [Display] attribute with the name, otherwise
public static bool IsDefined(string name, bool allowMatchingMetadataAttribute)
- {
- return name switch
- {
- nameof(global::System.DateTimeKind.Unspecified) => true,
- nameof(global::System.DateTimeKind.Utc) => true,
- nameof(global::System.DateTimeKind.Local) => true,
- _ => false,
- };
- }
+ => IsDefined(name);
#if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0
///
@@ -915,28 +914,26 @@ namespace System
///
/// The name to check if it's defined
/// if a member with the name exists in the enumeration, otherwise
- public static bool IsDefined(in global::System.ReadOnlySpan name) => IsDefined(name, allowMatchingMetadataAttribute: false);
+ public static bool IsDefined(in global::System.ReadOnlySpan name)
+ => name switch
+ {
+ global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::System.DateTimeKind.Unspecified), global::System.StringComparison.Ordinal) => true,
+ global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::System.DateTimeKind.Utc), global::System.StringComparison.Ordinal) => true,
+ global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::System.DateTimeKind.Local), global::System.StringComparison.Ordinal) => true,
+ _ => false,
+ };
///
/// Returns a boolean telling whether an enum with the given name exists in the enumeration,
- /// or optionally if a member decorated with a [Display] attribute
- /// with the required name exists.
- /// Slower then the overload, but doesn't allocate memory./>
+ /// or optionally if a member decorated with
+ /// exists. Slower then the overload, but doesn't allocate memory./>
///
/// The name to check if it's defined
/// If , considers the value of metadata attributes,otherwise ignores them
/// if a member with the name exists in the enumeration, or a member is decorated
/// with a [Display] attribute with the name, otherwise
public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute)
- {
- return name switch
- {
- global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::System.DateTimeKind.Unspecified), global::System.StringComparison.Ordinal) => true,
- global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::System.DateTimeKind.Utc), global::System.StringComparison.Ordinal) => true,
- global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::System.DateTimeKind.Local), global::System.StringComparison.Ordinal) => true,
- _ => false,
- };
- }
+ => IsDefined(name);
#endif
///
@@ -1048,8 +1045,9 @@ namespace System
/// If the parse operation fails, contains the default value of the underlying type
/// of . This parameter is passed uninitialized.
/// to read value in case insensitive mode; to read value in case sensitive mode.
- /// If , considers the value included in metadata attributes such as
- /// [Display] attribute when parsing, otherwise only considers the member names.
+ /// If ,
+ /// considers the value included in attribute
+ /// when parsing, otherwise only considers the member names.
/// if the value parameter was converted successfully; otherwise, .
public static bool TryParse(
#if NETCOREAPP3_0_OR_GREATER
@@ -1071,7 +1069,6 @@ namespace System
out global::System.DateTimeKind value,
bool allowMatchingMetadataAttribute)
{
-
switch (name)
{
case string s when s.Equals(nameof(global::System.DateTimeKind.Unspecified), global::System.StringComparison.OrdinalIgnoreCase):
@@ -1100,7 +1097,6 @@ namespace System
out global::System.DateTimeKind value,
bool allowMatchingMetadataAttribute)
{
-
switch (name)
{
case nameof(global::System.DateTimeKind.Unspecified):
@@ -1158,8 +1154,8 @@ namespace System
///
/// The case-sensitive string representation of the enumeration name or underlying value to convert
/// to read value in case insensitive mode; to read value in case sensitive mode.
- /// If , considers the value included in metadata attributes such as
- /// [Display] attribute when parsing, otherwise only considers the member names.
+ /// If , considers the value included in
+ /// attribute when parsing, otherwise only considers the member names.
/// An object of type whose
/// value is represented by
public static global::System.DateTimeKind Parse(
@@ -1225,8 +1221,8 @@ namespace System
/// If the parse operation fails, contains the default value of the underlying type
/// of . This parameter is passed uninitialized.
/// to read value in case insensitive mode; to read value in case sensitive mode.
- /// If , considers the value included in metadata attributes such as
- /// [Display] attribute when parsing, otherwise only considers the member names.
+ /// If , considers the value included in
+ /// attribute when parsing, otherwise only considers the member names.
/// if the value parameter was converted successfully; otherwise, .
public static bool TryParse(
#if NETCOREAPP3_0_OR_GREATER
diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanHandleNamespaceAndClassNameAreTheSame.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanHandleNamespaceAndClassNameAreTheSame.verified.txt
index 825f89c..461133e 100644
--- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanHandleNamespaceAndClassNameAreTheSame.verified.txt
+++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.CanHandleNamespaceAndClassNameAreTheSame.verified.txt
@@ -25,20 +25,6 @@ namespace Foo
///
public const int Length = 1;
- ///
- /// Returns the string representation of the value.
- /// If the attribute is decorated with a [Display] or [Description]attribute, then
- /// uses the provided value. Otherwise uses the name of the member, equivalent to
- /// calling ToString() on .
- ///
- /// The value to retrieve the string value for
- /// If uses the value provided in the
- /// [Display] or [Description]attribute as the string representation of the member.
- /// If , always uses the name of the member, the same as if ToString() was called.
- /// The string representation of the value
- public static string ToStringFast(this global::Foo.TestEnum value, bool useMetadataAttributes)
- => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast();
-
///
/// Returns the string representation of the value.
/// Directly equivalent to calling ToString() on .
@@ -52,6 +38,20 @@ namespace Foo
_ => value.AsUnderlyingType().ToString(),
};
+ ///
+ /// Returns the string representation of the value.
+ /// If the member is decorated with the attribute
+ /// then that value is returned. Otherwise returns uses the name of the member,
+ /// equivalent to calling ToString() on .
+ ///
+ /// The value to retrieve the string value for
+ /// If uses the value provided in the
+ /// attribute as the string representation of the member.
+ /// If , always uses the name of the member, the same as if ToString() was called.
+ /// The string representation of the value
+ public static string ToStringFast(this global::Foo.TestEnum value, bool useMetadataAttributes)
+ => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast();
+
private static string ToStringFastWithMetadata(this global::Foo.TestEnum value)
=> value.ToStringFast();
@@ -82,25 +82,26 @@ namespace Foo
///
/// The name to check if it's defined
/// if a member with the name exists in the enumeration, otherwise
- public static bool IsDefined(string name) => IsDefined(name, allowMatchingMetadataAttribute: false);
+ public static bool IsDefined(string name)
+ => name switch
+ {
+ nameof(global::Foo.TestEnum.Value1) => true,
+ _ => false,
+ };
///
/// Returns a boolean telling whether an enum with the given name exists in the enumeration,
- /// or if a member decorated with a [Display] attribute
+ /// or if a member decorated with
/// with the required name exists.
///
/// The name to check if it's defined
- /// If , considers the value of metadata attributes,otherwise ignores them
+ /// If ,
+ /// considers the value of
+ /// instead of the member name, otherwise ignores them
/// if a member with the name exists in the enumeration, or a member is decorated
/// with a [Display] attribute with the name, otherwise
public static bool IsDefined(string name, bool allowMatchingMetadataAttribute)
- {
- return name switch
- {
- nameof(global::Foo.TestEnum.Value1) => true,
- _ => false,
- };
- }
+ => IsDefined(name);
#if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0
///
@@ -108,26 +109,24 @@ namespace Foo
///
/// The name to check if it's defined
/// if a member with the name exists in the enumeration, otherwise
- public static bool IsDefined(in global::System.ReadOnlySpan name) => IsDefined(name, allowMatchingMetadataAttribute: false);
+ public static bool IsDefined(in global::System.ReadOnlySpan name)
+ => name switch
+ {
+ global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::Foo.TestEnum.Value1), global::System.StringComparison.Ordinal) => true,
+ _ => false,
+ };
///
/// Returns a boolean telling whether an enum with the given name exists in the enumeration,
- /// or optionally if a member decorated with a [Display] attribute
- /// with the required name exists.
- /// Slower then the overload, but doesn't allocate memory./>
+ /// or optionally if a member decorated with
+ /// exists. Slower then the overload, but doesn't allocate memory./>
///
/// The name to check if it's defined
/// If , considers the value of metadata attributes,otherwise ignores them
/// if a member with the name exists in the enumeration, or a member is decorated
/// with a [Display] attribute with the name, otherwise
public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute)
- {
- return name switch
- {
- global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::Foo.TestEnum.Value1), global::System.StringComparison.Ordinal) => true,
- _ => false,
- };
- }
+ => IsDefined(name);
#endif
///
@@ -239,8 +238,9 @@ namespace Foo
/// If the parse operation fails, contains the default value of the underlying type
/// of . This parameter is passed uninitialized.
/// to read value in case insensitive mode; to read value in case sensitive mode.
- /// If , considers the value included in metadata attributes such as
- /// [Display] attribute when parsing, otherwise only considers the member names.
+ /// If ,
+ /// considers the value included in attribute
+ /// when parsing, otherwise only considers the member names.
/// if the value parameter was converted successfully; otherwise, .
public static bool TryParse(
#if NETCOREAPP3_0_OR_GREATER
@@ -262,7 +262,6 @@ namespace Foo
out global::Foo.TestEnum value,
bool allowMatchingMetadataAttribute)
{
-
switch (name)
{
case string s when s.Equals(nameof(global::Foo.TestEnum.Value1), global::System.StringComparison.OrdinalIgnoreCase):
@@ -285,7 +284,6 @@ namespace Foo
out global::Foo.TestEnum value,
bool allowMatchingMetadataAttribute)
{
-
switch (name)
{
case nameof(global::Foo.TestEnum.Value1):
@@ -337,8 +335,8 @@ namespace Foo
///
/// The case-sensitive string representation of the enumeration name or underlying value to convert
/// to read value in case insensitive mode; to read value in case sensitive mode.
- /// If , considers the value included in metadata attributes such as
- /// [Display] attribute when parsing, otherwise only considers the member names.
+ /// If , considers the value included in
+ /// attribute when parsing, otherwise only considers the member names.
/// An object of type whose
/// value is represented by
public static global::Foo.TestEnum Parse(
@@ -404,8 +402,8 @@ namespace Foo
/// If the parse operation fails, contains the default value of the underlying type
/// of . This parameter is passed uninitialized.
/// to read value in case insensitive mode; to read value in case sensitive mode.
- /// If , considers the value included in metadata attributes such as
- /// [Display] attribute when parsing, otherwise only considers the member names.
+ /// If , considers the value included in
+ /// attribute when parsing, otherwise only considers the member names.
/// if the value parameter was converted successfully; otherwise, .
public static bool TryParse(
#if NETCOREAPP3_0_OR_GREATER
diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.DoesNotGenerateWarningsForObsoleteEnums_CS0612_Issue97.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.DoesNotGenerateWarningsForObsoleteEnums_CS0612_Issue97.verified.txt
index 0be1be2..e9c8e6e 100644
--- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.DoesNotGenerateWarningsForObsoleteEnums_CS0612_Issue97.verified.txt
+++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.DoesNotGenerateWarningsForObsoleteEnums_CS0612_Issue97.verified.txt
@@ -1,4 +1,4 @@
-//------------------------------------------------------------------------------
+//------------------------------------------------------------------------------
//
// This code was generated by the NetEscapades.EnumGenerators source generator
//
@@ -23,20 +23,6 @@
///
public const int Length = 2;
- ///
- /// Returns the string representation of the value.
- /// If the attribute is decorated with a [Display] or [Description]attribute, then
- /// uses the provided value. Otherwise uses the name of the member, equivalent to
- /// calling ToString() on .
- ///
- /// The value to retrieve the string value for
- /// If uses the value provided in the
- /// [Display] or [Description]attribute as the string representation of the member.
- /// If , always uses the name of the member, the same as if ToString() was called.
- /// The string representation of the value
- public static string ToStringFast(this global::MyEnum value, bool useMetadataAttributes)
- => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast();
-
///
/// Returns the string representation of the value.
/// Directly equivalent to calling ToString() on .
@@ -51,6 +37,20 @@
_ => value.AsUnderlyingType().ToString(),
};
+ ///
+ /// Returns the string representation of the value.
+ /// If the member is decorated with the attribute
+ /// then that value is returned. Otherwise returns uses the name of the member,
+ /// equivalent to calling ToString() on .
+ ///
+ /// The value to retrieve the string value for
+ /// If uses the value provided in the
+ /// attribute as the string representation of the member.
+ /// If , always uses the name of the member, the same as if ToString() was called.
+ /// The string representation of the value
+ public static string ToStringFast(this global::MyEnum value, bool useMetadataAttributes)
+ => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast();
+
private static string ToStringFastWithMetadata(this global::MyEnum value)
=> value.ToStringFast();
@@ -82,26 +82,27 @@
///
/// The name to check if it's defined
/// if a member with the name exists in the enumeration, otherwise
- public static bool IsDefined(string name) => IsDefined(name, allowMatchingMetadataAttribute: false);
+ public static bool IsDefined(string name)
+ => name switch
+ {
+ nameof(global::MyEnum.First) => true,
+ nameof(global::MyEnum.Second) => true,
+ _ => false,
+ };
///
/// Returns a boolean telling whether an enum with the given name exists in the enumeration,
- /// or if a member decorated with a [Display] attribute
+ /// or if a member decorated with
/// with the required name exists.
///
/// The name to check if it's defined
- /// If , considers the value of metadata attributes,otherwise ignores them
+ /// If ,
+ /// considers the value of
+ /// instead of the member name, otherwise ignores them
/// if a member with the name exists in the enumeration, or a member is decorated
/// with a [Display] attribute with the name, otherwise
public static bool IsDefined(string name, bool allowMatchingMetadataAttribute)
- {
- return name switch
- {
- nameof(global::MyEnum.First) => true,
- nameof(global::MyEnum.Second) => true,
- _ => false,
- };
- }
+ => IsDefined(name);
#if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0
///
@@ -109,27 +110,25 @@
///
/// The name to check if it's defined
/// if a member with the name exists in the enumeration, otherwise
- public static bool IsDefined(in global::System.ReadOnlySpan name) => IsDefined(name, allowMatchingMetadataAttribute: false);
+ public static bool IsDefined(in global::System.ReadOnlySpan name)
+ => name switch
+ {
+ global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyEnum.First), global::System.StringComparison.Ordinal) => true,
+ global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyEnum.Second), global::System.StringComparison.Ordinal) => true,
+ _ => false,
+ };
///
/// Returns a boolean telling whether an enum with the given name exists in the enumeration,
- /// or optionally if a member decorated with a [Display] attribute
- /// with the required name exists.
- /// Slower then the overload, but doesn't allocate memory./>
+ /// or optionally if a member decorated with
+ /// exists. Slower then the overload, but doesn't allocate memory./>
///
/// The name to check if it's defined
/// If , considers the value of metadata attributes,otherwise ignores them
/// if a member with the name exists in the enumeration, or a member is decorated
/// with a [Display] attribute with the name, otherwise
public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute)
- {
- return name switch
- {
- global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyEnum.First), global::System.StringComparison.Ordinal) => true,
- global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyEnum.Second), global::System.StringComparison.Ordinal) => true,
- _ => false,
- };
- }
+ => IsDefined(name);
#endif
///
@@ -241,8 +240,9 @@
/// If the parse operation fails, contains the default value of the underlying type
/// of . This parameter is passed uninitialized.
/// to read value in case insensitive mode; to read value in case sensitive mode.
- /// If , considers the value included in metadata attributes such as
- /// [Display] attribute when parsing, otherwise only considers the member names.
+ /// If ,
+ /// considers the value included in attribute
+ /// when parsing, otherwise only considers the member names.
/// if the value parameter was converted successfully; otherwise, .
public static bool TryParse(
#if NETCOREAPP3_0_OR_GREATER
@@ -264,7 +264,6 @@
out global::MyEnum value,
bool allowMatchingMetadataAttribute)
{
-
switch (name)
{
case string s when s.Equals(nameof(global::MyEnum.First), global::System.StringComparison.OrdinalIgnoreCase):
@@ -290,7 +289,6 @@
out global::MyEnum value,
bool allowMatchingMetadataAttribute)
{
-
switch (name)
{
case nameof(global::MyEnum.First):
@@ -345,8 +343,8 @@
///
/// The case-sensitive string representation of the enumeration name or underlying value to convert
/// to read value in case insensitive mode; to read value in case sensitive mode.
- /// If , considers the value included in metadata attributes such as
- /// [Display] attribute when parsing, otherwise only considers the member names.
+ /// If , considers the value included in
+ /// attribute when parsing, otherwise only considers the member names.
/// An object of type whose
/// value is represented by
public static global::MyEnum Parse(
@@ -412,8 +410,8 @@
/// If the parse operation fails, contains the default value of the underlying type
/// of . This parameter is passed uninitialized.
/// to read value in case insensitive mode; to read value in case sensitive mode.
- /// If , considers the value included in metadata attributes such as
- /// [Display] attribute when parsing, otherwise only considers the member names.
+ /// If , considers the value included in
+ /// attribute when parsing, otherwise only considers the member names.
/// if the value parameter was converted successfully; otherwise, .
public static bool TryParse(
#if NETCOREAPP3_0_OR_GREATER
diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.DoesNotGenerateWarningsForObsoleteEnums_CS0618_Issue97.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.DoesNotGenerateWarningsForObsoleteEnums_CS0618_Issue97.verified.txt
index 0be1be2..e9c8e6e 100644
--- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.DoesNotGenerateWarningsForObsoleteEnums_CS0618_Issue97.verified.txt
+++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.DoesNotGenerateWarningsForObsoleteEnums_CS0618_Issue97.verified.txt
@@ -1,4 +1,4 @@
-//------------------------------------------------------------------------------
+//------------------------------------------------------------------------------
//
// This code was generated by the NetEscapades.EnumGenerators source generator
//
@@ -23,20 +23,6 @@
///
public const int Length = 2;
- ///
- /// Returns the string representation of the value.
- /// If the attribute is decorated with a [Display] or [Description]attribute, then
- /// uses the provided value. Otherwise uses the name of the member, equivalent to
- /// calling ToString() on .
- ///
- /// The value to retrieve the string value for
- /// If uses the value provided in the
- /// [Display] or [Description]attribute as the string representation of the member.
- /// If , always uses the name of the member, the same as if ToString() was called.
- /// The string representation of the value
- public static string ToStringFast(this global::MyEnum value, bool useMetadataAttributes)
- => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast();
-
///
/// Returns the string representation of the value.
/// Directly equivalent to calling ToString() on .
@@ -51,6 +37,20 @@
_ => value.AsUnderlyingType().ToString(),
};
+ ///
+ /// Returns the string representation of the value.
+ /// If the member is decorated with the attribute
+ /// then that value is returned. Otherwise returns uses the name of the member,
+ /// equivalent to calling ToString() on .
+ ///
+ /// The value to retrieve the string value for
+ /// If uses the value provided in the
+ /// attribute as the string representation of the member.
+ /// If , always uses the name of the member, the same as if ToString() was called.
+ /// The string representation of the value
+ public static string ToStringFast(this global::MyEnum value, bool useMetadataAttributes)
+ => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast();
+
private static string ToStringFastWithMetadata(this global::MyEnum value)
=> value.ToStringFast();
@@ -82,26 +82,27 @@
///
/// The name to check if it's defined
/// if a member with the name exists in the enumeration, otherwise
- public static bool IsDefined(string name) => IsDefined(name, allowMatchingMetadataAttribute: false);
+ public static bool IsDefined(string name)
+ => name switch
+ {
+ nameof(global::MyEnum.First) => true,
+ nameof(global::MyEnum.Second) => true,
+ _ => false,
+ };
///
/// Returns a boolean telling whether an enum with the given name exists in the enumeration,
- /// or if a member decorated with a [Display] attribute
+ /// or if a member decorated with
/// with the required name exists.
///
/// The name to check if it's defined
- /// If , considers the value of metadata attributes,otherwise ignores them
+ /// If ,
+ /// considers the value of
+ /// instead of the member name, otherwise ignores them
/// if a member with the name exists in the enumeration, or a member is decorated
/// with a [Display] attribute with the name, otherwise
public static bool IsDefined(string name, bool allowMatchingMetadataAttribute)
- {
- return name switch
- {
- nameof(global::MyEnum.First) => true,
- nameof(global::MyEnum.Second) => true,
- _ => false,
- };
- }
+ => IsDefined(name);
#if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0
///
@@ -109,27 +110,25 @@
///
/// The name to check if it's defined
/// if a member with the name exists in the enumeration, otherwise
- public static bool IsDefined(in global::System.ReadOnlySpan name) => IsDefined(name, allowMatchingMetadataAttribute: false);
+ public static bool IsDefined(in global::System.ReadOnlySpan name)
+ => name switch
+ {
+ global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyEnum.First), global::System.StringComparison.Ordinal) => true,
+ global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyEnum.Second), global::System.StringComparison.Ordinal) => true,
+ _ => false,
+ };
///
/// Returns a boolean telling whether an enum with the given name exists in the enumeration,
- /// or optionally if a member decorated with a [Display] attribute
- /// with the required name exists.
- /// Slower then the overload, but doesn't allocate memory./>
+ /// or optionally if a member decorated with
+ /// exists. Slower then the overload, but doesn't allocate memory./>
///
/// The name to check if it's defined
/// If , considers the value of metadata attributes,otherwise ignores them
/// if a member with the name exists in the enumeration, or a member is decorated
/// with a [Display] attribute with the name, otherwise
public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute)
- {
- return name switch
- {
- global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyEnum.First), global::System.StringComparison.Ordinal) => true,
- global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyEnum.Second), global::System.StringComparison.Ordinal) => true,
- _ => false,
- };
- }
+ => IsDefined(name);
#endif
///
@@ -241,8 +240,9 @@
/// If the parse operation fails, contains the default value of the underlying type
/// of . This parameter is passed uninitialized.
/// to read value in case insensitive mode; to read value in case sensitive mode.
- /// If , considers the value included in metadata attributes such as
- /// [Display] attribute when parsing, otherwise only considers the member names.
+ /// If ,
+ /// considers the value included in attribute
+ /// when parsing, otherwise only considers the member names.
/// if the value parameter was converted successfully; otherwise, .
public static bool TryParse(
#if NETCOREAPP3_0_OR_GREATER
@@ -264,7 +264,6 @@
out global::MyEnum value,
bool allowMatchingMetadataAttribute)
{
-
switch (name)
{
case string s when s.Equals(nameof(global::MyEnum.First), global::System.StringComparison.OrdinalIgnoreCase):
@@ -290,7 +289,6 @@
out global::MyEnum value,
bool allowMatchingMetadataAttribute)
{
-
switch (name)
{
case nameof(global::MyEnum.First):
@@ -345,8 +343,8 @@
///
/// The case-sensitive string representation of the enumeration name or underlying value to convert
/// to read value in case insensitive mode; to read value in case sensitive mode.
- /// If , considers the value included in metadata attributes such as
- /// [Display] attribute when parsing, otherwise only considers the member names.
+ /// If , considers the value included in
+ /// attribute when parsing, otherwise only considers the member names.
/// An object of type whose
/// value is represented by
public static global::MyEnum Parse(
@@ -412,8 +410,8 @@
/// If the parse operation fails, contains the default value of the underlying type
/// of . This parameter is passed uninitialized.
/// to read value in case insensitive mode; to read value in case sensitive mode.
- /// If , considers the value included in metadata attributes such as
- /// [Display] attribute when parsing, otherwise only considers the member names.
+ /// If , considers the value included in
+ /// attribute when parsing, otherwise only considers the member names.
/// if the value parameter was converted successfully; otherwise, .
public static bool TryParse(
#if NETCOREAPP3_0_OR_GREATER
diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.DoesNotGenerateWarningsForObsoleteMembers_CS0612_Issue97.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.DoesNotGenerateWarningsForObsoleteMembers_CS0612_Issue97.verified.txt
index 0be1be2..e9c8e6e 100644
--- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.DoesNotGenerateWarningsForObsoleteMembers_CS0612_Issue97.verified.txt
+++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.DoesNotGenerateWarningsForObsoleteMembers_CS0612_Issue97.verified.txt
@@ -1,4 +1,4 @@
-//------------------------------------------------------------------------------
+//------------------------------------------------------------------------------
//
// This code was generated by the NetEscapades.EnumGenerators source generator
//
@@ -23,20 +23,6 @@
///
public const int Length = 2;
- ///
- /// Returns the string representation of the value.
- /// If the attribute is decorated with a [Display] or [Description]attribute, then
- /// uses the provided value. Otherwise uses the name of the member, equivalent to
- /// calling ToString() on .
- ///
- /// The value to retrieve the string value for
- /// If uses the value provided in the
- /// [Display] or [Description]attribute as the string representation of the member.
- /// If , always uses the name of the member, the same as if ToString() was called.
- /// The string representation of the value
- public static string ToStringFast(this global::MyEnum value, bool useMetadataAttributes)
- => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast();
-
///
/// Returns the string representation of the value.
/// Directly equivalent to calling ToString() on .
@@ -51,6 +37,20 @@
_ => value.AsUnderlyingType().ToString(),
};
+ ///
+ /// Returns the string representation of the value.
+ /// If the member is decorated with the attribute
+ /// then that value is returned. Otherwise returns uses the name of the member,
+ /// equivalent to calling ToString() on .
+ ///
+ /// The value to retrieve the string value for
+ /// If uses the value provided in the
+ /// attribute as the string representation of the member.
+ /// If , always uses the name of the member, the same as if ToString() was called.
+ /// The string representation of the value
+ public static string ToStringFast(this global::MyEnum value, bool useMetadataAttributes)
+ => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast();
+
private static string ToStringFastWithMetadata(this global::MyEnum value)
=> value.ToStringFast();
@@ -82,26 +82,27 @@
///
/// The name to check if it's defined
/// if a member with the name exists in the enumeration, otherwise
- public static bool IsDefined(string name) => IsDefined(name, allowMatchingMetadataAttribute: false);
+ public static bool IsDefined(string name)
+ => name switch
+ {
+ nameof(global::MyEnum.First) => true,
+ nameof(global::MyEnum.Second) => true,
+ _ => false,
+ };
///
/// Returns a boolean telling whether an enum with the given name exists in the enumeration,
- /// or if a member decorated with a [Display] attribute
+ /// or if a member decorated with
/// with the required name exists.
///
/// The name to check if it's defined
- /// If , considers the value of metadata attributes,otherwise ignores them
+ /// If ,
+ /// considers the value of
+ /// instead of the member name, otherwise ignores them
/// if a member with the name exists in the enumeration, or a member is decorated
/// with a [Display] attribute with the name, otherwise
public static bool IsDefined(string name, bool allowMatchingMetadataAttribute)
- {
- return name switch
- {
- nameof(global::MyEnum.First) => true,
- nameof(global::MyEnum.Second) => true,
- _ => false,
- };
- }
+ => IsDefined(name);
#if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0
///
@@ -109,27 +110,25 @@
///
/// The name to check if it's defined
/// if a member with the name exists in the enumeration, otherwise
- public static bool IsDefined(in global::System.ReadOnlySpan name) => IsDefined(name, allowMatchingMetadataAttribute: false);
+ public static bool IsDefined(in global::System.ReadOnlySpan name)
+ => name switch
+ {
+ global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyEnum.First), global::System.StringComparison.Ordinal) => true,
+ global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyEnum.Second), global::System.StringComparison.Ordinal) => true,
+ _ => false,
+ };
///
/// Returns a boolean telling whether an enum with the given name exists in the enumeration,
- /// or optionally if a member decorated with a [Display] attribute
- /// with the required name exists.
- /// Slower then the overload, but doesn't allocate memory./>
+ /// or optionally if a member decorated with
+ /// exists. Slower then the overload, but doesn't allocate memory./>
///
/// The name to check if it's defined
/// If , considers the value of metadata attributes,otherwise ignores them
/// if a member with the name exists in the enumeration, or a member is decorated
/// with a [Display] attribute with the name, otherwise
public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute)
- {
- return name switch
- {
- global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyEnum.First), global::System.StringComparison.Ordinal) => true,
- global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyEnum.Second), global::System.StringComparison.Ordinal) => true,
- _ => false,
- };
- }
+ => IsDefined(name);
#endif
///
@@ -241,8 +240,9 @@
/// If the parse operation fails, contains the default value of the underlying type
/// of . This parameter is passed uninitialized.
/// to read value in case insensitive mode; to read value in case sensitive mode.
- /// If , considers the value included in metadata attributes such as
- /// [Display] attribute when parsing, otherwise only considers the member names.
+ /// If ,
+ /// considers the value included in attribute
+ /// when parsing, otherwise only considers the member names.
/// if the value parameter was converted successfully; otherwise, .
public static bool TryParse(
#if NETCOREAPP3_0_OR_GREATER
@@ -264,7 +264,6 @@
out global::MyEnum value,
bool allowMatchingMetadataAttribute)
{
-
switch (name)
{
case string s when s.Equals(nameof(global::MyEnum.First), global::System.StringComparison.OrdinalIgnoreCase):
@@ -290,7 +289,6 @@
out global::MyEnum value,
bool allowMatchingMetadataAttribute)
{
-
switch (name)
{
case nameof(global::MyEnum.First):
@@ -345,8 +343,8 @@
///
/// The case-sensitive string representation of the enumeration name or underlying value to convert
/// to read value in case insensitive mode; to read value in case sensitive mode.
- /// If , considers the value included in metadata attributes such as
- /// [Display] attribute when parsing, otherwise only considers the member names.
+ /// If , considers the value included in
+ /// attribute when parsing, otherwise only considers the member names.
/// An object of type whose
/// value is represented by
public static global::MyEnum Parse(
@@ -412,8 +410,8 @@
/// If the parse operation fails, contains the default value of the underlying type
/// of . This parameter is passed uninitialized.
/// to read value in case insensitive mode; to read value in case sensitive mode.
- /// If , considers the value included in metadata attributes such as
- /// [Display] attribute when parsing, otherwise only considers the member names.
+ /// If , considers the value included in
+ /// attribute when parsing, otherwise only considers the member names.
/// if the value parameter was converted successfully; otherwise, .
public static bool TryParse(
#if NETCOREAPP3_0_OR_GREATER
diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.DoesNotGenerateWarningsForObsoleteMembers_CS0618_Issue97.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.DoesNotGenerateWarningsForObsoleteMembers_CS0618_Issue97.verified.txt
index 0be1be2..e9c8e6e 100644
--- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.DoesNotGenerateWarningsForObsoleteMembers_CS0618_Issue97.verified.txt
+++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.DoesNotGenerateWarningsForObsoleteMembers_CS0618_Issue97.verified.txt
@@ -1,4 +1,4 @@
-//------------------------------------------------------------------------------
+//------------------------------------------------------------------------------
//
// This code was generated by the NetEscapades.EnumGenerators source generator
//
@@ -23,20 +23,6 @@
///
public const int Length = 2;
- ///
- /// Returns the string representation of the value.
- /// If the attribute is decorated with a [Display] or [Description]attribute, then
- /// uses the provided value. Otherwise uses the name of the member, equivalent to
- /// calling ToString() on .
- ///
- /// The value to retrieve the string value for
- /// If uses the value provided in the
- /// [Display] or [Description]attribute as the string representation of the member.
- /// If , always uses the name of the member, the same as if ToString() was called.
- /// The string representation of the value
- public static string ToStringFast(this global::MyEnum value, bool useMetadataAttributes)
- => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast();
-
///
/// Returns the string representation of the value.
/// Directly equivalent to calling ToString() on .
@@ -51,6 +37,20 @@
_ => value.AsUnderlyingType().ToString(),
};
+ ///
+ /// Returns the string representation of the value.
+ /// If the member is decorated with the attribute
+ /// then that value is returned. Otherwise returns uses the name of the member,
+ /// equivalent to calling ToString() on .
+ ///
+ /// The value to retrieve the string value for
+ /// If uses the value provided in the
+ /// attribute as the string representation of the member.
+ /// If , always uses the name of the member, the same as if ToString() was called.
+ /// The string representation of the value
+ public static string ToStringFast(this global::MyEnum value, bool useMetadataAttributes)
+ => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast();
+
private static string ToStringFastWithMetadata(this global::MyEnum value)
=> value.ToStringFast();
@@ -82,26 +82,27 @@
///
/// The name to check if it's defined
/// if a member with the name exists in the enumeration, otherwise
- public static bool IsDefined(string name) => IsDefined(name, allowMatchingMetadataAttribute: false);
+ public static bool IsDefined(string name)
+ => name switch
+ {
+ nameof(global::MyEnum.First) => true,
+ nameof(global::MyEnum.Second) => true,
+ _ => false,
+ };
///
/// Returns a boolean telling whether an enum with the given name exists in the enumeration,
- /// or if a member decorated with a [Display] attribute
+ /// or if a member decorated with
/// with the required name exists.
///
/// The name to check if it's defined
- /// If , considers the value of metadata attributes,otherwise ignores them
+ /// If ,
+ /// considers the value of
+ /// instead of the member name, otherwise ignores them
/// if a member with the name exists in the enumeration, or a member is decorated
/// with a [Display] attribute with the name, otherwise
public static bool IsDefined(string name, bool allowMatchingMetadataAttribute)
- {
- return name switch
- {
- nameof(global::MyEnum.First) => true,
- nameof(global::MyEnum.Second) => true,
- _ => false,
- };
- }
+ => IsDefined(name);
#if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0
///
@@ -109,27 +110,25 @@
///
/// The name to check if it's defined
/// if a member with the name exists in the enumeration, otherwise
- public static bool IsDefined(in global::System.ReadOnlySpan name) => IsDefined(name, allowMatchingMetadataAttribute: false);
+ public static bool IsDefined(in global::System.ReadOnlySpan name)
+ => name switch
+ {
+ global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyEnum.First), global::System.StringComparison.Ordinal) => true,
+ global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyEnum.Second), global::System.StringComparison.Ordinal) => true,
+ _ => false,
+ };
///
/// Returns a boolean telling whether an enum with the given name exists in the enumeration,
- /// or optionally if a member decorated with a [Display] attribute
- /// with the required name exists.
- /// Slower then the overload, but doesn't allocate memory./>
+ /// or optionally if a member decorated with
+ /// exists. Slower then the overload, but doesn't allocate memory./>
///
/// The name to check if it's defined
/// If , considers the value of metadata attributes,otherwise ignores them
/// if a member with the name exists in the enumeration, or a member is decorated
/// with a [Display] attribute with the name, otherwise
public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute)
- {
- return name switch
- {
- global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyEnum.First), global::System.StringComparison.Ordinal) => true,
- global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::MyEnum.Second), global::System.StringComparison.Ordinal) => true,
- _ => false,
- };
- }
+ => IsDefined(name);
#endif
///
@@ -241,8 +240,9 @@
/// If the parse operation fails, contains the default value of the underlying type
/// of . This parameter is passed uninitialized.
/// to read value in case insensitive mode; to read value in case sensitive mode.
- /// If , considers the value included in metadata attributes such as
- /// [Display] attribute when parsing, otherwise only considers the member names.
+ /// If ,
+ /// considers the value included in attribute
+ /// when parsing, otherwise only considers the member names.
/// if the value parameter was converted successfully; otherwise, .
public static bool TryParse(
#if NETCOREAPP3_0_OR_GREATER
@@ -264,7 +264,6 @@
out global::MyEnum value,
bool allowMatchingMetadataAttribute)
{
-
switch (name)
{
case string s when s.Equals(nameof(global::MyEnum.First), global::System.StringComparison.OrdinalIgnoreCase):
@@ -290,7 +289,6 @@
out global::MyEnum value,
bool allowMatchingMetadataAttribute)
{
-
switch (name)
{
case nameof(global::MyEnum.First):
@@ -345,8 +343,8 @@
///
/// The case-sensitive string representation of the enumeration name or underlying value to convert
/// to read value in case insensitive mode; to read value in case sensitive mode.
- /// If , considers the value included in metadata attributes such as
- /// [Display] attribute when parsing, otherwise only considers the member names.
+ /// If , considers the value included in
+ /// attribute when parsing, otherwise only considers the member names.
/// An object of type whose
/// value is represented by
public static global::MyEnum Parse(
@@ -412,8 +410,8 @@
/// If the parse operation fails, contains the default value of the underlying type
/// of . This parameter is passed uninitialized.
/// to read value in case insensitive mode; to read value in case sensitive mode.
- /// If , considers the value included in metadata attributes such as
- /// [Display] attribute when parsing, otherwise only considers the member names.
+ /// If , considers the value included in
+ /// attribute when parsing, otherwise only considers the member names.
/// if the value parameter was converted successfully; otherwise, .
public static bool TryParse(
#if NETCOREAPP3_0_OR_GREATER
diff --git a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.HandlesStringsWithQuotesAndSlashesInDescription.verified.txt b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.HandlesStringsWithQuotesAndSlashesInDescription__.verified.txt
similarity index 91%
rename from tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.HandlesStringsWithQuotesAndSlashesInDescription.verified.txt
rename to tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.HandlesStringsWithQuotesAndSlashesInDescription__.verified.txt
index ae8e383..b9b3e9b 100644
--- a/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.HandlesStringsWithQuotesAndSlashesInDescription.verified.txt
+++ b/tests/NetEscapades.EnumGenerators.Tests/Snapshots/EnumGeneratorTests.HandlesStringsWithQuotesAndSlashesInDescription__.verified.txt
@@ -25,20 +25,6 @@ namespace Test
///
public const int Length = 5;
- ///
- /// Returns the string representation of the value.
- /// If the attribute is decorated with a [Display] or [Description]attribute, then
- /// uses the provided value. Otherwise uses the name of the member, equivalent to
- /// calling ToString() on .
- ///
- /// The value to retrieve the string value for
- /// If uses the value provided in the
- /// [Display] or [Description]attribute as the string representation of the member.
- /// If , always uses the name of the member, the same as if ToString() was called.
- /// The string representation of the value
- public static string ToStringFast(this global::Test.StringTesting value, bool useMetadataAttributes)
- => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast();
-
///
/// Returns the string representation of the value.
/// Directly equivalent to calling ToString() on .
@@ -56,6 +42,20 @@ namespace Test
_ => value.AsUnderlyingType().ToString(),
};
+ ///
+ /// Returns the string representation of the value.
+ /// If the member is decorated with the attribute
+ /// then that value is returned. Otherwise returns uses the name of the member,
+ /// equivalent to calling ToString() on .
+ ///
+ /// The value to retrieve the string value for
+ /// If uses the value provided in the
+ /// attribute as the string representation of the member.
+ /// If , always uses the name of the member, the same as if ToString() was called.
+ /// The string representation of the value
+ public static string ToStringFast(this global::Test.StringTesting value, bool useMetadataAttributes)
+ => useMetadataAttributes ? value.ToStringFastWithMetadata() : value.ToStringFast();
+
private static string ToStringFastWithMetadata(this global::Test.StringTesting value)
=> value switch
{
@@ -98,48 +98,41 @@ namespace Test
///
/// The name to check if it's defined
/// if a member with the name exists in the enumeration, otherwise
- public static bool IsDefined(string name) => IsDefined(name, allowMatchingMetadataAttribute: false);
+ public static bool IsDefined(string name)
+ => name switch
+ {
+ nameof(global::Test.StringTesting.Quotes) => true,
+ nameof(global::Test.StringTesting.LiteralQuotes) => true,
+ nameof(global::Test.StringTesting.Backslash) => true,
+ nameof(global::Test.StringTesting.BackslashLiteral) => true,
+ nameof(global::Test.StringTesting.NewLine) => true,
+ _ => false,
+ };
///
/// Returns a boolean telling whether an enum with the given name exists in the enumeration,
- /// or if a member decorated with a [Display] attribute
+ /// or if a member decorated with
/// with the required name exists.
///
/// The name to check if it's defined
- /// If , considers the value of metadata attributes,otherwise ignores them
+ /// If ,
+ /// considers the value of
+ /// instead of the member name, otherwise ignores them
/// if a member with the name exists in the enumeration, or a member is decorated
/// with a [Display] attribute with the name, otherwise
public static bool IsDefined(string name, bool allowMatchingMetadataAttribute)
- {
- var isDefinedInDisplayAttribute = false;
- if (allowMatchingMetadataAttribute)
- {
- isDefinedInDisplayAttribute = name switch
- {
- "Quotes \"" => true,
- "Literal Quotes \"" => true,
- "Backslash \\" => true,
- "LiteralBackslash \\" => true,
- "New\nLine" => true,
- _ => false,
- };
- }
+ => allowMatchingMetadataAttribute ? IsMetadataNameDefined(name) : IsDefined(name);
- if (isDefinedInDisplayAttribute)
+ private static bool IsMetadataNameDefined(string name)
+ => name switch
{
- return true;
- }
-
- return name switch
- {
- nameof(global::Test.StringTesting.Quotes) => true,
- nameof(global::Test.StringTesting.LiteralQuotes) => true,
- nameof(global::Test.StringTesting.Backslash) => true,
- nameof(global::Test.StringTesting.BackslashLiteral) => true,
- nameof(global::Test.StringTesting.NewLine) => true,
- _ => false,
+ "Quotes \"" => true,
+ "Literal Quotes \"" => true,
+ "Backslash \\" => true,
+ "LiteralBackslash \\" => true,
+ "New\nLine" => true,
+ _ => IsDefined(name),
};
- }
#if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0
///
@@ -147,49 +140,38 @@ namespace Test
///
/// The name to check if it's defined
/// if a member with the name exists in the enumeration, otherwise
- public static bool IsDefined(in global::System.ReadOnlySpan name) => IsDefined(name, allowMatchingMetadataAttribute: false);
+ public static bool IsDefined(in global::System.ReadOnlySpan name)
+ => name switch
+ {
+ global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::Test.StringTesting.Quotes), global::System.StringComparison.Ordinal) => true,
+ global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::Test.StringTesting.LiteralQuotes), global::System.StringComparison.Ordinal) => true,
+ global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::Test.StringTesting.Backslash), global::System.StringComparison.Ordinal) => true,
+ global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::Test.StringTesting.BackslashLiteral), global::System.StringComparison.Ordinal) => true,
+ global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, nameof(global::Test.StringTesting.NewLine), global::System.StringComparison.Ordinal) => true,
+ _ => false,
+ };
///
/// Returns a boolean telling whether an enum with the given name exists in the enumeration,
- /// or optionally if a member decorated with a [Display] attribute
- /// with the required name exists.
- /// Slower then the overload, but doesn't allocate memory./>
+ /// or optionally if a member decorated with
+ /// exists. Slower then the overload, but doesn't allocate memory./>
///
/// The name to check if it's defined
/// If , considers the value of metadata attributes,otherwise ignores them
/// if a member with the name exists in the enumeration, or a member is decorated
/// with a [Display] attribute with the name, otherwise
- public static bool IsDefined(in global::System.ReadOnlySpan name, bool allowMatchingMetadataAttribute)
- {
- var isDefinedInDisplayAttribute = false;
- if (allowMatchingMetadataAttribute)
- {
- isDefinedInDisplayAttribute = name switch
- {
- global::System.ReadOnlySpan current when global::System.MemoryExtensions.Equals(current, "Quotes \"", global::System.StringComparison.Ordinal) => true,
- global::System.ReadOnlySpan