dimanche 13 mars 2016

c# how to GetMethod for "operator +"

I want to summarize values of DataColumn in DataTable table. I don't know DataType of DataColumn.

So I write next test code (C# 2.0 or higher)

  object v1 = 1.1;
  object v2 = 2;

  System.Type dataType = typeof(Double);
//System.Type dataType = typeof(Int32);

  MethodInfo sumMethod = dataType.GetMethod("op_Addition", BindingFlags.Static | BindingFlags.Public);

  object v3 = sumMethod.Invoke(null, new object[] { Convert.ChangeType(v1, dataType), Convert.ChangeType(v2, dataType) });

On Run it raise error System.NullReferenceException

sumMethod is null. May be Double doesn't have "operator +"

How to fix it?





Aucun commentaire:

Enregistrer un commentaire