Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method with Generic Type, set value

I have a method which is something like below and i want to set the value of it with input string.

How would i go about it? Any advice will be greatly appreciated

private static void QueueCheckNAdd<T>(ref T param, string input)
    {
        param.DoSomethingLikeSetValue(input);
    }

for your reference, the generic type is something like int or double

like image 401
CJC Avatar asked Oct 18 '25 10:10

CJC


1 Answers

param = (T)(object)Convert.ChangeType(input, typeof(T));

The casts are necessary to convince the compiler that the result is really of type T.

like image 140
SLaks Avatar answered Oct 19 '25 23:10

SLaks



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!