为什么将对象中存储的枚举转换为 int 返回字符串?
我有一个 object
类型的属性,其中包含一个 Enum
值,当我使用 (int)value
对其进行转换时,它返回一个 <枚举名称的 code>string 。为什么?
我注意到这一点的代码位于
编辑
这是一个快速示例。我评论了放置断点的位置,并使用立即窗口来确定输出是什么。
MainWindow.xaml.cs
public partial class MainWindow : Window
{
public Int32 SomeNumber { get; set; }
public MainWindow()
{
InitializeComponent();
SomeNumber = 1;
RootWindow.DataContext = this;
}
}
public enum MyEnum
{
Value1 = 1,
Value2 = 2,
Value3 = 3
}
/// <summary>
/// Returns true if the int value equals the Enum parameter, otherwise returns false
/// </summary>
public class IsIntEqualEnumParameterConverter : IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (parameter == null || value == null) return false;
if (parameter.GetType().IsEnum && value is int)
{
// Breakpoint here
return (int)parameter == (int)value;
}
return false;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion
}
MainWindow.xaml
<Window x:Class="WpfApplication5.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication5"
Title="MainWindow" Height="350" Width="525"
x:Name="RootWindow">
<Window.Resources>
<local:IsIntEqualEnumParameterConverter x:Key="IsIntEqualEnumParameterConverter" />
</Window.Resources>
<StackPanel>
<TextBlock Text="{Binding SomeNumber, Converter={StaticResource IsIntEqualEnumParameterConverter}, ConverterParameter={x:Static local:MyEnum.Value1}}" />
</StackPanel>
</Window>
编辑 #2
只是希望消除一些混乱...
我说它返回一个字符串,因为运行 ?((int)parameter)
在立即窗口中返回枚举名称,而运行 ?System.Convert.ToInt32(parameter)
正确显示 int。
后来我发现它实际上一直在正确地评估 DataTrigger。我认为这不是因为我的控件在运行时不可见,但我发现这是因为我的 XAML 中存在错误(我忘记了 Grid.Column 属性,因此一个控件与另一个控件重叠) )。
很抱歉提出了令人困惑的问题。
编辑 #3
这是一些控制台应用程序代码,专门为 Jon 演示了情况:)
class Program
{
static void Main(string[] args)
{
object value;
value = Test.Value1;
// Put breakpoint here
// Run ?(int)value vs Convert.ToInt32(value) in the immediate window
// Why does the first return Value1 while the 2nd returns 1?
Console.ReadLine();
}
}
public enum Test
{
Value1 = 1
}
I have a property of type object
, which contains an Enum
value, and when I cast it using (int)value
, it returns a string
of the Enum's name. Why?
The code where I noticed this is in this answer. Using Convert.ToInt32()
correctly casts the Enum
to an int
, but I was just curious why I would get a string back when using (int)
. It doesn't even throw me an error.
Edit
Here's a quick sample. I commented where I put the breakpoint, and was using the immediate window to determine what the output was.
MainWindow.xaml.cs
public partial class MainWindow : Window
{
public Int32 SomeNumber { get; set; }
public MainWindow()
{
InitializeComponent();
SomeNumber = 1;
RootWindow.DataContext = this;
}
}
public enum MyEnum
{
Value1 = 1,
Value2 = 2,
Value3 = 3
}
/// <summary>
/// Returns true if the int value equals the Enum parameter, otherwise returns false
/// </summary>
public class IsIntEqualEnumParameterConverter : IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (parameter == null || value == null) return false;
if (parameter.GetType().IsEnum && value is int)
{
// Breakpoint here
return (int)parameter == (int)value;
}
return false;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion
}
MainWindow.xaml
<Window x:Class="WpfApplication5.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication5"
Title="MainWindow" Height="350" Width="525"
x:Name="RootWindow">
<Window.Resources>
<local:IsIntEqualEnumParameterConverter x:Key="IsIntEqualEnumParameterConverter" />
</Window.Resources>
<StackPanel>
<TextBlock Text="{Binding SomeNumber, Converter={StaticResource IsIntEqualEnumParameterConverter}, ConverterParameter={x:Static local:MyEnum.Value1}}" />
</StackPanel>
</Window>
Edit #2
Just hoping to clear up some confusion...
I said it was returning a string because running ?((int)parameter)
in the Immediate Window was returning the enum name, while running ?System.Convert.ToInt32(parameter)
was correctly displaying the int.
I found afterwards that it was actually evaluating correctly to the DataTrigger all along. I thought it wasn't because my control wasn't visible at runtime, however I discovered that was because of an error in my XAML (I forgot a Grid.Column
property, so one control was overlapping another).
Sorry for the confusing question.
Edit #3
Here's some console app code demonstrating the situation just for Jon :)
class Program
{
static void Main(string[] args)
{
object value;
value = Test.Value1;
// Put breakpoint here
// Run ?(int)value vs Convert.ToInt32(value) in the immediate window
// Why does the first return Value1 while the 2nd returns 1?
Console.ReadLine();
}
}
public enum Test
{
Value1 = 1
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
听起来您好像被“立即窗口”欺骗了。目前尚不清楚您在立即窗口中做了什么,但我可以绝对肯定地说,如果您转换为
int
,您会< em>没有取回字符串
。类型系统完全阻止了这种情况的发生。It sounds like you're being duped by the Immediate Window. It's not clear exactly what you did in the Immediate Window, but I can say with absolute certainty that if you cast to an
int
you do not get astring
back. The type system completely prevents that from happening.从技术上讲,您在这里描述的情况是不可能的。您看到的是调试器显示的枚举成员的名称。把这想象成一种“语法糖”。所以恕我直言,真正的问题是整数不相等。
要检查这一点:
读取您的“字符串”,检查声明的枚举中分配给的整数,并检查整数是否实际上等于您在函数中获得的参数。
It's technically impossible what you describe here. What you see is the NAME of enumeration member which is shown by debugger. Think of this like kind of "syntax sugare". So the real problem IMHO, is that just to ints are not equal.
To check this:
Read your "string", Check integer assigned to in enumeration declared, and ceck if integer actually equals to that one you get like a parameter in the function.
我意识到你的问题实际上是“为什么”而不是“如何”。
但是,方法如下:
Your question is really "why" and not "how", I realize that.
However, here's how:
我已经忘记了运行此代码示例,并且发现它每次都返回 false,因为
value is int
失败。 value 是一个对象,而不是整数。I've run this code sample into oblivion, and I'm finding it to return false each time because the
value is int
is failing. value is an object, not an integer.我想发生的事情是您的
对象参数
作为字符串
值传入,因此您的调用是错误的,您跳过正文并
返回错误< /code> 直接。
相反,使用 Enum.Parse() 或 Convert.ToInt32() 将字符串更改为 enum 值或 int 值,然后进行比较。
I imagine what is happening is that your
object parameter
is coming in as astring
value and thus your call tois false and you skip the body and
return false
directly.Instead, use Enum.Parse() or Convert.ToInt32() to change the string into either the enum value or the int value and then compare.