Esempio n. 1
0
        protected override Action <object, int, int, T> BuildSetter()
        {
            EnsureInitialized();
            if (m_readOnly)
            {
                return(null);
            }

            var        p_obj = Expression.Parameter(typeof(object));
            var        p_i1  = Expression.Parameter(typeof(int));
            var        p_i2  = Expression.Parameter(typeof(int));
            var        p_val = Expression.Parameter(ValueType);
            Expression param = p_val;

            Expression access = Expression.MakeMemberAccess(Expression.Convert(p_obj, ObjectType), MemberInfo);

            return(Expression.Lambda <Action <object, int, int, T> >(
                       Expression.Assign(
                           access,
                           MemberType.IsAssignableFrom(ValueType) ?
                           param : Expression.Convert(p_val, MemberType)
                           ),
                       p_obj, p_i1, p_i2, p_val
                       ).Compile());
        }
Esempio n. 2
0
 /// <summary>
 /// Sets the runtime current value of this property of field
 /// </summary>
 public bool SetValue(object value)
 {
     try
     {
         if (!CanWrite || !MemberExists)
         {
             return(false);
         }
         if (value == null && MemberType.IsValueType)
         {
             return(false);
         }
         if (MemberType.IsEnum)
         {
             if (!Enum.IsDefined(MemberType, value + ""))
             {
                 return(false);
             }
             else if (value != null && value.GetType() != MemberType)
             {
                 value = Enum.Parse(MemberType, value + "");
             }
         }
         if (value != null && !MemberType.IsAssignableFrom(value.GetType()))
         {
             return(false);
         }
         if (Member is PropertyInfo)
         {
             (Member as PropertyInfo).SetValue(ProbertyObject, value, null);
         }
         else if (Member is FieldInfo)
         {
             (Member as FieldInfo).SetValue(ProbertyObject, value);
         }
         return(true);
     }
     catch { return(false); }
 }
Esempio n. 3
0
        /// <summary>
        /// Creates a method that sets the property from the object.
        /// </summary>
        /// <typeparam name="TObject">The type of the object.</typeparam>
        /// <typeparam name="TValue">The type of the value.</typeparam>
        /// <returns>An accessor function.</returns>
        public Action <TObject, TValue> CreateSetMethod <TObject, TValue>()
        {
            var dm = new DynamicMethod(
                String.Format(CultureInfo.InvariantCulture, "Set-{0}-{1}-{2}", typeof(TObject).FullName, Name, Guid.NewGuid()),
                null,
                new Type[] { typeof(TObject), typeof(TValue) },
                true);
            var il = dm.GetILGenerator();

            il.Emit(OpCodes.Ldarg_0);
            il.Emit(OpCodes.Ldarg_1);

            // if the types don't match, see if we're trying to put a list of things into a thing
            // in that case, we want a FirstOrDefault
            if (typeof(TValue) != MemberType && !MemberType.IsAssignableFrom(typeof(TValue)))
            {
                var enumType = typeof(IEnumerable <>).MakeGenericType(MemberType);
                if (typeof(TValue).GetInterfaces().Contains(enumType))
                {
                    var firstOrDefault = typeof(System.Linq.Enumerable).GetMethods().Single(m => m.Name == "FirstOrDefault" && m.GetParameters().Length == 1).MakeGenericMethod(MemberType);
                    il.Emit(OpCodes.Call, firstOrDefault);
                }
            }

            if (FieldInfo != null)
            {
                il.Emit(OpCodes.Stfld, FieldInfo);
            }
            else
            {
                il.Emit(OpCodes.Callvirt, SetMethodInfo);
            }

            il.Emit(OpCodes.Ret);

            return((Action <TObject, TValue>)dm.CreateDelegate(typeof(Action <TObject, TValue>)));
        }