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:
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) ?
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)
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.
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.
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 rightParser.TryParse
according to the right type ofT
)?
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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With