示例#1
0
        /// <summary>
        /// 获取属性值
        /// </summary>
        /// <param name="target">拥有该成员的类实例</param>
        /// <returns></returns>
        private object Get(object target)
        {
            if (!_member.CanRead)
            {
                throw new XFrameworkException("{0} is unreadable", base.FullName);
            }

            if (_get == null)
            {
                _get = PropertyInvoker.InitializeGetInvoke(this);
            }
            return(_get(target));
        }
示例#2
0
 /// <summary>
 /// 设置属性值
 /// </summary>
 /// <param name="target">拥有该成员的类实例</param>
 /// <param name="value">字段/属性值</param>
 private void Set(object target, object value)
 {
     if (!_member.CanWrite)
     {
         throw new XFrameworkException("{0} is unwritable", base.FullName);
     }
     //value = value ?? TypeUtils.GetNullValue(_member.PropertyType);
     //if (value != null)
     //{
     //    if (value.GetType() != this.DataType) value = Convert.ChangeType(value, this.DataType);
     //}
     _set = _set ?? PropertyInvoker.InitializeSetInvoke(this);
     _set(target, value ?? TypeUtils.GetNullValue(_member.PropertyType));
 }
示例#3
0
        // 初始化 Set 动态方法
        static Action <object, object> InitializeSetInvoke(PropertyInvoker invoke)
        {
            MethodInfo    method        = invoke.SetMethod;
            DynamicMethod dynamicMethod = new DynamicMethod(method.Name, null, new Type[] { typeof(object), typeof(object) }, method.Module);
            ILGenerator   g             = dynamicMethod.GetILGenerator();
            Type          paramType     = method.GetParameters()[0].ParameterType;

            if (!method.IsStatic)
            {
                g.Emit(OpCodes.Ldarg_0);                                                  //Load the first argument (target object)
                g.Emit(OpCodes.Castclass, method.DeclaringType);                          //Cast to the source type
            }
            g.Emit(OpCodes.Ldarg_1);                                                      //Load the second argument (value object)
            g.EmitCast(paramType);
            g.EmitCall(method.IsVirtual ? OpCodes.Callvirt : OpCodes.Call, method, null); //Set the property value
            g.Emit(OpCodes.Ret);

            return(dynamicMethod.CreateDelegate(typeof(Action <object, object>)) as Action <object, object>);
        }
示例#4
0
        // 初始化 Get 动态方法
        static Func <object, object> InitializeGetInvoke(PropertyInvoker invoke)
        {
            MethodInfo    method        = invoke.GetMethod;
            DynamicMethod dynamicMethod = new DynamicMethod(method.Name, typeof(object), new Type[] { typeof(object) }, method.Module);
            ILGenerator   g             = dynamicMethod.GetILGenerator();

            if (!method.IsStatic)
            {
                g.Emit(OpCodes.Ldarg_0);                                                  //Load the first argument,(target object)
                g.Emit(OpCodes.Castclass, invoke.Member.DeclaringType);                   //Cast to the source type
            }
            g.EmitCall(method.IsVirtual ? OpCodes.Callvirt : OpCodes.Call, method, null); //Get the property value
            if (method.ReturnType.IsValueType)
            {
                g.Emit(OpCodes.Box, method.ReturnType);                                //Box if necessary
            }
            g.Emit(OpCodes.Ret);

            return(dynamicMethod.CreateDelegate(typeof(Func <object, object>)) as Func <object, object>);
        }
示例#5
0
        /// <summary>
        /// 创建成员反射器
        /// </summary>
        /// <param name="member">元数据</param>
        /// <returns></returns>
        public static MemberInvokerBase Create(MemberInfo member)
        {
            MemberInvokerBase invoker = null;

            if (member.MemberType == MemberTypes.Property)
            {
                invoker = new PropertyInvoker((PropertyInfo)member);
            }
            if (member.MemberType == MemberTypes.Field)
            {
                invoker = new FieldInvoker((FieldInfo)member);
            }
            if (member.MemberType == MemberTypes.Method)
            {
                invoker = new MethodInvoker((MethodInfo)member);
            }
            if (invoker == null)
            {
                throw new XFrameworkException("{0}.{1} not supported", member.ReflectedType, member.Name);
            }
            return(invoker);
        }