public static string Encode(object host, bool persistent) { Type type; if (host is MethodInfo) { MethodInfo methodInfo = (MethodInfo)host; if (methodInfo.IsStatic) { return(methodInfo.ReflectedType.FullName + "." + methodInfo.Name); } else { return(methodInfo.Name); } } else if (host is Type) { type = (Type)host; return(string.Format("typeof({0})", type.FullName)); } else { type = host.GetType(); } if (type.IsEnum) //处理enum常量 { return(HostOperation.EnumBitFlags(host)); } if (host is DateTime) { return(string.Format("new {0}({1})", typeof(DateTime).FullName, ((DateTime)host).Ticks)); } VAL val = HostValization.Host2Valor(host); if (persistent) { return(val.Valor); } else { //default contructor if (HostCoding.HasContructor(type, new Type[] {})) { return(string.Format("new {0}()", type.FullName)); //有缺省的constructor } if (type.FullName == host.ToString()) { return(string.Format("new {0}(...)", type.FullName)); } else { return(string.Format("new {0}({1})", type.FullName, host)); } } }
bool NewInstance(VAL V) //new user defined class, .net object or a listcon { VALL L = SysFuncBeginParameter(); if (V.ty == VALTYPE.funccon) { string func = V.Str; VAL userClass = GetVAL(func, true); if (userClass.ty == VALTYPE.classcon) { //VAL instance = NewVAL.UserType(func); VAL instance = new VAL(); VAL ret = InternalUserFuncCall(userClass, instance, new VAL(L)); return(SysFuncEnd(ret)); } else { VAL args = new VAL(L); VAL scope = new VAL(); if (!ES.IsEmpty()) { scope = ES.Top(); } VAL Clss = HostCoding.Decode(func, args, scope, context); return(SysFuncEnd(Clss)); } } else if (V.value is Type) //generic class { VAL args = new VAL(L); object instance = Activator.CreateInstance((Type)V.value, HostCoding.ConstructorArguments(args)); return(SysFuncEnd(VAL.NewHostType(instance))); } throw new RuntimeException(position, "new instance {0} failed", V); }
public static int HostCompareTo(Operator opr, VAL v1, VAL v2) { if (v1.ty != VALTYPE.hostcon || v2.ty != VALTYPE.hostcon) { throw new HostTypeException("cannot compare different type value {0} and {1}.", v1, v2); } object x1 = v1.value; object x2 = v2.value; if (x1 == null && x2 == null) { return(0); } if (x1 != null && x2 == null) { if (x1.GetType().IsValueType) { throw new HostTypeException("cannot compare value type {0} to null.", x1); } else { return(1); } } else if (x1 == null && x2 != null) { if (x2.GetType().IsValueType) { throw new HostTypeException("cannot compare value type {0} to null.", x2); } else { return(-1); } } if (x1 is Type && x2 is Type) { if ((Type)x1 == (Type)x2) { return(0); } else { return(-1); } } else if (!(x1 is Type) && (x2 is Type) || (x1 is Type) && !(x2 is Type)) { throw new HostTypeException("cannot compare type to non-type: {0} and {1}.", x1, x2); } Type type1 = x1.GetType(); Type type2 = x2.GetType(); if (type1.IsValueType && type2.IsValueType) { if (System.ValueType.Equals(x1, x2)) { return(0); } } Type[] I = type1.GetInterfaces(); if (I.Length != 0) { foreach (Type i in I) { if (i == typeof(IComparable)) { return((x1 as IComparable).CompareTo(x2)); } } } I = type2.GetInterfaces(); if (I.Length != 0) { foreach (Type i in I) { if (i == typeof(IComparable)) { return((x2 as IComparable).CompareTo(x1)); } } } //operator overloading >, >=, <, <=, !=, == VAL comp = HostFunction.OperatorOverloading(opr, v1, v2, true); if ((object)comp != null) { switch (opr) { case Operator.op_LessThan: return(comp.Boolcon? -1: 10); case Operator.op_Equality: return(comp.Boolcon ? 0 : 10); case Operator.op_GreaterThan: return(comp.Boolcon ? 1 : -10); } } Type type = HostCoding.CommonBaseClass(new object[] { x1, x2 }); if (type != null) { if (x1 == x2) { return(0); } } throw new HostTypeException("cannot compare value {0} and {1} without implement IComparable.", x1, x2); }