的changetype,转换 - 从一种类型转换到另类型、changetype

2023-09-07 09:33:51 作者:落魄

我看着,似乎是可以在.NET框架(我认为)的各个选项。特别是,我已经看了 1.类型转换器 2. Convert.Toxxx 3. Convert.ChangeType

I've looked at the various options that seem to be available on the .NET framework (I think). In particular, I've looked at 1. TypeConverter 2. Convert.Toxxx 3. Convert.ChangeType

每个人由于某种原因或其他,我不工作。我很惊讶,似乎没有要在.NET框架这样的事情的解决方案。我假设我不是唯一一个谁需要这个;)

Each of them for one reason or another don't work for me. I'm quite surprised that there doesn't seem to be a solution in the .NET framework for this sort of thing. I'm assuming I'm not the only one who needs this ;)

下面是我想要做的。从本质上讲,我已经得到了在应用程序中使用了一堆HTML表单。这些形式让用户输入数据(明显),我需要提取这些数据转化为各种数据类型。

Here is what I'm trying to do. Essentially, I've got a bunch of html forms that are used in an application. These forms allow users to enter data (obviously) and I need to extract this data into various data types.

这些数据类型是简单类型,原始类型,值类型,引用类型和自定义引用类型。换一种说法: 1.的Int32,Int64的,双,小数,日期时间等。 2.的Int32 [],双击[],字符串[]和其他可能的基本类型数组 有以上类型的属性3.各种自定义的DTO对象。

These data types are simple types, primitive types, value types, reference types and custom reference types. In other words: 1. Int32, Int64, double, decimal, DateTime etc. 2. Int32[], double[], string[] and possibly other arrays of primitive types 3. various custom DTO objects that have the above types as properties.

输入是一个字符串,通常因为我是在HTTP中土地的形式。为了给你的,为什么现有的解决方案,我不工作的想法,看看需要将下面的字符串输入

The "input" is in the form of a string typically since I'm in Http land. To give you an idea of why the existing solutions don't work for me, take a look at the following "string" inputs that need converting

在1,234 - >的Int32 在$ 1,234 - >的Int32 在$ 1,234 - >的Int32 - >的Int32 在1,2,3,4,5 - >的Int32 [] 在0或假或false或关 - >布尔假

我知道如何手工转换每种情况下,但我期待的任何东西是可以处理的上述输入的类型,我已经错过了框架或一个体面的解决方案的一部分。

I know how to convert each of these cases by hand, but I am looking for either something that's part of the framework that I've missed or a decent solution that handle the types of inputs listed above.

推荐答案

成龙,

这是在Web开发领域一个非常普遍的要求,因为你已经注意到,没有内置的方式来实现这一点,这有什么最简单的情况达不到。

This is a very common requirement in the Web Development world and as you've noted there is no built-in way to achieve this and what is there falls short in the simplest of cases.

@克里斯这些规则不是任意的任何镜头的想象。他们其实都是很常见的,当涉及到处理用户的输入,尤其是在网上。事实上布尔转换也pretty的共同鉴于在ASP.NET开/关(出于某种原因),收益

@Chris these rules are not arbitrary by any shot of the imagination. They are in fact very common when it comes to dealing with user input, especially over the web. In fact the Boolean conversions are also pretty common given that check boxes in ASP.NET return on/off (for some reason).

您有几个选项。一个是一个简单的方法,另一种可扩展的解决方案。这一切都始于您想要使用此功能可以在程序中的方式。既然你不就行了是你在做什么目前还是你想如何做到这一点流下淡了很多,我已经采取了做了一些假设的自由。

You have a a couple of options. One is a simplistic approach and another an extensible solution. It all starts from the way you'd like to use this functionality from within your application. Since you've not shed a lot of light on what it is you are doing currently or how you'd like to do this, I've taken the liberty of making a few assumptions.

的主要假设是这些值经由的Request.Form或Request.QueryStrings(这两者都是NameValueCollections,可容纳多个值对于给定的名称)。

The primary assumption being that the values come to you via the Request.Form or Request.QueryStrings (both of which are NameValueCollections that can hold multiple values for a given name).

让我们假设你想给定的参数的名称,你想改变它会返回值的类型要求的类型的方法称为一changeType。因此,该方法的签名可能是这样的:

Let's assume you'd like a method called ChangeType that given a name of the parameters and the Type you'd like to change it to will return the value as the required type. So the method signature might look like this:

公共牛逼的changetype< T>(字符串参数名称,T设置defaultValue =默认(T))

所以你可以使用它像这样:

And so you can use it like so:

int someId = ChangeType<int>("id", -1);

所以变量some​​Id的价值要么是从的Request.Form或Request.QueryStrings在一个int的形式(如果存在)中提取的值,或-1,如果它没有。

So the value of the variable someId will either be the value extracted from Request.Form or Request.QueryStrings in the form of an int (if it exists) or -1 if it does not.

一个更完整的实现如下:

A more complete implementation is as follows:

    static T ChangeType<T>(string value) where T: struct
{
  Type typeOft = typeof(T);
  if (String.IsNullOrEmpty(value.Trim()))
    return default(T);
  if (typeOft == typeof(Int32))
    return (T)ConvertToInt32(value);
  else if (typeOft == typeof(Int64))
    return (T)ConvertToInt64(value);
  else if (typeOft == typeof(Double))
    return (T)ConvertToDouble(value);
  else if (typeOft == typeof(Decimal))
    return (T)ConvertToDecimal(value);
  return default(T);
}

static object ConvertToInt32(string value)
{
  return Int32.Parse(value,
    NumberStyles.Currency ^ NumberStyles.AllowDecimalPoint);
}

static object ConvertToInt64(string value)
{
  return Int64.Parse(value,
    NumberStyles.Currency ^ NumberStyles.AllowDecimalPoint);
}

static object ConvertToDouble(string value)
{
  return Double.Parse(value, NumberStyles.Currency);
}

static object ConvertToDecimal(string value)
{
  return Decimal.Parse(value, NumberStyles.Currency);
}

如果你需要能够处理更多类型,简单地实现所需的方法(如ConvertToInt32为例),并添加另一个else条件的的changetype&LT; T&GT; 方法就大功告成了。

If you need to be able to handle more types, simply implement the required method (such as ConvertToInt32 for example) and add another else condition in the ChangeType<T> method and you're done.

现在,如果你正在寻找一个扩展的解决方案,这样你可以无需修改主code,并能OT处理自己的自定义类型添加额外的功能,那么请看看这个博客帖子矿。 [一changeType - 改变一个变量在C#中的类型] [1] 的http://www.matlus.com/2010/11/changetypet-changing-the-type-of-a-variable-in-c/

Now if you're looking for an extensible solution such that you can add additional capability without having to modify the primary code and be able ot handle your own custom types, then please take a look at this blog post of mine. [ChangeType – Changing the type of a variable in C#][1] http://www.matlus.com/2010/11/changetypet-changing-the-type-of-a-variable-in-c/

希望这有助于。

 
精彩推荐
图片推荐