private static Action <TObj, TProp> CreateSetter <TObj, TProp>(FieldInfo fieldInfo)
 {
     if (fieldInfo.IsInitOnly || fieldInfo.IsLiteral)
     {
         throw ReflectionError.CannotCreateSetterForReadOnly(fieldInfo);
     }
     return(CreateSetter <TObj, TProp>(fieldInfo.DeclaringType, fieldInfo.Name, fieldInfo.FieldType));
 }
 private static Action <TObj, TProp> CreateSetter <TObj, TProp>(PropertyInfo propertyInfo)
 {
     if (!propertyInfo.GetIndexParameters().IsNullOrEmpty())
     {
         throw ReflectionError.CannotCreateSetterForIndexer(propertyInfo);
     }
     if (!propertyInfo.CanWrite)
     {
         throw ReflectionError.CannotCreateSetterForReadOnly(propertyInfo);
     }
     return(CreateSetter <TObj, TProp>(propertyInfo.DeclaringType, propertyInfo.Name, propertyInfo.PropertyType));
 }
Exemple #3
0
        private static TDelegate GetCreator <TDelegate>(Type type, Type[] argumentTypes)
        {
            var constructor = type.GetConstructor(BindingFlags.Instance | BindingFlags.Public, null,
                                                  CallingConventions.HasThis, argumentTypes, new ParameterModifier[0]);

            if (constructor == null)
            {
                throw ReflectionError.CtorNotFound(type, argumentTypes);
            }

            var parameterExpressions = argumentTypes.Select(Expression.Parameter).ToList();

            var newExpression = Expression.New(constructor, parameterExpressions);

            return(Expression.Lambda <TDelegate>(newExpression, parameterExpressions).Compile());
        }
        public static Action <TObj, TProp> CreateSetter <TObj, TProp>(MemberInfo memberInfo)
        {
            var property = memberInfo as PropertyInfo;

            if (property != null)
            {
                return(CreateSetter <TObj, TProp>(property));
            }

            var field = memberInfo as FieldInfo;

            if (field != null)
            {
                return(CreateSetter <TObj, TProp>(field));
            }

            throw ReflectionError.CannotCreateSetter(memberInfo);
        }