有趣的C#难题 [英] Interesting C# puzzle

查看:96
本文介绍了有趣的C#难题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个字符串值,我想对它进行范围比较:


string minValue =" 1050";

string maxValue =" ; 5300" ;;

string theValue =" 3239" ;;


我有另一个字符串告诉我我需要转换上面的类型

比较之前的字符串:


string convertToType =" int";


代码不能假设上述任何内容价值或它必须转换为比较的类型,除了类型是支持类型在C#中的
。所以,从逻辑上讲你会做类似的事情:


object oMin = Convert.ChangeType(minValue,Type.GetType(convertToType));

//等等给你3个实际类型为int的对象变量


问题在于比较。您无法执行以下操作,因为对象

不支持运营商:

bool isInRange =(oValue> = oMin)&& (oValue< = oMax);


所以,除了创建一个巨大的switch()语句,每个

可能类型的情况下,你需要支持,你将如何在比较之上执行


I have 3 string values that I want to perform a range comparison on:

string minValue = "1050";
string maxValue = "5300";
string theValue = "3239";

I have another string that tells me the type I need to convert the above
strings to before comparing:

string convertToType = "int";

The code cannot assume anything about the above values or the type that it
must convert to for the comparison, except that the type is a supported type
in C#. So, logically you would do something like:

object oMin = Convert.ChangeType(minValue, Type.GetType(convertToType));
// etc. giving you 3 object variables which are actually of type int

The problem is the comparison. You can''t do the following because object
does not support the operators:

bool isInRange = (oValue >= oMin) && (oValue <= oMax);

So, other than creating a giant switch() statement with a case for each
possible type you care to support, how would you go about performing the
above comparison?

推荐答案

JV< oy ****** **@spammenot.com>写道:


< snip>
JV <oy********@spammenot.com> wrote:

<snip>
问题在于比较。您不能执行以下操作,因为对象
不支持运算符:

bool isInRange =(oValue> = oMin)&& (oValue< = oMax);

因此,除了创建一个巨大的switch()语句,其中包含您希望支持的每种可能类型的案例,您将如何执行
以上比较?
The problem is the comparison. You can''t do the following because object
does not support the operators:

bool isInRange = (oValue >= oMin) && (oValue <= oMax);

So, other than creating a giant switch() statement with a case for each
possible type you care to support, how would you go about performing the
above comparison?




我假设所涉及的类型实现了IComparable,并且在进行转换后使用

。事实上,没有提前的比较

所涉及类型的知识正是IC Comparable

界面的用途:)


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet 博客: http://www.msmvps.com/jon.skeet

如果回复小组,请不要给我发邮件



I''d assume that the types involved implemented IComparable, and use
that after doing the conversion. Indeed, comparisons without advance
knowledge of the types involved is precisely what the IComparable
interface is for :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


如果你确定你转换的类型是IComparable你可以做的


IComparable cVal =(IComparable)oVal;

if( cVal.CompareTo(oMin)< 0)做点什么......


如果你不确定,你可以用try / catch保护构造。


/ LM

" JV" < OY ******** @ spammenot.com>在留言中写道

新闻:%2 **************** @ TK2MSFTNGP02.phx.gbl ...
if you are sure that the type where you convert is IComparable you can do

IComparable cVal = (IComparable) oVal;
if (cVal.CompareTo(oMin) < 0) do something etc...

if you are not sure, you protect the construct in a try/catch.

/LM

"JV" <oy********@spammenot.com> wrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
我有我想在3个字符串值上执行范围比较:

string minValue =" 1050" ;;
string maxValue =" 5300" ;;
string theValue =" 3239"

我有另一个字符串告诉我在比较之前我需要将上述
字符串转换成类型:

string convertToType =" int" ;

代码不能假设上述值或它必须转换为比较的类型,除了类型是C#中支持的类型。所以,从逻辑上讲你会做类似的事情:

对象oMin = Convert.ChangeType(minValue,Type.GetType(convertToType));
//等给你3个实际的对象变量类型int

问题是比较。您不能执行以下操作,因为对象
不支持运算符:

bool isInRange =(oValue> = oMin)&& (oValue< = oMax);

因此,除了创建一个巨大的switch()语句,其中包含您希望支持的每种可能类型的案例,您将如何执行
以上比较?
I have 3 string values that I want to perform a range comparison on:

string minValue = "1050";
string maxValue = "5300";
string theValue = "3239";

I have another string that tells me the type I need to convert the above
strings to before comparing:

string convertToType = "int";

The code cannot assume anything about the above values or the type that it
must convert to for the comparison, except that the type is a supported
type in C#. So, logically you would do something like:

object oMin = Convert.ChangeType(minValue, Type.GetType(convertToType));
// etc. giving you 3 object variables which are actually of type int

The problem is the comparison. You can''t do the following because object
does not support the operators:

bool isInRange = (oValue >= oMin) && (oValue <= oMax);

So, other than creating a giant switch() statement with a case for each
possible type you care to support, how would you go about performing the
above comparison?



呃,首先不使用int是不是很蠢?


-

<%= Clinton Gallagher

NET csgallagher AT metromilwaukee.com

URL http://clintongallagher.metromilwaukee.com/


JV < OY ******** @ spammenot.com>在留言中写道

新闻:%2 **************** @ TK2MSFTNGP02.phx.gbl ...
Uh, isn''t it kind of dumb not to use an int in the first place?

--
<%= Clinton Gallagher
NET csgallagher AT metromilwaukee.com
URL http://clintongallagher.metromilwaukee.com/


"JV" <oy********@spammenot.com> wrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
我有我想在3个字符串值上执行范围比较:

string minValue =" 1050" ;;
string maxValue =" 5300" ;;
string theValue =" 3239"

我有另一个字符串告诉我在比较之前我需要将上述
字符串转换成类型:

string convertToType =" int" ;

代码不能假设上述值或它必须转换为比较的类型,除了类型是C#中支持的类型。所以,从逻辑上讲你会做类似的事情:

对象oMin = Convert.ChangeType(minValue,Type.GetType(convertToType));
//等给你3个实际的对象变量类型int

问题是比较。您不能执行以下操作,因为对象
不支持运算符:

bool isInRange =(oValue> = oMin)&& (oValue< = oMax);

因此,除了创建一个巨大的switch()语句,其中包含您希望支持的每种可能类型的案例,您将如何执行
以上比较?
I have 3 string values that I want to perform a range comparison on:

string minValue = "1050";
string maxValue = "5300";
string theValue = "3239";

I have another string that tells me the type I need to convert the above
strings to before comparing:

string convertToType = "int";

The code cannot assume anything about the above values or the type that it
must convert to for the comparison, except that the type is a supported
type in C#. So, logically you would do something like:

object oMin = Convert.ChangeType(minValue, Type.GetType(convertToType));
// etc. giving you 3 object variables which are actually of type int

The problem is the comparison. You can''t do the following because object
does not support the operators:

bool isInRange = (oValue >= oMin) && (oValue <= oMax);

So, other than creating a giant switch() statement with a case for each
possible type you care to support, how would you go about performing the
above comparison?



这篇关于有趣的C#难题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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