Hi,
I am trying to write a generic function and it isn't behaving as expected.
I want to avoid having to write custom convert functions for my enums so
I want to convert to an int and cast to the enum, this wouldn't compile so
I tried a few variations.
The cast fails inside the generic function even though its valid.
The code below is the simplest example of the problem I could think of, even
casting int to int fails.
Is this a limitation or am I doing something wrong?
Thanks, Vin
// code
private void TestConvert()
{
string source = "1";
int destination = 0;
Parse(source, destination, new Converter<string, int>(Convert.ToInt32));
}
static protected void Parse<Destination_type, Intermediate_type>(string source,
Destination_type destination, Converter<string, Intermediate_typeconverter)
{
Intermediate_type test = converter.Invoke(source);
// Error on next line: Cannot convert type 'Intermediate_type' to 'Destination_type'
destination = (Destination_type)test;
}