Generic Parse Method without Boxing

前端 未结 8 1287
余生分开走
余生分开走 2021-02-05 17:12

I am trying to write a generic Parse method that converts and returns a strongly typed value from a NamedValueCollection. I tried two methods but both of these methods are goin

相关标签:
8条回答
  • 2021-02-05 17:27

    Am I too late?

    static Dictionary<Type, Delegate> table = 
        new Dictionary<Type, Delegate>{
            { typeof(int), (Func<string,int>)Int32.Parse },
            { typeof(double), (Func<string,double>)Double.Parse },
            // ... as many as you want
        };
    
    static T Parse<T>(string str)
    {
        if (!table.TryGet(typeof(T), out Delegate func))
            throw new ArgumentException();
        var typedFunc = (Func<string, T>)func;
        return typedFunc(str);
    }
    

    When in trouble with types, try delegates and dicts!

    0 讨论(0)
  • 2021-02-05 17:30

    For better readability, you could use a generic dictionary with an anonymous function as follows:

    var parserFuncs = new Dictionary<Type, Func<string, object>>() {
        { typeof(int), p => (int) int.Parse(p) },
        { typeof(bool), p => (bool) bool.Parse(p) },
        { typeof(long), p => (long) long.Parse(p) },
        { typeof(short), p => (short) short.Parse(p) },
        { typeof(DateTime), p => (DateTime) DateTime.Parse(p) }
        /* ...same for all the other primitive types */
    };
    
    return (T) parserFuncs[typeof(T)](value);
    
    0 讨论(0)
  • 2021-02-05 17:31

    I'll add a little undocumented way:

    public static T Convert<T>()
    {
        if (typeof(T) == typeof(int))
        {
            int a = 5;
            T value = __refvalue(__makeref(a), T);
            return value;
        }
        else if (typeof(T) == typeof(long))
        {
            long a = 6;
            T value = __refvalue(__makeref(a), T);
            return value;
        }
    
        throw new NotImplementedException();
    }
    

    There is little documentation about them, but they work as of C# 4.0. Read for example here Hidden Features of C#? Remember that undocumented means unsupported, blah blah blah could not work in the future blah blah blah if you use them the devil will come for you blah blah blah :-)

    0 讨论(0)
  • 2021-02-05 17:34
    public static T Parse<T>(this NameValueCollection col, string key)
    {
      return (T)Convert.ChangeType(col[key], typeof(T));
    }
    

    I'm not entirely sure of ChangeType boxes or not (I guess reading the docs would tell me, but I'm pressed for time right now), but at least it gets rid of all that type-checking. The boxing overhead is not very high, though, so I wouldn't worry too much about it. If you're worried about run-time type consistency, I'd write the function as:

    public static T Parse<T>(this NameValueCollection col, string key)
    {
      T value;
    
      try
      {
        value = (T)Convert.ChangeType(col[key], typeof(T));
      }
      catch
      {
        value = default(T);
      }
    
      return value;
    }
    

    This way the function won't bomb if the value cannot be converted for whatever reason. That means, of course, that you'll have to check the returned value (which you'd have to do anyway since the user can edit the querystring).

    0 讨论(0)
  • 2021-02-05 17:38

    I think you are over estimating the impact of the boxing/unboxing. The parse method will have a much bigger overhead (string parsing), dwarfing the boxing overhead. Also all the if statements will have a bigger impact. Reflection has the biggest impact of all.

    I'd would not like to see this kind of code in production, as there is a cleaner way of doing it. The major problem I have with it is the large number of if statements you will need to cover all cases and the fact that someone could pass any old type to it.

    What I would do is write a parse function for each type I want to parse (ie ParseInt()). It's clearer and it is well defined what the function will try to do. Also with short static methods, the compiler is more likely to inline them, saving a function call.

    I think this is a bad application of generics, any particular reason for doing it this way?

    0 讨论(0)
  • 2021-02-05 17:41

    Here's a suggestion for implementation, following Robert Wagner's logic, but using a generic approach to reduce duplication:

    public static int ParseInt32(this NameValueCollection col, string key)
    {
        return Parse(col, key, int.Parse);
    }
    public static double ParseDouble(this NameValueCollection col, string key)
    {
        return Parse(col, key, double.Parse);
    }
    private static T Parse<T>(NameValueCollection col, string key, Func<string, T> parse)
    {
        string value = col[key];
    
        if (string.IsNullOrEmpty(value))
            return default(T);
    
        return parse(value);
    }
    

    Truth be told, returning zero for a null or empty string scares me; this could cause problems if some values are legitimately zero. Instead, I would have the methods return nullables (int?, double?, etc.), which is a slightly more compact approach than the out-parameter pattern used for the framework TryParse methods. You could do this:

    public static int? ParseInt32(this NameValueCollection col, string key)
    {
        return Parse(col, key, int.Parse);
    }
    public static double? ParseDouble(this NameValueCollection col, string key)
    {
        return Parse(col, key, double.Parse);
    }
    private static T? Parse<T>(NameValueCollection col, string key, Func<string, T> parse)
        where T : struct    
    {
        string value = col[key];
    
        if (string.IsNullOrEmpty(value))
            return default(T?);
    
        return parse(value);
    }
    

    But that would still throw an exception for non-null-or-empty strings that aren't numeric. It's better to use TryParse. The built-in Func delegates don't support ref or out parameters, so you'd have to declare your own delegate type, but that is fairly trivial.

    0 讨论(0)
提交回复
热议问题