Variable generic return type in C#

Is there any way to have a method return any one of a number of generic types from a method? For example, I have the following:

public static T ParseAttributeValue<T>(this XElement element, string attribute)
    {
        if(typeof(T) == typeof(Int32))
        {
            return Int32.Parse(element.Attribute(attribute).Value);
        }

        if(typeof(T) == typeof(Double))
        {
            return Double.Parse(element.Attribute(attribute).Value);
        }

        if(typeof(T) == typeof(String))
        {
            return element.Attribute(attribute).Value;
        }

        if(typeof(T) == typeof(ItemLookupType))
        {
            return Enum.Parse(typeof(T), element.Attribute(attribute).Value);
        }
    }

(This is only a very quick mockup, I'm aware that any production code would need to be significantly more thorough in null checks etc...)

But the compiler doesn't like it, complaining that Int32 cannot be implicitly converted to T (it doesn't work with a cast either). I can understand that. At compile time it has no way to know what T is, but I'm checking it beforehand. Is there anyway I can make this work?

Answers


I've done these types of generic methods in the past. The easiest way to get type inference is to provide a generic converter function.

public static T ParseAttributeValue<T>
          (this XElement element, string attribute, Func<string, T> converter)
{
  string value = element.Attribute(attribute).Value;
  if (String.IsNullOrWhiteSpace(value)) {
    return default(T);
  }

  return converter(value);
}

You can use it like the following:

int index = element.ParseAttributeValue("index", Convert.ToInt32);
double price = element.ParseAttributeValue("price", Convert.ToDouble);

You can even provide your own functions and have all the fun in the world (even return anonymous types):

ItemLookupType lookupType = element.ParseAttributeValue("lookupType",
  value => Enum.Parse(typeof(ItemLookupType), value));

var item = element.ParseAttributeValue("items",
  value => {
    List<string> items = new List<string>();
    items.AddRange(value.Split(new [] { ',' }));
    return items;
  });

.Net already has a bunch of great string conversion routines you can use! A TypeConverter can do most of the heavy lifting for you. Then you don't have to worry providing your own parsing implementations for built-in types.

Note that there are locale-aware versions of the APIs on TypeConverter that could be used if you need to handle parsing values expressed in different cultures.

The following code will parse values using the default culture:

using System.ComponentModel;

public static T ParseAttributeValue<T>(this XElement element, string attribute)
{
    var converter = TypeDescriptor.GetConverter(typeof(T));
    if (converter.CanConvertFrom(typeof(string)))
    {
        string value = element.Attribute(attribute).Value;
        return (T)converter.ConvertFromString(value);
    }

    return default(T);
}

This will work for a lot of built-in types, and you can decorate custom types with a TypeConverterAttribute to allow them to participate in the type conversion game too. This means that in the future you will be able to parse new types without having to change the implementation of the ParseAttributeValue.

see: http://msdn.microsoft.com/en-us/library/system.componentmodel.typeconverter.aspx


Why are you using the type parameter as the return type at all? This would work, just requires a cast after calling:

public static Object ParseAttributeValue<T>(this XElement element, string attribute)
{
    if(typeof(T) == typeof(Int32))
    {
        return Int32.Parse(element.Attribute(attribute).Value);
    }

    if(typeof(T) == typeof(Double))
    {
        return Double.Parse(element.Attribute(attribute).Value);
    }

    if(typeof(T) == typeof(String))
    {
        return element.Attribute(attribute).Value;
    }

    if(typeof(T) == typeof(ItemLookupType))
    {
        return Enum.Parse(typeof(T), element.Attribute(attribute).Value);
    }
}

Or better yet:

public static Int32 ParseAsInt32(this XElement element, string attribute)
{
    return Int32.Parse(element.Attribute(attribute).Value);
}

// etc, repeat for each type

This second approach has the additional benefit of having a much higher likelihood of getting inlined, plus it will (for value types like Int32) prevent the need to box/unbox the value. Both of these will cause the method to perform somewhat faster.


Not sure if this is exactly what you want, but you can make the returns work if you cast to object first then to T

    public static T ParseAttributeValue<T>(this XElement element, string attribute)
    {
        if (typeof(T) == typeof(Int32))
        {
            return (T)(object)Int32.Parse(element.Attribute(attribute).Value);
        }

        if (typeof(T) == typeof(Double))
        {
            return (T)(object)Double.Parse(element.Attribute(attribute).Value);
        }

        if (typeof(T) == typeof(String))
        {
            return (T)(object)element.Attribute(attribute).Value;
        }

        return default(T);
    }

However you still have to provide T at compile time, calling the method like:

int value = element.ParseAttributeValue<int>("attribute");

Here's two ways of doing it...

    static T ReadSetting<T>(string value)
    {
        object valueObj = null;
        if (typeof(T) == typeof(Int32))
            valueObj = Int32.Parse(value);
        return (T)valueObj;
    }
    static dynamic ReadSetting2<T>(string value)
    {
        if (typeof(T) == typeof(Int32))
            return Int32.Parse(value);
        throw new UnsupportedException("Type is unsupported");
    }
    static void Main(string[] args)
    {
        int val1 = ReadSetting<Int32>("2");
        int val2 = ReadSetting2<Int32>("3");
    }

With C++ templates, this kind of thing would work, but only if each piece of code were in a different, separate specialization. The thing that makes that work is that unused function templates are not compiled (or more accurately: not fully instantiated), so the fact that a piece of code would be invalid if that copy of the template were instantiated with a different type doesn't come up.

C# is different, and AFAIK there's no specialization for generics. One way to accomplish what you are trying to do, while working within the limitations of C# would be to create one function with a more abstract return type, and use the ParseAttributeValue only to cast it to T.

So you would have:

private static Object AbstractParseValue(System.Type t, XElement element, string attribute)

and

public static T ParseAttributeValue<T>(this XElement element, string attribute)
{
     return (T)AbstractParseValue(typeof(T), element, attribute);
}

I would suggest that rather than testing the type parameter every time the routine is executed, you should create a generic static class something like this:

internal static class ElementParser<T>
{
  public static Func<XElement, string, T> Convert = InitConvert;

  T DefaultConvert(XElement element, string attribute)
  {
    return Default(T); // Or maybe throw exception, or whatever
  }

  T InitConvert(XElement element, string attribute)
  {
    if (ElementParser<int>.Convert == ElementParser<int>.InitConvert)
    {  // First time here for any type at all
      Convert = DefaultConvert; // May overwrite this assignment below
      ElementParser<int>.Convert =
        (XElement element, string attribute) =>
          Int32.Parse(element.Attribute(attribute).Value);
      ElementParser<double>.Convert =
        (XElement element, string attribute) =>
          Int32.Parse(element.Attribute(attribute).Value);
      // etc. for other types
    }
    else // We've done other types, but not this type, and we don't do anything nice for it
    {
      Convert = DefaultConvert;
    }
    return Convert(element, attribute);      
  }
}
public static T ParseAttributeValue(this XElement element, string attribute)
{
  ElementParser<T>.Convert(element, attribute);
}

Using this approach, one will only have to do special handling the first time a particular type is used. After that, the conversion can be performed using only a single generic delegate invocation. Once could easily add any number of types, and even allow converters to be registered for any desired type at runtime.


Need Your Help

Errors Installing mysql2 gem via the Bundler

mysql ruby-on-rails ruby rubygems bundler

I am trying to install the mysql2 gem via the Bundler, but it keeps dying with the following error: