1

I have a method that requires a T to function properly. I have a Type object as a property of my class.

public Type DefaultNumaricType {get; private set;} = typeof(double);

private T Convert<T>(string input)
{
    ...
}

I want to be able to call the Convert<T> method and get an object of the type specified in DefaultNumaricType.

Is something like this possible:

double d = Convert<DefaultNumaricType>("123.00");
trashr0x
  • 6,457
  • 2
  • 29
  • 39
zaza
  • 892
  • 1
  • 18
  • 37
  • Could you make your object generic and have the type as a generic parameter instead? Alternatively what is the body of your Convert method? Methods like `Convert.ChangeType` take a Type object rather than a generic parameter so perhaps you could just make your method work slightly differently. – Chris Aug 21 '18 at 00:08
  • @Chris The body of my convert method is from here:https://stackoverflow.com/questions/2961656/generic-tryparse – zaza Aug 21 '18 at 00:10
  • @chris I need the return object as a numaric type as i'm preforming mathematical operations on it – zaza Aug 21 '18 at 00:11
  • 1
    Converting a string to a numeric type is often done via a `Parse` or `TryParse` method. What isn't working with your current (missing) code? – Rufus L Aug 21 '18 at 00:13
  • Hmmm... I can't think of a great way to do that in that case... Do you really need the type to be a parameter rather than making the class containing it generic taking the type parameter? – Chris Aug 21 '18 at 00:15
  • It could also use `as` to see if a cast is successful with `int`, `double`, etc. – Maximilian Burszley Aug 21 '18 at 00:15
  • @RufusL: Prse or TryParse require you to know the type you are parsing at compile time which isn't known here... – Chris Aug 21 '18 at 00:15
  • @RufusL the issue with that is that i want the user of the code to be able to specify the type of numaric value. So the converted value could be any of the c# numaric types based on the "DefaultNumaricType" parameter – zaza Aug 21 '18 at 00:18
  • In order to use the `TypeDescriptor` class mentioned in the linked question above, I believe you need to implement another class that can do the conversion, and then apply a [TypeConvertereAttribute](https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.typeconverterattribute?view=netframework-4.7.2) to this class. Hmm, except it's actually a `Type`. I'm not sure what you do here. – Rufus L Aug 21 '18 at 00:19
  • 1
    (FYI, `Numeric` is spelled with an `e`, not an `a`) – Rufus L Aug 21 '18 at 00:21

1 Answers1

1

Is is possible that you'd like something like this?

var converter = new Converter();

converter.Configure<double>(x => double.Parse(x));
converter.Configure<int?>(x => int.TryParse(x, out int result) ? (int?)result : null);

var value1 = converter.Convert<double>("42.123");
var value2 = converter.Convert<int?>("");

If so, here's the class that does it:

public class Converter
{
    private Dictionary<Type, Delegate> _factories = new Dictionary<Type, Delegate>();

    public void Configure<T>(Func<string, T> factory)
    {
        _factories[typeof(T)] = factory;
    }

    public T Convert<T>(string input)
    {
        return ((Func<string, T>)_factories[typeof(T)])(input);
    }
}
Enigmativity
  • 113,464
  • 11
  • 89
  • 172