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)); } } }
private static string ToJson(VAL val, string tag, int tab, bool quotationMark) { StringWriter o = new StringWriter(); o.Write(Indent(tab)); if (tag != "") { if (quotationMark) { o.Write("\"" + tag + "\""); } else { o.Write(tag); } o.Write(" : "); } if (val.IsAssociativeArray()) { o.WriteLine("{"); for (int i = 0; i < val.Size; i++) { VAL v = val[i]; o.Write(ToJson(v[1], v[0].Str, tab + 1, quotationMark)); if (i < val.Size - 1) { o.WriteLine(","); } else { o.WriteLine(); } } o.Write(Indent(tab)); o.Write("}"); if (!quotationMark && val.Class != null) { o.Write(".typeof(\"{0}\")", val.Class); } } else if (val.ty == VALTYPE.listcon) { o.WriteLine("["); for (int j = 0; j < val.Size; j++) { VAL a = val[j]; o.Write(ToJson(a, "", tab + 1, quotationMark)); if (j < val.Size - 1) { o.WriteLine(","); } else { o.WriteLine(); } } o.Write(Indent(tab)); o.Write("]"); if (!quotationMark && val.Class != null) { o.Write(".typeof(\"{0}\")", val.Class); } } else if (val.ty == VALTYPE.hostcon) { val = HostValization.Host2Valor(val.value); if (val.ty == VALTYPE.listcon) { o.Write(ToJson(val, "", tab, quotationMark)); } else { o.Write(val.Valor); } } else { o.Write(val.Valor); } return(o.ToString()); }