简体   繁体   English

是否可以为通用Enum类型编写扩展方法?

[英]Is it possible to write an extension method for a generic Enum type?

Over on this StackOverflow question , I found this really nice helper method for parsing a string into a generic Enum value: 这个StackOverflow问题上 ,我发现了一个非常好的帮助程序方法,用于将字符串解析为通用Enum值:

public static T ParseEnum<T>(string value)
{
    return (T) Enum.Parse(typeof(T), value, true);
}

I'd like to change this from a helper method into an extension method , but I'm not all that familiar with using generics, so I'm not sure if/how that would work. 我想将其从辅助方法更改为扩展方法 ,但我对使用泛型并不十分熟悉,因此不确定是否/如何使用泛型。

I tried this, but as you'll quickly see this code doesn't actually compile: 我尝试过此方法,但是您很快就会看到此代码实际上并未编译:

public static T ParseEnum(this T myEnum, string value)
{
    return (T) Enum.Parse(typeof(myEnum), value, true);
}

And I also tried this, but this doesn't do it either: 我也尝试过这样做,但这也不起作用:

public static Enum ParseEnum(this Enum myEnum, string value)
{
    return Enum.Parse(typeof(myEnum), value, true);
}

Is it even possible to do what I'm asking about here? 甚至可以做我在这里问的事情吗?

When writing an extension method, the first thing you need to do is figure out how you want to call it. 编写扩展方法时,您需要做的第一件事就是弄清楚如何调用它。 Ie what are you extending? 在扩大?

In the case of parsing a string and returning an enum value, the only thing that really makes sense is to extend the string class. 在解析string并返回enum值的情况下,真正有意义的唯一事情就是扩展string类。 In that view, this is what the extension method would look like: 在该视图中,扩展方法将如下所示:

    public static T ParseEnum<T>(this string text)
    {
        return (T)Enum.Parse(typeof(T), text);
    }

You would call it like this: 您可以这样称呼它:

Pets pet = "Cat".ParseEnum<Pets>();

Another alternative uses type inference, at the cost of a by-reference argument (which I prefer to avoid): 另一种选择是使用类型推断,但要以引用参数为代价(我希望避免):

    public static void ParseEnum<T>(this string text, out T t)
    {
        t = (T)Enum.Parse(typeof(T), text);
    }

You would call that like this: 您会这​​样称呼:

Pets pet;

"Cat".ParseEnum(out pet);

IMHO this is not as good, even though you get type inference, because you can't use the method call in an expression. 恕我直言,即使您得到类型推断,它也不是很好,因为您不能在表达式中使用方法调用。

Note that the syntax Pets.ParseEnum("Cat") is not going to work, because Pets is a type name, not a type instance, and you can extend only based on instances of objects. 请注意,语法Pets.ParseEnum("Cat")无效,因为Pets是类型名称,而不是类型实例,并且只能基于对象实例进行扩展。 But perhaps the above example is close enough (it's about the same number of characters to type anyway :) ) 但是上面的示例也许足够接近(无论如何键入的字符数都差不多:))

You can rename your helper method to Enum<> to make it look like it's extending Enum. 您可以将您的辅助方法重命名为Enum <>,以使其看起来像在扩展Enum。

/// <summary> Enum Extension Methods </summary>
/// <typeparam name="T"> type of Enum </typeparam>
public class Enum<T> where T : struct, IConvertible
{
    public static T Parse(string input)
    {
        return (T)Enum.Parse(typeof(T), input, true);
    }
}

Then use it the same way as the helper method the OP mentioned: 然后以与OP提到的辅助方法相同的方式使用它:

var result = Enum<StatusEnum>.Parse("Active");

This isn't an extension method. 这不是扩展方法。 Yet, it shares one of the goals of extension methods. 但是,它具有扩展方法的目标之一。 Naming a class close to Enum tries to help discoverability. 将类命名为接近Enum会尝试帮助发现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM