反射 - 直接为枚举的整数值 [英] Reflection - integer values directly to enum
问题描述
以下代码位于自定义反序列化器中:
对象值=(int)1;
字符串nameToParse = Enum.GetName(field.FieldType ,值);
value = Enum.Parse(field.FieldType,nameToParse);
目前我们按照以下路径:
intValue --enum name --enum value
有没有办法反思:
intValue - -enum value
The following code is in a custom deserializer:
object value = (int) 1;
string nameToParse = Enum.GetName(field.FieldType, value);
value = Enum.Parse(field.FieldType, nameToParse);
Currently we follow the path below:
intValue --enum name --enum value
Is there any way in reflection to do:
intValue --enum value
推荐答案
tobiwan_kenobi写道:
tobiwan_kenobi wrote:
以下代码在自定义反序列化器中:
对象值=(int)1;
字符串nameToParse = Enum.GetName(field.FieldType,value);
value = Enum.Parse(field.FieldType,nameToParse);
目前我们按照以下路径:
intValue --enum name --enum value
有没有办法反思:
intValue --enum value
The following code is in a custom deserializer:
object value = (int) 1;
string nameToParse = Enum.GetName(field.FieldType, value);
value = Enum.Parse(field.FieldType, nameToParse);
Currently we follow the path below:
intValue --enum name --enum value
Is there any way in reflection to do:
intValue --enum value
你可以从int转换为enum,不是吗?
这个场景一直对我有用:
public enum TestEnum
{
一个= 1,
两个
}
public TestMyEnum()
{
TestEnum a =(TestEnum)1;
TestEnum b =(TestEnum)2;
Console.WriteLine(" A:" + a.ToString()+" \ nB:" + b.ToString());
}
Chris。
You can cast to the enum from an int, can''t you?
This scenario has always worked for me:
public enum TestEnum
{
One = 1,
Two
}
public TestMyEnum()
{
TestEnum a = (TestEnum)1;
TestEnum b = (TestEnum)2;
Console.WriteLine("A: " + a.ToString() + "\nB: " + b.ToString());
}
Chris.
Chris Shepherd< ch ** @ nospam.chsh.cawrote:
Chris Shepherd <ch**@nospam.chsh.cawrote:
intValue --enum value
intValue --enum value
你可以从int转换为enum,不是吗?
You can cast to the enum from an int, can''t you?
只有在编译时知道枚举类型 - 它看起来像是
OP不会。
不幸的是Convert.ChangeType不起作用,这是我的第一个
的想法。我怀疑使用Expression.Convert可以使用一些
的工作,但不幸的是晚餐正在打电话给我。时间让Marc拿走
结束,我会说:)
(说真的,一定有更好的办法,但我不是知道它... ...
-
Jon Skeet - < sk *** @ pobox.com>
网站: http://www.pobox.com/~skeet
博客: http://www.msmvps.com/jon .skeet
C#深度: http://csharpindepth.com
Only if you know the enum type at compile time - which it looks like
the OP doesn''t.
Unfortunately Convert.ChangeType doesn''t work, which was my first
thought. I suspect that using Expression.Convert would work with a bit
of work, but unfortunately dinner is calling me. Time for Marc to take
over, I''d say :)
(Seriously, there must be a better way, but I don''t know it offhand...)
--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
不,在这种情况下我们不能施展,因为我们正在使用反射写作
a一般例程将创建一个任何需要获取的枚举的实例
反序列化,所以我们不知道它是哪个枚举。在反序列化时从XML文档中恢复的枚举类型是
。
" Chris Shepherd"写道:
No, in this situation we can''t cast because we are using reflection to write
a general routine that will create an instance of any enum that needs to get
deserialized, so we don''t know which enum it is. The type of enum is
recovered from the XML document at deserialization time.
"Chris Shepherd" wrote:
tobiwan_kenobi写道:
tobiwan_kenobi wrote:
以下代码位于自定义反序列化器中:
对象值=(int)1;
string nameToParse = Enum.GetName(field.FieldType,value);
value = Enum。 Parse(field.FieldType,nameToParse);
目前我们遵循以下路径:
intValue --enum name --enum value
有没有办法反思:
intValue --enum value
The following code is in a custom deserializer:
object value = (int) 1;
string nameToParse = Enum.GetName(field.FieldType, value);
value = Enum.Parse(field.FieldType, nameToParse);
Currently we follow the path below:
intValue --enum name --enum value
Is there any way in reflection to do:
intValue --enum value
你可以从int转换为enum,不是吗?
这个场景一直对我有用:
public enum TestEnum
{
一个= 1,
两个
}
public TestMyEnum()
{
TestEnum a =(TestEnum)1;
TestEnum b =(TestEnum)2;
Console.WriteLine(" A:" + a.ToString ()+" \ nB:" + b.ToString());
}
Chris。
You can cast to the enum from an int, can''t you?
This scenario has always worked for me:
public enum TestEnum
{
One = 1,
Two
}
public TestMyEnum()
{
TestEnum a = (TestEnum)1;
TestEnum b = (TestEnum)2;
Console.WriteLine("A: " + a.ToString() + "\nB: " + b.ToString());
}
Chris.
这篇关于反射 - 直接为枚举的整数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!