English 中文(简体)
How to Enum. GetValues in .Net 3.5?
原标题:How to Enum.GetValues in .Net 3.5?

In .Net 4.0, I use System.Enum.GetValues(typeof(Gender)) to get the list of enum item.
In a complete example, I use to look for enum value in this way:

    Gender retVal = Gender.Male;

    foreach (Gender enumType in System.Enum.GetValues(typeof(Gender)))
    {
        if (enumType.ToString().Trim().ToUpper().Substring(0,1).Equals(stringToEnum.Trim().ToUpper()))
        {
            retVal = enumType;
            break;
        }
    }  

But how could I do this in .Net 3.5 (Pocket PC) ?
Thanks in advance !

我使用以下答案,但我没有工作。 该法典:

Enum Values:

namespace Ionic.Zlib
{
    public enum CompressionLevel
    {
        Level0 = 0,
        None = 0,
        Level1 = 1,
        BestSpeed = 1,
        Level2 = 2,
        Level3 = 3,
        Level4 = 4,
        Level5 = 5,
        Level6 = 6,
        Default = 6,
        Level7 = 7,
        Level8 = 8,
        BestCompression = 9,
        Level9 = 9,
    }
}  

使用:

我只是错了新物体的开端。 现在这项工作:

public static Ionic.Zlib.CompressionLevel GetCompressionLevel(string Expression)
{
    Ionic.Zlib.CompressionLevel result = Ionic.Zlib.CompressionLevel.None;
    foreach (Ionic.Zlib.CompressionLevel item in EnumGetValues(new Ionic.Zlib.CompressionLevel()))
    {
        if(object.Equals(item.ToString().Trim().ToUpper(), Expression.Trim().ToUpper()))
        {
            result = item;
            break;
        }
    }
    return result;
}
最佳回答
问题回答

除复式回答外,我建议采用固定的方法,采用通用的参数。 这样,你就不需要将大体类型的变数传给方法:

public static class EnumHelper
{
    public static TEnum[] GetValues<TEnum>()
    {
        return typeof(TEnum)
            .GetFields(BindingFlags.Static | BindingFlags.Public)
            .Select(fieldInfo => (TEnum)fieldInfo.GetValue(null))
            .ToArray();
    }
}

通常我不喜欢以“Helper”结束的班级,而只是使用。 Enum将与以下编码相冲突:-等。

2. 采用这一方法,仅以你的大体类型援引:

var values = EnumHelper.GetValues<MyEnum>();




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签