c# - Exception when building Expression to call StringBuilder.Append(Object) with DateTime -


I created a ToStringBuilder (found) that shows the types and dynamically expression s creates fast toaster ways to compile.

It works well, but I've discovered it for errors on DateTime . This stringbuilder The append (object) is pressed when trying to call to pass the date time . Do I need to create an expression for box value types? How is this best done?

I have made the following test case to show failure.

  // Pass [TEST] Public Zero update time () {stringbilder sb = new stringbilder (); Sb.Append (new date time ()); } // throws [test] Public Zero expression AppendDateTime () {parameter expedition sbArgExpression = Expression.Parameter (typef (stringbilder), "SB"); Parameter expiration datetimeAgxpress = expression.pemeter (typef (datetime), "datetime"); Var Append Mode Method = Typefinder (stringbinder) .GetMethod ("Add", New [] {Typef (DateTime)}); Var call = expression Call (sbArgExpression, Append Mode, Datetime ERGation); // This line throws on var lambda = Expression.Lambda & lt; StringBuilder DATETIME, & gt; & Gt; (Call sbArgExpression, dateTimeArgExpression) .Compile (); Var datetime = new date time (); Var sb = new stringbilder (); Lambda Invoke (sb, date time); }   

Exception is ..

  System.ArgumentException was rejected by user code Message = expression type used for the 'System.DateTime' parameter Can not be used in the type of method 'System.Object' 'System.Text.StringBuilder enclosed (System.Object)' source = System.Core of StackTrace: System.Linq.Expressions.Expression.ValidateOneArgument (MethodBase method, ExpressionType nodeKind, Expression Arg, debate on System.Linq.Expressions.Expression.Call on ParameterInfo System (Expression Example, MethodInfo Method, IEnumerable`1 argument); System.Linq.Expressions.Expression.ValidateArgumentTypes (MethodBase method, ExpressionType nodeKind, pi on ReadOnlyCollection`1 & amp; Linq.Expressions.Expression.Call (Expression Example, MethodInfo Method, Expression [] Logic) Test.TestToStringBuilder.ExpressionAppendDateTime () InnerException with Less:    

resolved it, its use expression To use type AS , the active value type as non-primary object [test] Public Nine ExpressionAppendDateTime () {ParameterExpression sbArgExpression = Expression.Parameter (typeof (StringBuilder), "sb"); Parameter expiration datetimeAgxpress = expression.pemeter (typef (datetime), "datetime"); Var Append Mode Method = Typefinder (stringbinder) .GetMethod ("Add", New [] {Typef (DateTime)}); Type T = typef (datetime); Expression AGR; If (T. ISWE type & amp;! TISpramirty) {arg = expression Typeas (datetime erg excitation, typef (object)); } Else {arg = dateTimeArgExpression; } Var call = expression.col (sbArgExpression, Appendement Method, RGR); Lambda on = Expression.Lambda & LT; & Lt; StringBuilder DATETIME, & gt; & Gt; (Call, sbArgExpression, dateTimeArgExpression) .Compile (); Var datetime = new date time (); Var sb = new stringbilder (); Lambda Invoke (sb, date time); }

Comments