如何检查变量是否为枚举 [英] How to check if a variable is an Enum

查看:113
本文介绍了如何检查变量是否为枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法检查某个变量是否为枚举?


示例代码:


public enum MyEnum {

星期一,

星期二,

星期三

}

public void MyMethod()

{

MyEnum myEnum = MyEnum.Monday;

string myString =" Bla bla bla";

bool firstTry = IsEnum(myEnum);

bool secondTry = IsEnum(myString);

}


有没有办法写一个函数IsEnum(),以便firstTry为true,并且
secondTry为false?


myEnum.GetType()告诉我myEnum属于MyEnum类型。

GetTypeCode()告诉我它是一个Int32。


谢谢,


-Patrick

Is there a way to check if a certain variable is an enum?

Example code:

public enum MyEnum {
Monday,
Tuesday,
Wednesday
}
public void MyMethod()
{
MyEnum myEnum = MyEnum.Monday;
string myString = "Bla bla bla";
bool firstTry = IsEnum(myEnum);
bool secondTry = IsEnum(myString);
}

Is there a way to write a function IsEnum() so that firstTry is true and
secondTry is false?

myEnum.GetType() tells me that myEnum is of the type MyEnum.
GetTypeCode() tells me that it''s an Int32.

Thanks,

-Patrick

推荐答案

Patrick,


我的猜测是你可以在
问题 - 它应该在链中有System.Enum。


- < br $>
真诚的,

Dmitriy Lapshin [C#/ .NET MVP]

今天将单元测试的强大功能带到VS .NET IDE! /> http://www.x-unity.net/teststudio.aspx


" Patrick B" <是ne ******* @ devzoo.com>在消息中写道

新闻:Op ************** @ TK2MSFTNGP09.phx.gbl ...
Patrick,

My guess is that you can check the inheritance chain for the type in
question - it should have System.Enum in the chain.

--
Sincerely,
Dmitriy Lapshin [C# / .NET MVP]
Bring the power of unit testing to the VS .NET IDE today!
http://www.x-unity.net/teststudio.aspx

"Patrick B" <ne*******@devzoo.com> wrote in message
news:Op**************@TK2MSFTNGP09.phx.gbl...
有没有办法检查某个变量是否为枚举?

示例代码:

public enum MyEnum {
Monday,
Tuesday,
Wednesday
}
public void MyMethod()
{
MyEnum myEnum = MyEnum.Monday;
string myString =" Bla bla bla" ;;
bool firstTry = IsEnum(myEnum);
bool secondTry = IsEnum(myString);
}
是否有办法编写函数IsEnum(),以便firstTry为true并且
secondTry是假的?

myEnum.GetType()告诉我myEnum属于MyEnum类型。 GetTypeCode()
告诉我它是一个Int32。

谢谢,

-Patrick
Is there a way to check if a certain variable is an enum?

Example code:

public enum MyEnum {
Monday,
Tuesday,
Wednesday
}
public void MyMethod()
{
MyEnum myEnum = MyEnum.Monday;
string myString = "Bla bla bla";
bool firstTry = IsEnum(myEnum);
bool secondTry = IsEnum(myString);
}

Is there a way to write a function IsEnum() so that firstTry is true and
secondTry is false?

myEnum.GetType() tells me that myEnum is of the type MyEnum. GetTypeCode()
tells me that it''s an Int32.

Thanks,

-Patrick






嗨帕特里克,


Enum.IsDefined(typeof(MyEnum),myValue)


Roland


" Patrick B" schrieb:
Hi Patrick,

Enum.IsDefined(typeof(MyEnum), myValue)

Roland

"Patrick B" schrieb:
有没有办法检查某个变量是否为枚举?

示例代码:

public enum MyEnum {
周一,周二,
周三
}
公开无效MyMethod()
{
MyEnum myEnum = MyEnum.Monday;
string myString =" Bla bla bla" ;;
bool firstTry = IsEnum(myEnum);
bool secondTry = IsEnum(myString);
}

是有一种方法可以编写一个函数IsEnum(),以便firstTry为true,并且第二次尝试是假的吗?

myEnum.GetType()告诉我myEnum属于MyEnum类型。
GetTypeCode()告诉我它是Int32。
谢谢,

-Patrick
Is there a way to check if a certain variable is an enum?

Example code:

public enum MyEnum {
Monday,
Tuesday,
Wednesday
}
public void MyMethod()
{
MyEnum myEnum = MyEnum.Monday;
string myString = "Bla bla bla";
bool firstTry = IsEnum(myEnum);
bool secondTry = IsEnum(myString);
}

Is there a way to write a function IsEnum() so that firstTry is true and
secondTry is false?

myEnum.GetType() tells me that myEnum is of the type MyEnum.
GetTypeCode() tells me that it''s an Int32.

Thanks,

-Patrick



Roland,


有没有办法使用IsDefined,即使我没有myValue来检查

反对呢?我不想检查myValue是否已定义,如果Enum本身已定义,我想检查




谢谢,


Patrick

Roland Polzer写道:
Roland,

Is there a way to use IsDefined even if I don''t have a myValue to check
against it? I don''t want to check if myValue is defined, I want to check
if the Enum itself is defined.

Thanks,

Patrick
Roland Polzer wrote:
嗨Patrick,

Enum.IsDefined(typeof(MyEnum),myValue )

Roland

Patrick B schrieb:
Hi Patrick,

Enum.IsDefined(typeof(MyEnum), myValue)

Roland

"Patrick B" schrieb:



这篇关于如何检查变量是否为枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆