/// <summary>
		/// Sets the value of the property
		/// </summary>
		/// <param name="script">The script.</param>
		/// <param name="obj">The object.</param>
		/// <param name="v">The value to set.</param>
		public void SetValue(Script script, object obj, DynValue v)
		{
			this.CheckAccess(MemberDescriptorAccess.CanWrite, obj);

			if (IsReadonly || IsConst)
				throw new ScriptRuntimeException("userdata field '{0}.{1}' cannot be written to.", this.FieldInfo.DeclaringType.Name, this.Name);

			object value = ScriptToClrConversions.DynValueToObjectOfType(v, this.FieldInfo.FieldType, null, false);

			try
			{
				if (value is double)
					value = NumericConversions.DoubleToType(FieldInfo.FieldType, (double)value);

				FieldInfo.SetValue(IsStatic ? null : obj, value);
			}
			catch (ArgumentException)
			{
				// non-optimized setters fall here
				throw ScriptRuntimeException.UserDataArgumentTypeMismatch(v.Type, FieldInfo.FieldType);
			}
			catch (InvalidCastException)
			{
				// optimized setters fall here
				throw ScriptRuntimeException.UserDataArgumentTypeMismatch(v.Type, FieldInfo.FieldType);
			}
			catch (FieldAccessException ex)
			{
				throw new ScriptRuntimeException(ex);
			}
		}
Beispiel #2
0
        internal void SetValue(object obj, object value, DataType originalType)
        {
            try
            {
                if (value is double)
                {
                    value = ConversionHelper.DoubleToType(PropertyInfo.PropertyType, (double)value);
                }

                if (AccessMode == InteropAccessMode.LazyOptimized && m_OptimizedSetter == null)
                {
                    OptimizeSetter();
                }

                if (m_OptimizedSetter != null)
                {
                    m_OptimizedSetter(obj, value);
                }
                else
                {
                    PropertyInfo.SetValue(IsStatic ? null : obj, value, null);
                }
            }
            catch (ArgumentException)
            {
                // non-optimized setters fall here
                throw ScriptRuntimeException.UserDataArgumentTypeMismatch(originalType, PropertyInfo.PropertyType);
            }
            catch (InvalidCastException)
            {
                // optimized setters fall here
                throw ScriptRuntimeException.UserDataArgumentTypeMismatch(originalType, PropertyInfo.PropertyType);
            }
        }
        /// <summary>
        /// Sets the value of the property
        /// </summary>
        /// <param name="script">The script.</param>
        /// <param name="obj">The object.</param>
        /// <param name="v">The value to set.</param>
        public void SetValue(Script script, object obj, DynValue v)
        {
            this.CheckAccess(MemberDescriptorAccess.CanWrite, obj);

            if (m_Setter == null)
            {
                throw new ScriptRuntimeException("userdata property '{0}.{1}' cannot be written to.",
                                                 this.PropertyInfo.DeclaringType.Name, this.Name);
            }

            var value = ScriptToClrConversions.DynValueToObjectOfType(v, this.PropertyInfo.PropertyType, null, false);

            try
            {
                if (value is double d)
                {
                    value = NumericConversions.DoubleToType(this.PropertyInfo.PropertyType, d);
                }

                if (this.AccessMode == InteropAccessMode.LazyOptimized && m_OptimizedSetter == null)
                {
                    this.OptimizeSetter();
                }

                if (m_OptimizedSetter != null)
                {
                    m_OptimizedSetter(obj, value);
                }
                else
                {
                    m_Setter.Invoke(this.IsStatic ? null : obj,
                                    new[] { value }); // convoluted workaround for --full-aot Mono execution
                }
            }
            catch (ArgumentException)
            {
                // non-optimized setters fall here
                throw ScriptRuntimeException.UserDataArgumentTypeMismatch(v.Type, this.PropertyInfo.PropertyType);
            }
            catch (InvalidCastException)
            {
                // optimized setters fall here
                throw ScriptRuntimeException.UserDataArgumentTypeMismatch(v.Type, this.PropertyInfo.PropertyType);
            }
        }