如何在没有开关的情况下解析各种类型? [英] How to parse various types without a switch?

查看:75
本文介绍了如何在没有开关的情况下解析各种类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我需要阅读一个大的CSV文件,其中不同的字段应转换为

不同的类型,

如int,double,datetime,SqlMoney等。


我有一个数组,它描述了字段及其类型。我想

以某种方式存储对此数组中解析操作的引用

(如Int32.Parse,Double.Parse,SqlMoney.Parse等),

所以我可以在不写长开关的情况下调用合适的一个。


出于性能原因,不能使用反射。


我试图创建一个委托,但是因为Int32.Parse,Double.Parse等

都有不同的返回类型,创建一个共同的委托类型

出现不可能。


现在我最终为每种类型的Parse方法编写包装器,



静态对象ParseDouble(string str){return double.Parse(str);}

然后将这些方法的委托插入到数组中。


这似乎有效,但它看起来很丑陋。我仍然希望.NET

框架

有办法以官方方式完成,我忽略了这一点。例如,

它有接口IConvertible,可用于实现相反:

转换

一个对象到各种类型,但我不能找一个正式的解析方法。


谢谢

John

解决方案

John,


你已经交叉(没有错)但是我认为你会更好

将这个转换为一种语言新闻组也是如此,因为我们不知道你现在使用的是什么程序语言。


Microsoft的两个最大的开发人员新闻组都在Excel.developer旁边

和ASPNET


microsoft.public.dotnet.csharp



microsoft.public。 dotnet.languages.vb


至少当你发布到你现在发布的两个新闻组时,告诉我们你使用的程序语言是什么
us。 br />

我希望这会有所帮助,


Cor

因为Parse()没有在接口中声明,你必须创建

包装器。

它就像对象Parse(string csvstrword );


(在C中我们有sscanf,它可以读入许多数据类型。)

---

hB




< John>在消息新闻中写道:rf ******************** @ speakeasy.net ...

使用反射不是一个出于性能原因的选择。


反思并不一定要慢。你不能摆脱开销,但是如果你正确编码它可以非常快。

我试图创建一个委托,但是因为Int32。 Parse,Double.Parse等
都有不同的返回类型,创建一个共同的委托类型
似乎是不可能的。




返回一个委托返回后对象和unbox它(如果适用)。

这将比反射更少开销。


或者,如果你可以解决实际类型要解析的值,你可以使用Convert.ChangeType创建一种通用转换器函数:


公共对象TryParse(object / * string * / val) ,System.Type type){

try {

返回Convert.ChangeType(val,type);

}

catch {

返回null;

}

}


并将其称为:


string s =" 3.41";

double d =(double)TryParse(s,System.Double);
-

Klaus H. Probst,MVP
http://www.simulplex.net/


Hi,
I need to read a big CSV file, where different fields should be converted to
different types,
such as int, double, datetime, SqlMoney, etc.

I have an array, which describes the fields and their types. I would like
to somehow store a reference to parsing operations in this array
(such as Int32.Parse, Double.Parse, SqlMoney.Parse, etc),
so I can invoke the appropriate one without writing a long switch.

Using reflection is not an option for performance reasons.

I tried to create a delegate, but since Int32.Parse, Double.Parse, etc.
all have different return types, creating a common delegate type
appears to be impossible.

For now I ended up writing wrappers around Parse methods for each type,
such as
static object ParseDouble(string str) {return double.Parse(str);}
then inserting delegates to these methods into the array.

This seems to work, but it looks pretty ugly. I still hope that .NET
framework
has a way to do it in official way, which I overlooked. For example,
it has interface IConvertible, which can be used to achieve opposite:
convert
an object to various types, but I cannot find an official way to do parsing.

Thank you
John

解决方案

John,

You have crossposted (nothing wrong with) however I think that you be better
of with crossposting this to a language newsgroup too, because we don''t know
what program language you are using now.

The two largest developer newsgroups of Microsoft are beside Excel.developer
and ASPNET

microsoft.public.dotnet.csharp
and
microsoft.public.dotnet.languages.vb

At least when you post to the two newsgroups that you are posting now, tell
us than what program language you use.

I hope this helps,

Cor


since Parse() is not declared in interface, you have to create
wrappers.
It would be like object Parse(string csvstrword);

(In C we have sscanf, it can read into many datatypes.)
---
hB



<John> wrote in message news:rf********************@speakeasy.net...

Using reflection is not an option for performance reasons.
Reflection doesn''t have to be slow. You can''t get rid of the overhead, but
if you code it correctly it can be quite fast.
I tried to create a delegate, but since Int32.Parse, Double.Parse, etc.
all have different return types, creating a common delegate type
appears to be impossible.



Return an object and unbox it (if applicable) after the delegate returns.
This will have less overhead than reflection.

Or, if you can resolve the actual type of the value being parsed you can
create a sort of generic converter function using Convert.ChangeType:

public object TryParse(object /* string */ val, System.Type type) {
try {
return Convert.ChangeType(val, type);
}
catch {
return null;
}
}

And call it like:

string s = "3.41";
double d = (double) TryParse(s, System.Double);
--
Klaus H. Probst, MVP
http://www.simulplex.net/


这篇关于如何在没有开关的情况下解析各种类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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