I wish to be able to modify some parameter value of generic type T, and I am trying to do this with LINQ, but I cannot figure it out.
Here is what I have:
public static T ConvertParam(T param)
{
T val = param.Select(i => { i = (T)ModifyValue(Convert.ToDouble(i)); });
return val;
}
public static UInt64 ModifyValue(double value)
{
UInt64 result = (UInt64)(value) * 3 + 1;
return result;
}
In this case the problem resides in the value returned by "ModifyValue()" that needs to be cast to whatever T param contains, but I am not sure how to do that. T can be an int, uint, int[], uint[], etc.
Handling the arrays is also tricky.
I have also tried with a normal for loop as here below:
public static T ConvertParam(T param)
{
List<UInt64> output = new List<UInt64>();
foreach (var v in param)
{
output.Add(ModifyValue(Convert.ToDouble(v));
}
return output.ToArray();
}
The problem here again is that the returned value may or may not be an array. It should just return the modified version of T param.
EDIT
Note that ModifyValue() is just a dummy example. "param" MUST be generic!
EDIT 2
Maybe I could do something around these lines (not sure how to though):
public static T ConvertParam(T param)
{
T ret = param.ToList().ConvertAll(i => i = new T() { ModifyValue(Convert.ToDouble(i)) });
return ret;
}
ConvertParamshould have generic type argumentT, otherwise ist declaration is invalidModifyValueis doing, is yourvaluealways adouble?ConvertParam? Why do you think it should be generic?UInt64?