Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dispatch C# generic method call into specialized method calls

I have the following C# class:

public class MyType<T>
{
   public void TryParse(string p_value)
   {
      T value ;

      Parser.TryParse(p_value, out value);

      // Do something with value
   }
}

The point is to call the right Parser.TryParse method, depending on the generic type T.

This uses the following static class:

static public class Parser
{
   static public void TryParse(string p_intput, out object p_output)
   {
      // Do something and return the right value
   }

   static public void TryParse(string p_intput, out double p_output)
   {
      // Do something and return the right value
   }

   static public void TryParse(string p_intput, out int p_output)
   {
      // Do something and return the right value
   }
}

I expected this to work: In the worst case, the "object" TryParse would be called. Instead, I have two compilation errors:

  • CS1502: The best overloaded method match for 'Parser.TryParse(string, out object)' has some invalid arguments
  • CS1503: Argument 2: cannot convert from 'out T' to 'out object'

Question 1: I don't understand why this doesn't work: I can be naive, but aren't all C# objects supposed to derive from "object" ? Why T cannot be converted to object?

Question 2: How can I dispatch a method with generic type T into the right non-generic methods (i.e. MyType<T>.TryParse calling the right Parser.TryParse according to the right type of T) ?

Note

The question was edited to reflect the original question intent (as written in the title: How to dispatch C# generic method call into specialized method calls)

like image 1000
paercebal Avatar asked Mar 07 '11 15:03

paercebal


People also ask

What is Dispatch in C?

This page shows how a dispatch table can be implemented in C or C++. A dispatch table is a way of associating a function with a string or numeric value in a general way. The concepts illustrated include data structure initialization, typedefs for functions, and calling functions through pointers.

What is a processor dispatch?

CPU Dispatching is a technique where your binary detects which features your CPU has, and based on that decide which version of code to execute. For CPU dispatching to work you will need to provide several flavors of your critical function.


2 Answers

Actually, ref and out parameters do not allow type variation. So, to pass a variable to a method expecting an out object parameter, that variable must be declared as object.

From the specification (§10.6.1.2 and §10.6.1.3)

When a formal parameter is a reference parameter, the corresponding argument in a method invocation must consist of the keyword ref followed by a variable-reference (§5.3.3) of the same type as the formal parameter.

When a formal parameter is an output parameter, the corresponding argument in a method invocation must consist of the keyword out followed by a variable-reference (§5.3.3) of the same type as the formal parameter.

See: Why do ref and out parameters not allow type variation? for some insight into why.

Bonus question: How can I dispatch a method with generic type T into the right non-generic methods (i.e. MyType<T>.TryParse calling the right Parser.TryParse according to the right type of T)?

I'm going to turn it back around on you. Why are you doing this? If you are invoking MyType<T>.TryParse as, say, MyType<int>.TryParse, why not call Int32.TryParse directly? What is this extra layer buying you?

like image 154
jason Avatar answered Oct 13 '22 00:10

jason


I know this is somewhat low-tech, but I have had the same problem, where I solved it by making a Dictionary<Type, Parser> containing the individual parsers. I will be interested in what answers this questions bring.

Regards, Morten

like image 37
Morten Avatar answered Oct 13 '22 00:10

Morten