IEnumerable<PropertyInfo> EnumerateProperties(Type type) { foreach (var p in type.GetProperties (flags)) { // TextEditInfo has case-conflicting properties (e.g. position and Position). Do not generate member-collision. if (Char.IsUpper (p.Name [0]) && type.GetProperty (Char.ToLower (p.Name [0]) + p.Name.Substring (1)) != null) continue; // It is not really doable or at least very easy to support // callbacks. So, ignore them so far. if (IsCallType (p.PropertyType)) continue; // FIXME: in this type, there are complicated variable between object and call (Action/Func), so I disabled all members at all. if (type.GetTypeReferenceName () == "AstWalkerDetailCallback") continue; yield return p; } }
IEnumerable<MethodInfo> EnumerateMethods(Type type) { foreach (var m in type.GetMethods (flags)) { // It is not really doable or at least very easy to support // callbacks. So, ignore them so far. if (IsCallType (m.ReturnType) || m.GetParameters ().Select (p => p.ParameterType).Any (t => IsCallType (t))) continue; // FIXME: in this type, there are complicated variable between object and call (Action/Func), so I disabled all members at all. if (type.GetTypeReferenceName () == "AstWalkerDetailCallback") continue; yield return m; } }
string GetImplementedType(Type type, bool addImpl = false) { if (type == typeof (void)) return "void"; if (type == typeof (string)) return "string"; if (type == typeof (bool)) return "bool"; if (type == typeof (double)) return "double"; if (type.IsArray) return "TypeScriptArray<" + GetImplementedType (type.GetElementType ()) + ">"; if (type.IsGenericParameter) return type.Name; switch (type.GetTypeReferenceName ()) { case "any": return "object"; } // FIXME: we cannot support callback yet. So, just return object if (IsCallType (type)) return "object"; string suffix = type.IsInterface && addImpl ? "_Impl" : string.Empty; bool isSystem = (type.Namespace ?? "").StartsWith ("System"); if (type.IsGenericType) return (isSystem ? "" : "TypeScriptServiceBridge.") + type.Namespace + "." + type.GetTypeReferenceName (suffix); if (!isSystem) return "TypeScriptServiceBridge." + type.FullName + suffix; return type.FullName + suffix; }
void GenerateInterface(Type type) { output.WriteLine ("\tpublic interface {0} : ITypeScriptObject", type.GetTypeReferenceName ()); GenerateImplements (type); output.WriteLine ("\t{"); foreach (var p in EnumerateProperties (type)) { GenerateFieldSignature (type, p, null); output.WriteLine (" { get; set; }"); } foreach (var m in EnumerateMethods (type)) { GenerateMethodSignature (type, m, null); output.WriteLine (";"); } output.WriteLine ("\t}"); output.WriteLine ("\tpublic class {0} : TypeScriptObject, {1}", type.GetTypeReferenceName ("_Impl"), type.GetTypeReferenceName ()); output.WriteLine ("\t{"); output.WriteLine ("\t\tpublic {0}_Impl (ObjectInstance instance) : base (instance) {{}}", type.GetPrimaryName ()); ImplementProperties (type); ImplementMethods (type); output.WriteLine ("\t}"); }
void GenerateEnum(Type type) { output.WriteLine ("\tpublic enum " + type.GetTypeReferenceName ()); output.WriteLine ("\t{"); foreach (var n in Enum.GetNames (type)) output.WriteLine ("\t\t{0} = {1},", n, ((IConvertible) Enum.Parse (type, n)).ToInt32 (null)); output.WriteLine ("\t}"); }
void GenerateClass(Type type) { output.Write ("\tpublic class " + type.GetTypeReferenceName () + " : "); string baseType = GetBaseTypeFromAttribute (type); if (baseType == null) output.WriteLine ("TypeScriptObject"); else output.WriteLine (baseType); GenerateImplements (type); output.WriteLine ("\t{"); output.WriteLine ("\t\tpublic {0} (ObjectInstance instance) : base (instance) {{}}", type.GetPrimaryName ()); foreach (var c in type.GetConstructors (flags)) { if (!c.IsPublic) continue; // FIXME: they are workarounds for conflict betweeen // ctor(Object regex) and ctor(object instance)... if (type.GetTypeReferenceName () == "RegexLiteral" || type.GetTypeReferenceName () == "RegularExpressionLiteralToken") continue; output.WriteLine ("\t\tpublic {0} ({1})", type.GetPrimaryName (), GetArgumentsSignature (c)); var args = GetMarshaledCallArguments (c); output.WriteLine ("\t\t\t : base (CallConstructor (\"{0}\", \"{1}\"{2}))", type.Namespace, type.GetTypeReferenceName (), args); output.WriteLine ("\t\t{"); output.WriteLine ("\t\t}"); } ImplementProperties (type); ImplementMethods (type); ImplementInterfaces (type); output.WriteLine ("\t}"); }