public static StackObject *EnumHasFlag(ILIntepreter intp, StackObject *esp, IList <object> mStack, CLRMethod method, bool isNewObj) { var ret = esp - 1 - 1; AppDomain domain = intp.AppDomain; var p = esp - 1; object val = StackObject.ToObject(p, domain, mStack); intp.Free(p); p = esp - 1 - 1; object ins = StackObject.ToObject(p, domain, mStack); intp.Free(p); bool res = false; if (ins is ILEnumTypeInstance) { ILEnumTypeInstance enumIns = (ILEnumTypeInstance)ins; int num = enumIns.Fields[0].Value; int valNum = ((ILEnumTypeInstance)val).Fields[0].Value; res = (num & valNum) == valNum; } else { res = ((Enum)ins).HasFlag((Enum)val); } if (res) return(ILIntepreter.PushOne(ret)); }
public static StackObject *EnumGetName(ILIntepreter intp, StackObject *esp, IList <object> mStack, CLRMethod method, bool isNewObj) { var ret = esp - 1 - 1; AppDomain domain = intp.AppDomain; var p = esp - 1; object val = StackObject.ToObject(p, domain, mStack); intp.Free(p); p = esp - 1 - 1; Type t = (Type)StackObject.ToObject(p, domain, mStack); intp.Free(p); if (t is ILRuntimeType) { ILType it = ((ILRuntimeType)t).ILType; List <string> res = new List <string>(); if (it.IsEnum) { if (val is ILEnumTypeInstance) { ILEnumTypeInstance ins = (ILEnumTypeInstance)val; return(ILIntepreter.PushObject(ret, mStack, ins.ToString(), true)); } else if (val.GetType().IsPrimitive) { ILEnumTypeInstance ins = new ILEnumTypeInstance(it); ins[0] = val; return(ILIntepreter.PushObject(ret, mStack, ins.ToString(), true)); } else { throw new NotImplementedException(); } } else { throw new Exception(string.Format("{0} is not Enum", t.FullName)); } } else if (t is ILRuntimeWrapperType) { return(ILIntepreter.PushObject(ret, mStack, Enum.GetName(((ILRuntimeWrapperType)t).RealType, val), true)); } else { return(ILIntepreter.PushObject(ret, mStack, Enum.GetName(t, val), true)); } }
public static StackObject *EnumToObject(ILIntepreter intp, StackObject *esp, IList <object> mStack, CLRMethod method, bool isNewObj) { var ret = esp - 1 - 1; AppDomain domain = intp.AppDomain; var p = esp - 1; int val = p->Value; intp.Free(p); p = esp - 1 - 1; Type t = (Type)StackObject.ToObject(p, domain, mStack); intp.Free(p); if (t is CSHotFixType) { ILType it = ((CSHotFixType)t).ILType; List <string> res = new List <string>(); if (it.IsEnum) { ILEnumTypeInstance ins = new ILEnumTypeInstance(it); ins[0] = val; return(ILIntepreter.PushObject(ret, mStack, ins, true)); } else { throw new Exception(string.Format("{0} is not Enum", t.FullName)); } } else if (t is CSHotFixWrapperType) { return(ILIntepreter.PushObject(ret, mStack, Enum.GetName(((CSHotFixWrapperType)t).RealType, val), true)); } else { return(ILIntepreter.PushObject(ret, mStack, Enum.GetName(t, val), true)); } }
/*public unsafe static object ObjectGetType(ILContext ctx, object instance, object[] param, IType[] genericArguments) * { * var type = instance.GetType(); * if (type == typeof(ILTypeInstance)) * { * return ((ILTypeInstance)instance).Type.ReflectionType; * } * else * return type; * }*/ public static StackObject *EnumParse(ILIntepreter intp, StackObject *esp, IList <object> mStack, CLRMethod method, bool isNewObj) { var ret = esp - 1 - 1; AppDomain domain = intp.AppDomain; var p = esp - 1; string name = (string)StackObject.ToObject(p, domain, mStack); intp.Free(p); p = esp - 1 - 1; Type t = (Type)StackObject.ToObject(p, domain, mStack); intp.Free(p); if (t is ILRuntimeType) { ILType it = ((ILRuntimeType)t).ILType; if (it.IsEnum) { var fields = it.TypeDefinition.Fields; for (int i = 0; i < fields.Count; i++) { var f = fields[i]; if (f.IsStatic) { if (f.Name == name) { ILEnumTypeInstance ins = new ILEnumTypeInstance(it); ins[0] = f.Constant; ins.Boxed = true; return(ILIntepreter.PushObject(ret, mStack, ins, true)); } else { int val; if (int.TryParse(name, out val)) { if ((int)f.Constant == val) { ILEnumTypeInstance ins = new ILEnumTypeInstance(it); ins[0] = f.Constant; ins.Boxed = true; return(ILIntepreter.PushObject(ret, mStack, ins, true)); } } } } } return(ILIntepreter.PushNull(ret)); } else { throw new Exception(string.Format("{0} is not Enum", t.FullName)); } } else if (t is ILRuntimeWrapperType) { return(ILIntepreter.PushObject(ret, mStack, Enum.Parse(((ILRuntimeWrapperType)t).RealType, name), true)); } else { return(ILIntepreter.PushObject(ret, mStack, Enum.Parse(t, name), true)); } }