简体   繁体   English

在 C# 中使用 double 类型作为参数

[英]Use the type double as an argument in C#

Consider the following extension to a WPF TextBox :考虑以下对 WPF TextBox扩展:

public static void Parse<T>(this TextBox textBox, Func<string, T> parser)
{
    try
    {
        T value = parser(textBox.Text);
        textBox.Text = value.ToString();
    }
    catch
    {
        textBox.Text = default(T).ToString();
    }
}

That can be used like so:可以这样使用:

private void TextBox_LostFocus(object sender, RoutedEventArgs e)
{
    ((TextBox)sender).Parse(double.Parse);
}

My question is: can the extension method be updated/modified so that I can call ((TextBox)sender).Parse(double) instead of ((TextBox)sender).Parse(double.Parse) ?我的问题是:可以更新/修改扩展方法,以便我可以调用((TextBox)sender).Parse(double)而不是((TextBox)sender).Parse(double.Parse)吗?


EDIT编辑

The extension method is meant to be very easily used to make sure that the user set, for example, a double or int value inside a TextBox .扩展方法旨在非常容易地用于确保用户在TextBox设置例如doubleint值。 The idea is to use the method like so:这个想法是使用这样的方法:

private void TextBox_LostFocus(object sender, RoutedEventArgs e)
{
    ((TextBox)sender).Parse(double); // make sure the input is a double
    //or
    ((TextBox)sender).Parse(int); // make sure the input is a int
}

What goes inside the extension method doesn't really matter, as long as it works, but I want a minimalist code.扩展方法内部的内容并不重要,只要它有效,但我想要一个极简的代码。

Perhaps a better name for the method would be something like ParseTo() , or better yet, Validate() , as said in the comments.也许该方法的更好名称是ParseTo() ,或者更好的是Validate() ,如评论中所述。

Have a look at this看看这个

public static string Parse<T>(string val)
{
    try
    {
        var result = (T)Convert.ChangeType(val, typeof(T));
        return result.ToString();
    }
    catch(Exception ex)
    {
        return default(T).ToString();
    }           
}

public static void Main()
{
    Console.WriteLine(Result<int>("2"));
    Console.WriteLine(Result<double>("22f"));
}

the first result will be 2, since its actually an integer, but the second value is a float, and im trying to parse it as a double, the value will be 0. If it fails it will give you the default value of T.第一个结果将是 2,因为它实际上是一个整数,但第二个值是一个浮点数,我试图将其解析为双精度值,该值将为 0。如果失败,它将为您提供默认值 T。

You can use the generic parameter type directly:您可以直接使用泛型参数类型:

public static void Validate<TNumeric>(this TextBox textBox) where TNumeric : struct
{
  switch (typeof(TNumeric))
  {
    case Type doubleType when doubleType == typeof(double):
      if (!double.TryParse(textBox.Text, out _))
      {
        textBox.Text = default(TNumeric).ToString();
      } break;
    case Type int32Type when int32Type == typeof(int):
      if (!int.TryParse(textBox.Text, out _))
      {
        textBox.Text = default(TNumeric).ToString();
      } break;
    default: 
      throw new ArgumentException($"Validation of type {typeof(TNumeric)} not supported");
  }
}

Example例子

numericTextBox.Validate<double>();

Alternatively use specialized methods:或者使用专门的方法:

public static void ParseDouble(this TextBox textBox)
{
  if (!double.TryParse(textBox.Text, out _))
  {
    textBox.Text = default(double).ToString();
  }
}

public static void ParseInt32(this TextBox textBox)
{
  if (!int.TryParse(textBox.Text, out _))
  {
    textBox.Text = default(int).ToString();
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM