C# 显式转换字符串到枚举

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1061228/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 07:17:16  来源:igfitidea点击:

C# explicit cast string to enum

c#.net-3.5enumscasting

提问by Boris Gougeon

I would like to have an explicit cast between from a string to an enum in c# in order to have this :

我想在 c# 中从字符串到枚举之间进行显式转换,以便:

(MyEnum) Enum.Parse(typeof(MyEnum),stringValue)

I would like to deport this into an explicit cast operator, I did this but didn't work :

我想将其驱逐到显式转换运算符中,我这样做了但没有用:

public static explicit operator (MyEnum)(value stringValue){
     return (MyEnum) Enum.Parse(typeof(MyEnum),stringValue);
}

Do you know if it's possible in C# using .NET 3.5?

您知道在 C# 中使用 .NET 3.5 是否可行吗?

采纳答案by jason

A cast is not possible. The issue is that a user-defined conversion must be enclosed in a struct or class declaration, and the conversion must be to or from the enclosing type. Thus,

无法进行演员表。问题是用户定义的转换必须包含在结构或类声明中,并且转换必须是与封闭类型之间的转换。因此,

public static explicit operator MyEnum(string value)

is impossible because neither MyEnumnor stringcan ever be the enclosing type.

是不可能的,因为既不能MyEnumstring不能是封闭类型。

The relevant section of the ECMA334 C# spec is 17.9.4:

ECMA334 C# 规范的相关部分是 17.9.4:

A conversion operator converts from a source type, indicated by the parameter type of the conversion operator, to a target type, indicated by the return type of the conversion operator. A class or struct is permitted to declare a conversion from a source type S to a target type T only if all of the following are true, where S0 and T0 are the types that result from removing the trailing ? modifiers, if any, from S and T:

转换运算符将源类型(由转换运算符的参数类型指示)转换为目标类型(由转换运算符的返回类型指示)。仅当以下所有条件都为真时,才允许类或结构声明从源类型 S 到目标类型 T 的转换,其中 S0 和 T0 是删除尾随的 ? 来自 S 和 T 的修饰符,如果有的话:

S0 and T0 are different types.

S0 和 T0 是不同的类型。

Either S0 or T0 is the class or struct type in which the operator declaration takes place.

S0 或 T0 是发生运算符声明的类或结构类型

Neither S0 nor T0 is an interface-type.

S0 和 T0 都不是接口类型。

Excluding user-defined conversions, a conversion does not exist from S to T or from T to S.

排除用户定义的转换,不存在从 S 到 T 或从 T 到 S 的转换。

However, you can do an extension method on the string class.

但是,您可以对字符串类执行扩展方法。

public static class StringExtensions {
    public static T ConvertToEnum<T>(this string value)  {
        Contract.Requires(typeof(T).IsEnum);
        Contract.Requires(value != null);
        Contract.Requires(Enum.IsDefined(typeof(T), value));
        return (T)Enum.Parse(typeof(T), value);
    }
}

回答by Andy

Is it necessary to use a cast operator? Another option would be to add an extension method off of string:

是否有必要使用强制转换运算符?另一种选择是从字符串中添加一个扩展方法:

public static class StringEnumConversion
{
    public static T Convert<T>(this string str)
    {
        return (T)Enum.Parse(typeof(T), str);
    }
}

回答by andy

You could also use "StringValue" attributes for get what you want.

您还可以使用“StringValue”属性来获取您想要的内容。

Check out this question: stackoverflow.com/questions/424366/c-string-enums, there's plenty of info there that might help.

看看这个问题:stackoverflow.com/questions/424366/c-string-enums,那里有很多可能有帮助的信息。

cheers

干杯