2

I am trying to create a generic function like this, which I plan to instantiate with basic types, mostly DateTime, int and string.

static T MyParameter<T>(string value, T defaultValue)
{
    return value.StartsWith("$$") ? defaultValue : T.Parse(value);
}

This won't compile as there is no guarantee that T will have "Parse".

Is there a "direct" way to implement it? (I mean, avoiding reflection or delegates to do the dirty work).

I can't find any constraint to T that would do the trick.

4
  • I'm afraid there isn't. Oct 7, 2013 at 20:40
  • 1
    My general approach to this is to constrain T to IConverible, and use Convert.ChangeType and cast to T to handle this. However, if your value starts with $$, it will almost always fail to parse unless you strip off the dollar signs.
    – vcsjones
    Oct 7, 2013 at 20:40
  • @vcsjones I think that's why he returns defaultValue when value starts with $$. Oct 7, 2013 at 20:42
  • @MarcinJuraszek Oh, derp. I switched the condition in my head. Yes that makes good sense now.
    – vcsjones
    Oct 8, 2013 at 15:04

3 Answers 3

6

You could pass in the parse method for the generic case:

static T MyParameter<T>(string value, T defaultValue, Func<string, T> parse)
{
    return value.StartsWith("$$") ? defaultValue : parse(value);
}

But for convenience, you probably want to provide overloads for the most common types you plan to use:

static int MyParameter(string value, int defaultValue)
{
    return MyParameter(value, defaultValue, int.Parse);
}
static DateTime MyParameter(string value, DateTime defaultValue)
{
    return MyParameter(value, defaultValue, DateTime.Parse);
}
static string MyParameter(string value, string defaultValue)
{
    return MyParameter(value, defaultValue, x => x);
}

If you really don't like this, you could use reflection, though I wouldn't recommend it:

static T MyParameter<T>(string value, T defaultValue)
{
    if (value.StartsWith("$$"))
    {
        return defaultValue 
    }

    var method = typeof(T).GetMethod("Parse", new[] { typeof(string) });
    return (T)method.Invoke(null, new[] { value });
}
1
  • Note: Reflection can actually be a fair bit quicker if you cache method in a Dictionary per value of T. (more time for validation getting the right method too) Of course basic types all implement IConvertible.
    – user645280
    Oct 8, 2013 at 13:35
2

What about using a Constraint on the generic?

http://msdn.microsoft.com/en-us/library/d5x73970.aspx

So your method becomes:

static T MyParameter<T>(string value, T defaultValue) where T : IParseable
{
    return (T)(value.StartsWith("$$") ? defaultValue : defaultValue.Parse(value));
}

Of course this means that all types used must implement IParseable.

4
  • this is the correct approach. basically, you need to use covariance of the type to make sure that it conforms to what you are trying to call.
    – Alex
    Oct 7, 2013 at 20:56
  • This won't actually compile. 1) You're trying to call Parse statically on T, 2) unless IParseable is generic (e.g. where T : IParseable<T>) there's no way the return value of Parse can be returned without a cast, and 3) even if you fixed those issues, you still wouldn't be able to call this with built-in data types (int, DateTime, string) provided by .NET since they don't implement the custom IParseable interface.
    – p.s.w.g
    Oct 7, 2013 at 21:09
  • Yep, you are right p.s.w.g. I fixed #1 and #2. #3, you are exactly right. This is the problem with this code. You basically have to write wrappers for all the basic data types which is annoying.
    – Zippit
    Oct 7, 2013 at 21:30
  • You still haven't fixed #2. If IParseable isn't a generic interface, it's return type will be something like object, so you'll have to cast it the return value to return it. If it is generic, you'd have to specify that in the constraint. Also, now that you've corrected #1, you have to deal with the possibility that defaultValue may be null. Sorry if it seems like I'm nit-picking. This is a valid approach, but I think without an working example, it's like to just cause more confusion.
    – p.s.w.g
    Oct 7, 2013 at 21:53
1

This should do it:

static T MyParameter<T>(string value, T defaultValue)
{
    return (T) (value.StartsWith("$$") ? defaultValue : Convert.ChangeType(value, typeof (T)));
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.