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

ExtractConstantFromValue() public static method

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

			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);
			}

			return null;
		}