From 9e09e0561baf7a898c308180d142cecea9e56b8f Mon Sep 17 00:00:00 2001 From: jpbruyere Date: Wed, 3 Feb 2016 18:19:30 +0100 Subject: Cast extention method for Type, used in binding resolution --- src/ExtensionsMethods.cs | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'src/ExtensionsMethods.cs') diff --git a/src/ExtensionsMethods.cs b/src/ExtensionsMethods.cs index af756a78..19193e92 100644 --- a/src/ExtensionsMethods.cs +++ b/src/ExtensionsMethods.cs @@ -19,6 +19,7 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . using System; +using System.Linq.Expressions; namespace Crow { @@ -38,6 +39,18 @@ namespace Crow handler(sender, e); } } + /// + /// Type Extension method for Casting + /// + 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; + } } } -- cgit v1.3.1-4-g156e