Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从十进制转换为T?_C# - Fatal编程技术网

C# 如何从十进制转换为T?

C# 如何从十进制转换为T?,c#,C#,我在NumbericUpDown控件上构建了一个包装器。 包装器是通用的,可以支持int?双倍 我想写一个方法,它将执行以下操作 public partial class NullableNumericUpDown<T> : UserControl where T : struct { private NumbericUpDown numericUpDown; private T? Getvalue() { T? value = numericUpDown.V

我在NumbericUpDown控件上构建了一个包装器。 包装器是通用的,可以支持int?双倍

我想写一个方法,它将执行以下操作

public partial class NullableNumericUpDown<T> : UserControl where T : struct
{
  private NumbericUpDown numericUpDown;


  private T? Getvalue()
  {
    T? value = numericUpDown.Value as T?; // <-- this is null :) thus my question
    return value;
  }}
public分部类NullableNumericUpDown:UserControl其中T:struct
{
私人数字Updown数字Updown;
私有T?Getvalue()
{

T?value=numericUpDown.value为T?;//不清楚如何使用它。 如果需要双精度创建GetDouble()方法,请为integers-GetInteger()创建

编辑:

好的,现在我想我理解了您的用例

试试这个:

using System;
using System.ComponentModel;

static Nullable<T> ConvertFromString<T>(string value) where T:struct
{
    TypeConverter converter = TypeDescriptor.GetConverter(typeof(T));
    if (converter != null && !string.IsNullOrEmpty(value))
    {
        try
        {
            return (T)converter.ConvertFrom(value);
        }
        catch (Exception e) // Unfortunately Converter throws general Exception
        {
            return null;
        }
    }

    return null;
}

...

double? @double = ConvertFromString<double>("1.23");
Console.WriteLine(@double); // prints 1.23

int? @int = ConvertFromString<int>("100");
Console.WriteLine(@int); // prints 100

long? @long = ConvertFromString<int>("1.1");
Console.WriteLine(@long.HasValue); // prints False
使用系统;
使用系统组件模型;
静态可空ConvertFromString(字符串值),其中T:struct
{
TypeConverter converter=TypeDescriptor.GetConverter(typeof(T));
if(converter!=null&!string.IsNullOrEmpty(value))
{
尝试
{
返回(T)转换器。转换自(值);
}
catch(异常e)//不幸的是,转换器引发了一般异常
{
返回null;
}
}
返回null;
}
...
double?@double=ConvertFromString(“1.23”);
Console.WriteLine(@double);//打印1.23
int?@int=ConvertFromString(“100”);
Console.WriteLine(@int);//打印100
long?@long=ConvertFromString(“1.1”);
Console.WriteLine(@long.HasValue);//打印错误

因为此方法将始终返回

numericUpDown.Value
您没有理由将该值转换为十进制以外的任何值。是否正在尝试解决一个您没有遇到的问题?

来自Decimal的公共类,其中t:struct,IConvertible
public class FromDecimal<T> where T : struct, IConvertible
{
    public T GetFromDecimal(decimal Source)
    {
        T myValue = default(T);
        myValue = (T) Convert.ChangeType(Source, myValue.GetTypeCode());
        return myValue;
    }
}

public class FromDecimalTestClass
{
    public void TestMethod()
    {
        decimal a = 1.1m;
        var Inter = new FromDecimal<int>();
        int x = Inter.GetFromDecimal(a);
        int? y = Inter.GetFromDecimal(a);
        Console.WriteLine("{0} {1}", x, y);

        var Doubler = new FromDecimal<double>();
        double dx = Doubler.GetFromDecimal(a);
        double? dy = Doubler.GetFromDecimal(a);
        Console.WriteLine("{0} {1}", dx, dy);
    }
}
{ 公共T GetFromDecimal(十进制源) { T myValue=默认值(T); myValue=(T)Convert.ChangeType(源代码myValue.GetTypeCode()); 返回myValue; } } 公共类FromDecimalTestClass { 公共void TestMethod() { 十进制a=1.1m; var Inter=new FromDecimal(); int x=内部GetFromDecimal(a); int?y=Inter.GetFromDecimal(a); Console.WriteLine(“{0}{1}”,x,y); var Doubler=新的FromDecimal(); double dx=Doubler.GetFromDecimal(a); double?dy=Doubler.GetFromDecimal(a); WriteLine(“{0}{1}”,dx,dy); } }

private T?Getvalue()
{
T?值=空;
if(this.HasValue)
值=new FromDecimal().GetFromDecimal(NumericUpDown);
返回值;
}

如果您只需要double和int,为什么您需要generic?Aku,这很酷,但不幸的是DoubleConverter不支持decimal:|我不明白您在说什么。为什么要使用DoubleConverter?此代码可以使用decimal?@decimal=ConvertFromString(“999999999999”);这也是:ConvertFromString(“1.1”)上周我问了一个问题。我认为答案对你的情况可能有效。我看不出它有什么帮助。我想以某种方式使用转换器逻辑。你的问题还不够清楚。GetValue方法是包装器的一部分吗?或者numericUpDown是包装器的一个实例吗?可能需要更多的代码来显示你试图实现的功能我会帮忙的。你会返回类似于MathProvider的东西。我想问题是如何创建这个“类似的东西”
private T? Getvalue()
{
  T? value = null;
  if (this.HasValue)
    value = new FromDecimal<T>().GetFromDecimal(NumericUpDown);
  return value;
}