static string _ReplacePlus(string name, CsNameOption opt) { if (opt == CsNameOption.BridgeTypeToString) { return(name); } else { return(name.Replace('+', '.')); } }
public static string CsFullName(this Type type, CsNameOption opt = CsNameOption.Compilable) { string fn = CsFullName_Impl(type, opt); if (opt == CsNameOption.BridgeTypeToString) { return(fn); } else { return(shortenName(fn)); } }
static string typefn(Type tType, string eraseNs, CsNameOption opt = CsNameOption.Compilable) { string fn = JSNameMgr.CsFullName(tType, opt); if (eraseNs == "no-namespace") { int dot = fn.LastIndexOf('.'); if (dot >= 0) { fn = fn.Substring(dot + 1); } } else if (!string.IsNullOrEmpty(eraseNs) && fn.StartsWith(eraseNs + ".")) { fn = fn.Substring(eraseNs.Length + 1); } return(fn); }
// 当有一个类 TestGeneric<T>,有可能需要产生下面几种名字 // 名字形式 使用环境 // TestGeneric<> typeof(TestGeneric<>) 可编译 // TestGeneric<T> public class TestGeneric<T> // TestGeneric<GameObject> 具体类型时 // TestGeneric`1 bridge使用 static string CsFullName_Impl(Type type, CsNameOption opt) { if (type == null) { return(""); } bool with_t = (opt == CsNameOption.CompilableWithT); bool bridge = (opt == CsNameOption.BridgeTypeToString); if (type.IsByRef) { type = type.GetElementType(); } bool isgp = type.IsGenericParameter; bool cgt = type.ContainsGenericParameters; bool gt = type.IsGenericType; bool gtd = type.IsGenericTypeDefinition; if (isgp) { // T return(_TName(type, opt)); } else if (!cgt && !gt && !gtd) { string rt = type.FullName; return(_ReplacePlus(rt, opt)); } else if (gt || gtd) { string N = string.Empty; string PN = ""; // if (gtd) // { // N = type.FullName; // } // else { if (type.IsNested && type.DeclaringType != null) { PN = CsFullName_Impl(type.DeclaringType, opt); PN += bridge ? "+" : "."; } else if (!string.IsNullOrEmpty(type.Namespace)) { PN = type.Namespace; PN += "."; } N = type.Name; } Type[] Ts = type.GetGenericArguments(); int iOft = N.IndexOf("`" + Ts.Length); if (bridge && gtd) { if (iOft >= 0) { N = N.Substring(0, iOft); } N += "`" + Ts.Length; return(_ReplacePlus(N, opt)); } if (gtd && !with_t) { for (var i = 0; i < GenTSuffix.Length; i++) { N = N.Replace(GenTSuffix[i], GenTSuffixReplaceCS[i]); } } else { if (iOft >= 0) { N = N.Substring(0, iOft); } Ts = EraseTDefinedByParent(type, Ts); if (Ts.Length > 0) { N += bridge ? "[" : "<"; for (int i = 0; i < Ts.Length; i++) { if (bridge) { N += "["; } N += CsFullName_Impl(Ts[i], opt); if (bridge) { N += "]"; } if (i != Ts.Length - 1) { N += ","; } } N += bridge ? "]" : ">"; } } return(_ReplacePlus(PN + N, opt)); } else // contains generic parameter { if (!type.IsArray) { throw new Exception("CsFullName_Impl - Unknown type - " + type.ToString()); } string af = "["; for (int d = 1; d < type.GetArrayRank(); d++) { af += ","; } af += "]"; return(CsFullName_Impl(type.GetElementType(), opt) + af); } }
static string _TName(Type T, CsNameOption opt) { return((opt == CsNameOption.BridgeTypeToString) ? "``" + T.GenericParameterPosition : T.Name); }