Clone() public méthode

Creates a shallow copy of this object with a new value
public Clone ( dynamic newValue ) : CseObject
newValue dynamic Value to give the cloned object
Résultat CseObject
Exemple #1
0
        ///
        /// <summary>
        ///		Applies numeric affirmation (i.e. unary plus) to numeric values
        /// </summary>
        ///
        /// <param name="obj">The CseObject with the value to affirm</param>
        ///
        /// <returns>The CseObject after affirmation</returns>
        ///
        /// <exception cref="CseLogicExceptionType.CANT_AFFIRM_NON_NUM" />
        ///
        internal static CseObject Affirm(CseObject obj)
        {
            CseObject result = (CseObject)obj.Clone();

            dynamic value = obj.Value;
            double  numValue;

            if (value is string)
            {
                throw new CseLogicException(CseLogicExceptionType.CANT_AFFIRM_NON_NUM, value.ToString());
            }
            else if (!double.TryParse(value.ToString(), out numValue))
            {
                MethodInfo mi = value.GetType().GetMethod(OpOverloadNames.UPLUS);
                if (null != mi)
                {
                    result.Value = value.GetType().InvokeMember(OpOverloadNames.UPLUS, OpOverloadNames.Flags, null, CsEval.evalEnvironment, new object[] { value });
                    return(result);
                }
                else
                {
                    throw new CseLogicException(CseLogicExceptionType.CANT_AFFIRM_NON_NUM, value.ToString());
                }
            }

            return(result);
        }
Exemple #2
0
        ///
        /// <summary>
        ///		Applies numeric negation (i.e. unary minus) to numeric values
        /// </summary>
        ///
        /// <param name="obj">The CseObject with the value to negate</param>
        ///
        /// <returns>The CseObject after negation</returns>
        ///
        /// <exception cref="CseLogicExceptionType.CANT_NEGATE_NON_NUM" />
        /// <exception cref="CseLogicExceptionType.ERROR_NEGATING_VALUE_OF_TYPE" />
        ///
        internal static CseObject Negate(CseObject obj)
        {
            CseObject result = (CseObject)obj.Clone();

            dynamic value = obj.Value;
            double  numValue;

            if (value is string)
            {
                throw new CseLogicException(CseLogicExceptionType.CANT_NEGATE_NON_NUM, value.ToString());
            }
            else if (!double.TryParse(value.ToString(), out numValue))
            {
                MethodInfo mi = value.GetType().GetMethod(OpOverloadNames.UMINUS);
                if (null != mi)
                {
                    result.Value = value.GetType().InvokeMember(OpOverloadNames.UMINUS, OpOverloadNames.Flags, null, CsEval.evalEnvironment, new object[] { value });
                    return(result);
                }
                else
                {
                    throw new CseLogicException(CseLogicExceptionType.CANT_NEGATE_NON_NUM, value.ToString());
                }
            }

            numValue    *= -1;
            result.Value = numValue;

            try {
                result = CastExp.Parse(CsEval.evalEnvironment, value.GetType().Name, result);
            }
            catch {
                if (value.ToString().ToLower().Contains("e"))
                {
                    result = LiteralExp.ParseEType(numValue.ToString());
                }
                else if (value.ToString().Contains("."))
                {
                    result = LiteralExp.ParseFloatType(numValue.ToString(), null);
                }
                else
                {
                    throw new CseLogicException(CseLogicExceptionType.ERROR_NEGATING_VALUE_OF_TYPE, value.ToString(), value.GetType().Name);
                }
            }

            return(result);
        }
Exemple #3
0
		///
		/// <summary>
		///		A holistic method to use for testing coercion of objects
		/// </summary>
		/// 
		/// <param name="fromArg">The object to be coerced</param>
		/// 
		/// <param name="to">The type to convert the object to.</param>
		/// 
		/// <returns>True if the coercion is valid, false otherwise</returns>
		///
		public static bool CanConvertType(CseObject fromArg, Type to) {

			CseObject fromData = (CseObject)fromArg.Clone();


			if (fromData == null || to == null) {
				throw new ArgumentException("Param '" + (fromData == null ? "fromData" : "to") + "' must not be null");
			}

			Type from = fromData.ValueType;

			// null literal conversion 6.1.5
			if (fromArg.Value == null) {
				return IsNullableType(to);
			}

			// identity conversion 6.1.1
			if (from.GetHashCode().Equals(to.GetHashCode()))
				return true;

			// implicit constant expressions 6.1.9
			if (fromData.IsLiteral) {
				bool canConv = false;

				dynamic num = fromData.Value;
				if (from == typeof(int)) {
					switch (Type.GetTypeCode(to)) {
						case TypeCode.SByte:
							if (num >= sbyte.MinValue && num <= sbyte.MaxValue)
								canConv = true;
							break;
						case TypeCode.Byte:
							if (num >= byte.MinValue && num <= byte.MaxValue)
								canConv = true;
							break;
						case TypeCode.Int16:
							if (num >= short.MinValue && num <= short.MaxValue)
								canConv = true;
							break;
						case TypeCode.UInt16:
							if (num >= ushort.MinValue && num <= ushort.MaxValue)
								canConv = true;
							break;
						case TypeCode.UInt32:
							if (num >= uint.MinValue && num <= uint.MaxValue)
								canConv = true;
							break;
						case TypeCode.UInt64:
							if (num >= 0)
								canConv = true;
							break;
					}
				}
				else if (from == typeof(long)) {
					if (to == typeof(ulong)) {
						if (num >= 0)
							canConv = true;
					}
				}

				if (canConv)
					return true;
			}

			// string conversion
			// TODO: check if this is necessary
			if (from == typeof(string)) {
				if (to == typeof(object))
					return true;
				else
					return false;
			}


			// implicit nullable conversion 6.1.4
			if (IsNullableType(to)) {

				if (IsNullableType(fromData.ValueType)) {

					// If the source value is null, then just return successfully (because the target value is a nullable type)
					if (fromData.Value == null) {
						return true;
					}

					fromData.CompileTimeType = Nullable.GetUnderlyingType(fromData.ValueType);
				}

				return CanConvertType(fromData, Nullable.GetUnderlyingType(to));

			}

			// implicit enumeration conversion 6.1.3
			long longTest = -1;

			if (fromData.IsLiteral && to.IsEnum && long.TryParse(fromData.Value.ToString(), out longTest)) {
				if (longTest == 0)
					return true;
			}

			// implicit reference conversion 6.1.5
			if (!from.IsValueType && !to.IsValueType) {
				bool? irc = ImpRefConv(fromData, to);
				if (irc.HasValue)
					return irc.Value;
			}

			// implicit numeric conversion 6.1.2
			try {
				object fromObj = null;
				double dblTemp;
				decimal decTemp;
				char chrTemp;
				fromObj = Activator.CreateInstance(from);

				if (char.TryParse(fromObj.ToString(), out chrTemp) || double.TryParse(fromObj.ToString(), out dblTemp) || decimal.TryParse(fromObj.ToString(), out decTemp)) {
					if (NumConv.ContainsKey(from) && NumConv[from].Contains(to))
						return true;
					else
						return CrawlThatShit(to.GetHashCode(), from, new List<int>());
				}
				//else {
				//   return CrawlThatShit(to.GetHashCode(), from, new List<int>());
				//}
			}
			catch {
				//return CrawlThatShit(to.GetHashCode(), from, new List<int>());
			}

			return false;
		}
Exemple #4
0
        ///
        /// <summary>
        ///		A holistic method to use for testing coercion of objects
        /// </summary>
        ///
        /// <param name="fromArg">The object to be coerced</param>
        ///
        /// <param name="to">The type to convert the object to.</param>
        ///
        /// <returns>True if the coercion is valid, false otherwise</returns>
        ///
        public static bool CanConvertType(CseObject fromArg, Type to)
        {
            CseObject fromData = (CseObject)fromArg.Clone();


            if (fromData == null || to == null)
            {
                throw new ArgumentException("Param '" + (fromData == null ? "fromData" : "to") + "' must not be null");
            }

            Type from = fromData.ValueType;

            // null literal conversion 6.1.5
            if (fromArg.Value == null)
            {
                return(IsNullableType(to));
            }

            // identity conversion 6.1.1
            if (from.GetHashCode().Equals(to.GetHashCode()))
            {
                return(true);
            }

            // implicit constant expressions 6.1.9
            if (fromData.IsLiteral)
            {
                bool canConv = false;

                dynamic num = fromData.Value;
                if (from == typeof(int))
                {
                    switch (Type.GetTypeCode(to))
                    {
                    case TypeCode.SByte:
                        if (num >= sbyte.MinValue && num <= sbyte.MaxValue)
                        {
                            canConv = true;
                        }
                        break;

                    case TypeCode.Byte:
                        if (num >= byte.MinValue && num <= byte.MaxValue)
                        {
                            canConv = true;
                        }
                        break;

                    case TypeCode.Int16:
                        if (num >= short.MinValue && num <= short.MaxValue)
                        {
                            canConv = true;
                        }
                        break;

                    case TypeCode.UInt16:
                        if (num >= ushort.MinValue && num <= ushort.MaxValue)
                        {
                            canConv = true;
                        }
                        break;

                    case TypeCode.UInt32:
                        if (num >= uint.MinValue && num <= uint.MaxValue)
                        {
                            canConv = true;
                        }
                        break;

                    case TypeCode.UInt64:
                        if (num >= 0)
                        {
                            canConv = true;
                        }
                        break;
                    }
                }
                else if (from == typeof(long))
                {
                    if (to == typeof(ulong))
                    {
                        if (num >= 0)
                        {
                            canConv = true;
                        }
                    }
                }

                if (canConv)
                {
                    return(true);
                }
            }

            // string conversion
            // TODO: check if this is necessary
            if (from == typeof(string))
            {
                if (to == typeof(object))
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }


            // implicit nullable conversion 6.1.4
            if (IsNullableType(to))
            {
                if (IsNullableType(fromData.ValueType))
                {
                    // If the source value is null, then just return successfully (because the target value is a nullable type)
                    if (fromData.Value == null)
                    {
                        return(true);
                    }

                    fromData.CompileTimeType = Nullable.GetUnderlyingType(fromData.ValueType);
                }

                return(CanConvertType(fromData, Nullable.GetUnderlyingType(to)));
            }

            // implicit enumeration conversion 6.1.3
            long longTest = -1;

            if (fromData.IsLiteral && to.IsEnum && long.TryParse(fromData.Value.ToString(), out longTest))
            {
                if (longTest == 0)
                {
                    return(true);
                }
            }

            // implicit reference conversion 6.1.5
            if (!from.IsValueType && !to.IsValueType)
            {
                bool?irc = ImpRefConv(fromData, to);
                if (irc.HasValue)
                {
                    return(irc.Value);
                }
            }

            // implicit numeric conversion 6.1.2
            try {
                object  fromObj = null;
                double  dblTemp;
                decimal decTemp;
                char    chrTemp;
                fromObj = Activator.CreateInstance(from);

                if (char.TryParse(fromObj.ToString(), out chrTemp) || double.TryParse(fromObj.ToString(), out dblTemp) || decimal.TryParse(fromObj.ToString(), out decTemp))
                {
                    if (NumConv.ContainsKey(from) && NumConv[from].Contains(to))
                    {
                        return(true);
                    }
                    else
                    {
                        return(CrawlThatShit(to.GetHashCode(), from, new List <int>()));
                    }
                }
                //else {
                //   return CrawlThatShit(to.GetHashCode(), from, new List<int>());
                //}
            }
            catch {
                //return CrawlThatShit(to.GetHashCode(), from, new List<int>());
            }

            return(false);
        }
Exemple #5
0
		///
		/// <summary>
		///		Applies numeric negation (i.e. unary minus) to numeric values
		/// </summary>
		/// 
		/// <param name="obj">The CseObject with the value to negate</param>
		/// 
		/// <returns>The CseObject after negation</returns>
		/// 
		/// <exception cref="CseLogicExceptionType.CANT_NEGATE_NON_NUM" />
		/// <exception cref="CseLogicExceptionType.ERROR_NEGATING_VALUE_OF_TYPE" />
		/// 
		internal static CseObject Negate(CseObject obj) {
			CseObject result = (CseObject)obj.Clone();

			dynamic value = obj.Value;
			double numValue;

			if (value is string)
				throw new CseLogicException(CseLogicExceptionType.CANT_NEGATE_NON_NUM, value.ToString());
			else if (!double.TryParse(value.ToString(), out numValue)) {
				MethodInfo mi = value.GetType().GetMethod(OpOverloadNames.UMINUS);
				if (null != mi) {
					result.Value = value.GetType().InvokeMember(OpOverloadNames.UMINUS, OpOverloadNames.Flags, null, CsEval.evalEnvironment, new object[] { value });
					return result;
				}
				else
					throw new CseLogicException(CseLogicExceptionType.CANT_NEGATE_NON_NUM, value.ToString());
			}

			numValue *= -1;
			result.Value = numValue;

			try {
				result = CastExp.Parse(CsEval.evalEnvironment, value.GetType().Name, result);
			}
			catch {
				if (value.ToString().ToLower().Contains("e"))
					result = LiteralExp.ParseEType(numValue.ToString());
				else if (value.ToString().Contains("."))
					result = LiteralExp.ParseFloatType(numValue.ToString(), null);
				else
					throw new CseLogicException(CseLogicExceptionType.ERROR_NEGATING_VALUE_OF_TYPE, value.ToString(), value.GetType().Name);
			}

			return result;
		}
Exemple #6
0
		///
		/// <summary>
		///		Applies numeric affirmation (i.e. unary plus) to numeric values
		/// </summary>
		/// 
		/// <param name="obj">The CseObject with the value to affirm</param>
		/// 
		/// <returns>The CseObject after affirmation</returns>
		/// 
		/// <exception cref="CseLogicExceptionType.CANT_AFFIRM_NON_NUM" />
		/// 
		internal static CseObject Affirm(CseObject obj) {
			CseObject result = (CseObject)obj.Clone();

			dynamic value = obj.Value;
			double numValue;

			if (value is string)
				throw new CseLogicException(CseLogicExceptionType.CANT_AFFIRM_NON_NUM, value.ToString());
			else if (!double.TryParse(value.ToString(), out numValue)) {
				MethodInfo mi = value.GetType().GetMethod(OpOverloadNames.UPLUS);
				if (null != mi) {
					result.Value = value.GetType().InvokeMember(OpOverloadNames.UPLUS, OpOverloadNames.Flags, null, CsEval.evalEnvironment, new object[] { value });
					return result;
				}
				else
					throw new CseLogicException(CseLogicExceptionType.CANT_AFFIRM_NON_NUM, value.ToString());
			}

			return result;
		}