如何在C#中动态解析和比较字符串值?
抱歉,我不得不重新编辑这个问题。
我需要动态地将两个字符串值解析为适当的类型并比较它们,然后返回 bool 结果。
示例 1:
string lhs = “10”;
string rhs = “10”;
Compare.DoesEqual(lhs, rhs, typeof(int)); //true
Compare.DoesEqual(lhs, rhs, typeof(string)); //true
示例 2:
string lhs = “2.0”;
string rhs = “3.1”;
Compare.IsGreaterThan(lhs, rhs, typeof(int)); //false
Compare.IsGreaterThan(lhs, rhs, typeof(double)); //false
Compare.IsGreaterThan(lhs, rhs, typeof(string)); //invalid, always false
目前我正在这样做(我认为这样做很愚蠢):
public partial class Comparer
{
public static bool DoesEqual(string lhs, string rhs, Type type)
{
if (type.Equals(typeof(int)))
{
try
{
return int.Parse(lhs) > int.Parse(rhs);
}
catch
{
return false;
}
}
if (type.Equals(typeof(double)))
{
try
{
return double.Parse(lhs) > double.Parse(rhs);
}
catch
{
return false;
}
}
return false;
}
}
而这个:
public partial class Comparer
{
public static bool IsGreaterThan(string lhs, string rhs, Type type)
{
if (type.Equals(typeof(int)))
{
try
{
return int.Parse(lhs) == int.Parse(rhs);
}
catch
{
return false;
}
}
if (type.Equals(typeof(double)))
{
try
{
return double.Parse(lhs) == double.Parse(rhs);
}
catch
{
return false;
}
}
if (type.Equals(typeof(string)))
{
return lhs.Equals(rhs);
}
return false;
}
}
我正在寻找更好的更好(更通用的方式)实现(也许使用表达式树?)。 我很感激任何建议。 谢谢你!
Sorry that I had to re-edit this question.
I need to dynamically parse two string values to the appropriate type and compare them, then return a bool result.
Example 1:
string lhs = “10”;
string rhs = “10”;
Compare.DoesEqual(lhs, rhs, typeof(int)); //true
Compare.DoesEqual(lhs, rhs, typeof(string)); //true
Example 2:
string lhs = “2.0”;
string rhs = “3.1”;
Compare.IsGreaterThan(lhs, rhs, typeof(int)); //false
Compare.IsGreaterThan(lhs, rhs, typeof(double)); //false
Compare.IsGreaterThan(lhs, rhs, typeof(string)); //invalid, always false
Currently I am doing like this (I think it's stupid to do it such way):
public partial class Comparer
{
public static bool DoesEqual(string lhs, string rhs, Type type)
{
if (type.Equals(typeof(int)))
{
try
{
return int.Parse(lhs) > int.Parse(rhs);
}
catch
{
return false;
}
}
if (type.Equals(typeof(double)))
{
try
{
return double.Parse(lhs) > double.Parse(rhs);
}
catch
{
return false;
}
}
return false;
}
}
And this:
public partial class Comparer
{
public static bool IsGreaterThan(string lhs, string rhs, Type type)
{
if (type.Equals(typeof(int)))
{
try
{
return int.Parse(lhs) == int.Parse(rhs);
}
catch
{
return false;
}
}
if (type.Equals(typeof(double)))
{
try
{
return double.Parse(lhs) == double.Parse(rhs);
}
catch
{
return false;
}
}
if (type.Equals(typeof(string)))
{
return lhs.Equals(rhs);
}
return false;
}
}
I am looking for better a better (more generic way) implementation (perhaps using Expression Tree?). I appreciate any suggestions. Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最通用的方法是使用 TypeConverter。 假设我们有:
来自某个地方。 您可以这样做:
不幸的是,在处理转换器时没有办法避免
catch
。 现在您已经从字符串解析了两个对象,您可以进行通用比较。 相等很简单 - 只需使用Object.Equals
(最好是静态的 - 它处理空值):对于排序比较,您可以检查类型是否支持
IComparable
:但请注意,某些不提供
operator<
的类型仍然是有序的,因此实现IComparable
- 例如,String
就是这样做的。The most generic way is to use
TypeConverter
. Let's say we have:from somewhere. You can do:
Unfortunately, there's no way to avoid a
catch
there when dealing with converters. Now that you have two objects parsed from strings, you can do generic comparisons. Equality is simple - just useObject.Equals
(preferably static one - it handles nulls):For ordering comparisons, you can check if the type supports
IComparable
:Note however that some types that don't provide
operator<
are still ordered, and thus implementIComparable
- for example,String
does that.哦,我很想承认这个问题。 :)
编辑:测试:
胆量:
Oh I am so about to own this problem. :)
Edit: The tests:
The guts:
这是作业吗?
我的意思是,答案是只编写类/方法的实现。
您遇到的具体问题是什么? 到底是什么你不明白?
如果你把问题分解成几个部分,事情就会变得很容易。
Is this homework?
I mean, the answer is to just write the implementation for the class/method.
What is the specific problem you are having? What is it you don't understand, exactly?
If you break the problem down into parts it becomes easy.