C#两个泛型数据能实现相加吗?
public class Generic<T>{
public T MySum( List<T> listInt)
{
Func<List<T>, int,T> S = null;
S = (list, x) =>
{
if (x == 1) { return list[x-1]; }
else
{
return Add(S(list, x - 1),list[x - 1]);
}
};
return S(listInt, listInt.Count);
}
public T Add(T num1, T num2)
{
var para1 = Expression.Parameter(typeof(T), "p1");
var para2 = Expression.Parameter(typeof(T), "p2");
var expsAdd = Expression<Func<T, T, T>>.Lambda<Func<T, T, T>>
(Expression.Add(para1, para2));
Func<T, T, T> fun = ();
return fun(num1, num2);
}
}
class Program
{
static void Main(string[] args)
{
var R = new Generic<int>();
var listInt=new List<int>{3,4};
//R.MySum(listInt);
//R.Add(2, 6);
Console.WriteLine(R.Add(listInt));
Console.WriteLine(R.Add(2, 6));
}
}
小弟是学生正在学习C#..
我没能实现两个泛型相加!!求高手来解决!!