C# 如何在转换器中将对象强制转换为 int?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/915457/
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
How do I cast an object to an int in a Converter?
提问by Edward Tanguay
I give up, how do I cast this?
我放弃了,我怎么投这个?
class AmountIsTooHighConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
//int amount = (int)value;
//int amount = (int)(string)value;
//int amount = (int)(value.ToString);
//int amount = Int32.Parse(value);
//int amount = (int)Convert.ChangeType(value, typeof(int));
//int amount = Convert.ToInt32(value);
if (amount >= 35)
{
return true;
}
else
{
return false;
}
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return null;
}
}
采纳答案by Thomas Levesque
Both Convert.ToInt32
or Int32.Parse
should work... If they don't, then the value is definitely not an int ;)
Try to put a breakpoint in your converter to watch the value, it might show you why it doesn't work
两者Convert.ToInt32
或Int32.Parse
都应该工作......如果他们不这样做,那么该值绝对不是一个 int ;)
尝试在您的转换器中放置一个断点以查看该值,它可能会告诉您为什么它不起作用
回答by Darin Dimitrov
Put a breakpoint at the opening parenthesis of your Convert method. Start your application in debug mode. When the breakpoint is hit analyze the actual type of value. Do the cast. If value is string cast it to string and then parse the result to get an integer. Run the program again. It should work this time.
在 Convert 方法的左括号处放置一个断点。在调试模式下启动您的应用程序。当断点被击中时,分析value的实际类型。做演员。如果值为字符串,则将其转换为字符串,然后解析结果以获取整数。再次运行程序。这次应该可以了。
回答by Guffa
If the value is actually a string object (with a content that represents an integer value), this gives the least overhead:
如果该值实际上是一个字符串对象(具有表示整数值的内容),则开销最小:
int amount = Int32.Parse((string)value);
The Convert.ToInt32
should be able to handle most anything that is possible to convert to an int
, like a string containing digits or any numeric type that is within the range that an int
can handle.
TheConvert.ToInt32
应该能够处理大多数可以转换为 an 的东西int
,比如包含数字的字符串或在 anint
可以处理的范围内的任何数字类型。
回答by focaccine86
If the value is an object, ToString()
is better to convert to string before parsing to Int
int amount = Int32.Parse(value.ToString());
如果值是一个对象,ToString()
最好先转换成字符串再解析Int
int amount = Int32.Parse(value.ToString());
回答by asdf
The answer is basically simple. Because the IValueConverter
has itself a method called Convert
you cannot simply us the Convert
object just like that.
答案基本上很简单。因为IValueConverter
它本身有一个调用的方法Convert
,所以不能Convert
像那样简单地使用对象。
But if you add the correct prefix it works:
但是,如果您添加正确的前缀,它会起作用:
int amount = System.Convert.ToInt32(value);
回答by J.Memisevic
int amount;
if(int32.TryParse((string)value,out amount)
{
if(amount>=35)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
This is safest way, I think!
我认为这是最安全的方法!