Mono.CSharp.Constant.CreateConstantFromValue C# (CSharp) Method

CreateConstantFromValue() public static method

public static CreateConstantFromValue ( System.TypeSpec t, object v, Mono.CSharp.Location loc ) : Constant
t System.TypeSpec
v object
loc Mono.CSharp.Location
return Constant
		public static Constant CreateConstantFromValue (TypeSpec t, object v, Location loc)
		{
			switch (t.BuiltinType) {
			case BuiltinTypeSpec.Type.Int:
				return new IntConstant (t, (int) v, loc);
			case BuiltinTypeSpec.Type.String:
				return new StringConstant (t, (string) v, loc);
			case BuiltinTypeSpec.Type.UInt:
				return new UIntConstant (t, (uint) v, loc);
			case BuiltinTypeSpec.Type.Long:
				return new LongConstant (t, (long) v, loc);
			case BuiltinTypeSpec.Type.ULong:
				return new ULongConstant (t, (ulong) v, loc);
			case BuiltinTypeSpec.Type.Float:
				return new FloatConstant (t, (float) v, loc);
			case BuiltinTypeSpec.Type.Double:
				return new DoubleConstant (t, (double) v, loc);
			case BuiltinTypeSpec.Type.Short:
				return new ShortConstant (t, (short) v, loc);
			case BuiltinTypeSpec.Type.UShort:
				return new UShortConstant (t, (ushort) v, loc);
			case BuiltinTypeSpec.Type.SByte:
				return new SByteConstant (t, (sbyte) v, loc);
			case BuiltinTypeSpec.Type.Byte:
				return new ByteConstant (t, (byte) v, loc);
			case BuiltinTypeSpec.Type.Char:
				return new CharConstant (t, (char) v, loc);
			case BuiltinTypeSpec.Type.Bool:
				return new BoolConstant (t, (bool) v, loc);
			case BuiltinTypeSpec.Type.Decimal:
				return new DecimalConstant (t, (decimal) v, loc);
			}

			if (t.IsEnum) {
				var real_type = EnumSpec.GetUnderlyingType (t);
				return new EnumConstant (CreateConstantFromValue (real_type, v, loc), t);
			}

			if (v == null) {
				if (t.IsNullableType)
					return Nullable.LiftedNull.Create (t, loc);

				if (TypeSpec.IsReferenceType (t))
					return new NullConstant (t, loc);
			}

#if STATIC
			throw new InternalErrorException ("Constant value `{0}' has unexpected underlying type `{1}'", v, t.GetSignatureForError ());
#else
			return null;
#endif
		}

Usage Example

示例#1
0
        protected virtual Expression DoResolveInitializer(ResolveContext rc)
        {
            if (in_transit)
            {
                field.Compiler.Report.Error(110, expr.Location,
                                            "The evaluation of the constant value for `{0}' involves a circular definition",
                                            GetSignatureForError());

                expr = null;
            }
            else
            {
                in_transit = true;
                expr       = expr.Resolve(rc);
            }

            in_transit = false;

            if (expr != null)
            {
                Constant c = expr as Constant;
                if (c != null)
                {
                    c = field.ConvertInitializer(rc, c);
                }

                if (c == null)
                {
                    if (TypeSpec.IsReferenceType(field.MemberType))
                    {
                        Error_ConstantCanBeInitializedWithNullOnly(rc, field.MemberType, expr.Location, GetSignatureForError());
                    }
                    else if (!(expr is Constant))
                    {
                        Error_ExpressionMustBeConstant(rc, expr.Location, GetSignatureForError());
                    }
                    else
                    {
                        expr.Error_ValueCannotBeConverted(rc, field.MemberType, false);
                    }
                }

                expr = c;
            }

            if (expr == null)
            {
                expr = New.Constantify(field.MemberType, Location);
                if (expr == null)
                {
                    expr = Constant.CreateConstantFromValue(field.MemberType, null, Location);
                }
                expr = expr.Resolve(rc);
            }

            return(expr);
        }