C# 检查枚举是否具有等于字符串的字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/328025/
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-03 23:22:40  来源:igfitidea点击:

Check if an enum has a field that equals a string

c#enums

提问by Boris Callens

I have an enum

我有一个枚举

public enum FileExtentions {
    mp3,
    mpeg
}

And I have a FileInfo of which I want to check if the extension is in the previous enum. I was hoping I could do a

我有一个 FileInfo,我想检查它的扩展名是否在上一个枚举中。我希望我能做一个

FileExtensions.Any(e=>e.ToString().Equals(file.Extension));

But that would have been too awesome. Any ideas?

但这太棒了。有任何想法吗?

采纳答案by Konrad Rudolph

What's the reason behind AnyEquals? Did you overlook Contains?

背后的原因是什么Any…… Equals?你忽略了Contains吗?

bool result = Enum.GetNames(typeof(FileExtensions)).Contains("mp3");

回答by Boris Callens

While pressing submit I thought of the answer myself:

在按下提交时,我自己想到了答案:

Enum.GetNames(typeof(FileExtensions)).Any(f=>f.Equals("."+file.Extension))

回答by Jeff Donnici

You can do that with the System.Enum type:

你可以用 System.Enum 类型做到这一点:

string extension = Enum.GetName(typeof(FileExtensions), FileExtensions.mp3);

if (extension == file.Extension)
  // do something here

Update:

更新:

Ah, I misunderstood that you want to check the entire enum for the extension and not that a specific enum value matches the extension. In that case, your approach with GetNames()is the route - sorry you can't mark your own answer as accepted for your own question. ;-)

啊,我误解了您要检查扩展名的整个枚举,而不是特定的枚举值与扩展名匹配。在这种情况下,您使用GetNames() 的方法是路线 - 抱歉,您无法将自己的答案标记为已接受您自己的问题。;-)

回答by huseyint

You can extend the FileInfo type with the following extension method:

您可以使用以下扩展方法扩展 FileInfo 类型:

public static bool HasExtension(this FileInfo file)
{
    var ext = file.Extension.StartsWith(".") ? file.Extension.Substring(1) 
                                             : file.Extension;

    return Enum.GetNames(typeof(FileExtensions))
               .Any(f => f.Equals(ext));
}

and use it like:

并使用它:

bool hasExtension = file.HasExtension();

回答by Mark Brackett

Enum.IsDefinedwill take a string containing the name of an enum value. The only ugliness is that you have to strip the leading period off of File.Extensionand it's case sensitive:

Enum.IsDefined将采用包含枚举值名称的字符串。唯一的丑陋之处是您必须从File.Extension 中去除前导句点,并且区分大小写:

Enum.IsDefined(typeof(FileExtension), file.Extension.Substring(1).ToLower())

Edit: Extension method goodness to get close to your desired syntax:

编辑:扩展方法优点以接近您想要的语法:

IEnumerable<string> GetNames(this Type t) {
   if (!t.IsEnum) throw new ArgumentException();

   return Enum.GetNames(t);
}

typeof(FileExtensions).GetNames().Any(e=>e.ToString().Equals(file.Extension));

Personally, though, I'd still rather the IsDefined route:

不过,就我个人而言,我仍然更喜欢 IsDefined 路线:

bool IsDefined(this Type t, string name) {
   if (!t.IsEnum) throw new ArgumentException();

   return Enum.IsDefined(t, name);
}

typeof(FileExtension).IsDefined(file.Extension);

回答by Срба

The best way is to use Enum.IsDefined function. It pretty easy, in your case:

最好的方法是使用 Enum.IsDefined 函数。这很容易,就你而言:

if (Enum.IsDefined(typeof(FileExtentions), file.Extension))

回答by Ali Karaca

General solution:

一般解决方案:

Enum.IsDefined(Type, Object)

Your solution is:

您的解决方案是:

Enum.IsDefined(typeof(FileExtentions), FileInfo)