Loading

代码片段

Section 01: Expression表达式转换为委托

/// <summary>
        /// 直接返回了强类型的委托
        /// </summary>
        private static Action<TInstance, TProperty> BuildSetPropertyAction<TInstance, TProperty>(PropertyInfo propertyInfo)
        {
            var instanceParam = Expression.Parameter(typeof(TInstance), "instance");
            var valueParam = Expression.Parameter(typeof(TProperty), "value");
            var propertyProperty = Expression.Property(instanceParam, propertyInfo);
            var assignExpression = Expression.Assign(propertyProperty, valueParam);
            var lambdaExpression = Expression.Lambda<Action<TInstance, TProperty>>(assignExpression, instanceParam, valueParam);
            return lambdaExpression.Compile();
        }

Section 02: 利用委托替换Reflection

 public partial class Test : System.Web.UI.Page
    {
        // 委托
        public delegate int AddMethod(int a, int b);

        protected void Page_Load(object sender, EventArgs e)
        {
            // 实现一:
            var obj = new TestObject();
            var objType = obj.GetType();
            var add = objType.GetMethod("Add");
            var d = (AddMethod)Delegate.CreateDelegate(typeof(AddMethod), obj, add);
            for (var i = 0; i < 100; i++) d(2, 3);

            //实现二:
            var ddd = (Func<TestObject, int, int, int>)Delegate.CreateDelegate(typeof(Func<TestObject, int, int, int>), add);

            Int32 result = ddd(obj,34, 34);
        }
    }

    public sealed class TestObject
    {
        public int Add(int a, int b)
        {
            return a + b;
        }
    }


Section 3: HttpRequest加载obj所需的数据

public static void LoadDataFromHttpRequest(HttpRequest request, object obj)
        {
            PropertyInfo[] properties = obj.GetType().GetProperties();
            foreach (PropertyInfo p in properties)
            {
                object val = Convert.ChangeType(request[p.Name], p.PropertyType);
                p.SetValue(obj,val,null);
            }
        }


Section 4: Reflection 之属性相关操作

引入名称空间

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Reflection;
using System.Reflection.Emit;
using System.Linq.Expressions;
using System.Globalization;
using System.Diagnostics;

 public partial class WebForm2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var stopwatch = Stopwatch.StartNew();
            object Nothing = System.Reflection.Missing.Value;
            //stype = typeof(T).GetGenericArguments()[0].Name.ToLower();  朱工的泛型获取方法
            Ship ship = new Ship();
            PropertyInfo propertyInfo = ship.GetType().GetProperty("Age");
            propertyInfo.SetValue(ship, Convert.ChangeType("56", propertyInfo.PropertyType), null);
            Object objVal = propertyInfo.GetValue(ship,null);
            Boolean b = propertyInfo.PropertyType.IsClass;       //判断该类型是否为类
            Boolean isb = propertyInfo.PropertyType.IsValueType; //判断该值是否为值类型
            stopwatch.Stop();
            Response.Write(String.Format("{0}: {1}", "操行操作用了:"
                , stopwatch.Elapsed.TotalMilliseconds.ToString(CultureInfo.InvariantCulture)));
        }
    }

    public class Ship
    {
        public String ShipName { get; set; }

        public Int32 Age { get; set; }
    }


Section 5:

public partial class WebForm1 : System.Web.UI.Page
    {
        List<Dog> data = new List<Dog>();

        protected void Page_Load(object sender, EventArgs e)
        {
            Type t1 = data.GetType().GetGenericArguments()[0];

            Type t2 = data.GetType().GetProperty("Item").PropertyType; 
        }
    }

    public class Dog
    { 
    
    }

 

 

 

 

 

posted @ 2014-01-28 16:13  Sam Xiao  阅读(86)  评论(0)    收藏  举报