Fan.Sys.Field.set C# (CSharp) Method

set() public method

public set ( object instance, object value, bool checkConst ) : void
instance object
value object
checkConst bool
return void
        public virtual void set(object instance, object value, bool checkConst)
        {
            m_parent.finish();

              // check const
              if ((m_flags & FConst.Const) != 0)
              {
            if (checkConst)
              throw ReadonlyErr.make("Cannot set const field " + qname()).val;
            else if (value != null && !isImmutable(value))
              throw ReadonlyErr.make("Cannot set const field " + qname() + " with mutable value").val;
              }

              // check static
              if ((m_flags & FConst.Static) != 0)
            throw ReadonlyErr.make("Cannot set static field " + qname()).val;

              // check generic type (the .NET runtime will check non-generics)
              if (m_type.isGenericInstance() && value != null)
              {
            if (!@typeof(value).@is(m_type.toNonNullable()))
              throw ArgErr.make("Wrong type for field " + qname() + ": " + m_type + " != " + @typeof(value)).val;
              }

              if (m_setter != null)
              {
            m_setter.invoke(instance, new object[] { value });
            return;
              }

              try
              {
            m_reflect.SetValue(instance, unbox(value));
              }
              catch (ArgumentException e)
              {
            throw ArgErr.make(e).val;
              }
              catch (Exception e)
              {
            if (m_reflect == null)
              throw Err.make("Field not mapped to System.Reflection correctly").val;

            throw Err.make(e).val;
              }
        }

Same methods

Field::set ( object instance, object value ) : void

Usage Example

Beispiel #1
0
        private static object doTrap(object self, string name, List args, Type type)
        {
            Slot slot = type.slot(name, true);

            if (slot is Method)
            {
                Method m = (Method)slot;
                return(m.m_func.callOn(self, args));
            }
            else
            {
                Field f       = (Field)slot;
                int   argSize = (args == null) ? 0 : args.sz();
                if (argSize == 0)
                {
                    return(FanUtil.box(f.get(self)));
                }

                if (argSize == 1)
                {
                    object val = args.get(0);
                    f.set(self, val);
                    return(FanUtil.box(val));
                }

                throw ArgErr.make("Invalid number of args to get or set field '" + name + "'").val;
            }
        }
All Usage Examples Of Fan.Sys.Field::set