]> O.S.I.I.S - jp/crow.git/commitdiff
Cast extention method for Type, used in binding resolution
authorjpbruyere <jp.bruyere@hotmail.com>
Wed, 3 Feb 2016 17:19:30 +0000 (18:19 +0100)
committerjpbruyere <jp.bruyere@hotmail.com>
Wed, 3 Feb 2016 17:19:30 +0000 (18:19 +0100)
src/ExtensionsMethods.cs
src/GraphicObjects/GraphicObject.cs

index af756a784ba504b21ae21bcc5e07fd4efe94441e..19193e924eece46781b360bd92eaf5f37bb208fb 100644 (file)
@@ -19,6 +19,7 @@
 //  You should have received a copy of the GNU General Public License
 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 using System;
+using System.Linq.Expressions;
 
 namespace Crow
 {
@@ -38,6 +39,18 @@ namespace Crow
                                handler(sender, e);
                        }
                }
+               /// <summary>
+               /// Type Extension method for Casting
+               /// </summary>
+               public static object Cast(this Type Type, object data)
+               {
+                       var DataParam = Expression.Parameter(typeof(object), "data");
+                       var Body = Expression.Block(Expression.Convert(Expression.Convert(DataParam, data.GetType()), Type));
+
+                       var Run = Expression.Lambda(Body, DataParam).Compile();
+                       var ret = Run.DynamicInvoke(data);
+                       return ret;
+               }
        }
 }
 
index 7fd0e1ca3fab34d2b2a7ac580ebafd130ba63e36..8bbf67b033ab4d737482232c8cb9a9aef14cbe87 100644 (file)
@@ -986,7 +986,12 @@ namespace Crow
                                                }else
                                                        targetValue = targetValue.ToString ();
                                        }
-                                       b.Source.Property.GetSetMethod ().Invoke (this, new object[] { targetValue });
+                                       if (targetValue != null)
+                                               b.Source.Property.GetSetMethod ().Invoke 
+                                               (this, new object[] { b.Source.Property.PropertyType.Cast(targetValue)});
+                                       else
+                                               b.Source.Property.GetSetMethod ().Invoke 
+                                                       (this, new object[] { targetValue });
                                        #endregion
 
                                        //if no dyn update, skip jump table