public override void SetValue(object obj, object val, BindingFlags invokeAttr, Binder binder, CultureInfo culture)
 {
     if (!IsStatic)
     {
         if (obj == null)
         {
             throw new TargetException("Non-static field requires a target");
         }
         if (!DeclaringType.IsAssignableFrom(obj.GetType()))
         {
             throw new ArgumentException(string.Format(
                                             "Field {0} defined on type {1} is not a field on the target object which is of type {2}.",
                                             Name, DeclaringType, obj.GetType()),
                                         "obj");
         }
     }
     if (IsLiteral)
     {
         throw new FieldAccessException("Cannot set a constant field");
     }
     if (binder == null)
     {
         binder = Type.DefaultBinder;
     }
     CheckGeneric();
     if (val != null)
     {
         RuntimeType fieldType = (RuntimeType)FieldType;
         val = fieldType.CheckValue(val, binder, culture, invokeAttr);
     }
     SetValueInternal(this, obj, val);
 }
        protected virtual void EnsureContextIsAssignanbleFromDeclaringType()
        {
            if (Context != null)
            {
                Type contextType = Context.GetType();

                if (!DeclaringType.IsAssignableFrom(contextType))
                {
                    throw new MemberAccessException(string.Format("The type {0} is not assignable from type {1}", DeclaringType.Name, contextType));
                }
            }
        }
        public override object GetValue(object obj)
        {
            if (!IsStatic)
            {
                if (obj == null)
                {
                    throw new TargetException("Non-static field requires a target");
                }
                if (!DeclaringType.IsAssignableFrom(obj.GetType()))
                {
                    throw new ArgumentException(string.Format(
                                                    "Field {0} defined on type {1} is not a field on the target object which is of type {2}.",
                                                    Name, DeclaringType, obj.GetType()),
                                                "obj");
                }
            }

            if (!IsLiteral)
            {
                CheckGeneric();
            }
            return(GetValueInternal(obj));
        }
Example #4
0
 public override void SetValue(object obj, object val, BindingFlags invokeAttr, Binder binder, CultureInfo culture)
 {
     if (!IsStatic)
     {
         if (obj == null)
         {
             throw new TargetException("Non-static field requires a target");
         }
         if (!DeclaringType.IsAssignableFrom(obj.GetType()))
         {
             throw new ArgumentException(string.Format(
                                             "Field {0} defined on type {1} is not a field on the target object which is of type {2}.",
                                             Name, DeclaringType, obj.GetType()),
                                         "obj");
         }
     }
     if (IsLiteral)
     {
         throw new FieldAccessException("Cannot set a constant field");
     }
     if (binder == null)
     {
         binder = Binder.DefaultBinder;
     }
     CheckGeneric();
     if (val != null)
     {
         object newVal;
         newVal = binder.ChangeType(val, type, culture);
         if (newVal == null)
         {
             throw new ArgumentException("Object type " + val.GetType() + " cannot be converted to target type: " + type, "val");
         }
         val = newVal;
     }
     SetValueInternal(this, obj, val);
 }
 public bool IsForType(Type type)
 {
     return(DeclaringType.IsAssignableFrom(type));
 }