// Returns the name of a type. public string GetNameType(MType mType) { string name; if (mType.Type == MType.MTType.System) { switch (mType.FullName) { case "System.SByte": case "System.Byte": case "System.Char": case "System.Decimal": case "System.Double": case "System.Single": case "System.Int32": case "System.Int64": name = "number"; break; case "System.Boolean": name = "boolean"; break; case "System.String": name = "string"; break; case "System.Void": name = "void"; break; default: name = "any"; break; } } else { name = GetNameClass(mType); } return(name); }
// Returns the return type of a method. private string GetNameReturn(MType mType) { // Checks if is a task. if (mType.Type == MType.MTType.Task) { // Checks if there is no return. if (!mType.GenericTypes.Any()) { return("void"); } // Extracts task return. mType = mType.GenericTypes.First(); } // Checks if is a wrapped type. if (mType.Name == "TypedRpcReturn") { // Returns the wrapped type. return(GetNameType(mType.GenericTypes.First())); } else { // Returns the original type. return(GetNameType(mType)); } }
// Returns the name of a class. private string GetNameClass(MType mType) { // Declarations string name; // Checks type type. switch (mType.Type) { case MType.MTType.Array: case MType.MTType.List: name = GetNameType(mType.GenericTypes[0]) + "[]"; break; case MType.MTType.Dictionary: name = "any"; break; case MType.MTType.Task: name = mType.GenericTypes.Any() ? GetNameType(mType.GenericTypes[0]) : "void"; break; // Custom class. default: name = string.Empty; if (mType.GenericTypes.Length > 0) { name = string.Join(", ", mType.GenericTypes.Select(gt => GetNameType(gt))); name = string.Format("<{0}>", name); } name = mType.Name + name; break; } return(name); }
// Builds a model type. private MType BuildMTypeRef(EnvDTE.CodeTypeRef codeTypeRef) { // Declarations MType mType; // Checks if property is generic. if (codeTypeRef.TypeKind == EnvDTE.vsCMTypeRef.vsCMTypeRefOther) { // Builds generic type. mType = new MType(); mType.Name = codeTypeRef.AsString; mType.FullName = codeTypeRef.AsFullName; mType.Type = MType.MTType.Generic; } else { mType = BuildMType(codeTypeRef.CodeType); } return(mType); }
// Builds a model type. private MType BuildMType(EnvDTE.CodeType codeType) { // Declarations MType mType; // Validates type. if (codeType == null) { return(TypeVoid); } // New type. mType = new MType(); mType.Name = codeType.Name; mType.FullName = codeType.FullName; if (mType.FullName.EndsWith("?")) { mType.FullName = mType.FullName.Substring(0, mType.FullName.Length - 1); } // Checks type type. if (codeType.FullName.EndsWith("[]")) { mType.Type = MType.MTType.Array; } else if (codeType.FullName.StartsWith("System.Collections.Generic.List")) { mType.Type = MType.MTType.List; } else if (codeType.FullName.StartsWith("System.Threading.Tasks.Task")) { mType.Type = MType.MTType.Task; } else if (codeType.FullName.StartsWith("System.Collections.Generic.Dictionary")) { mType.Type = MType.MTType.Dictionary; } else if (codeType.FullName.StartsWith("Microsoft.Owin.IOwinContext")) { mType.Type = MType.MTType.OwinContext; } else if (codeType.FullName.StartsWith("System")) { mType.Type = MType.MTType.System; } else if (codeType.FullName == ("JsonRpc.JsonError")) { mType.Type = MType.MTType.Ignore; } else { mType.Type = MType.MTType.Custom; } // If array. if (mType.Type == MType.MTType.Array) { // Adds its type. string codeTypeName = codeType.FullName.Substring(0, codeType.FullName.Length - 2); mType.GenericTypes = new MType[] { BuildMTypeRef(GetCodeTypeRef(codeTypeName)) }; } // If List or Task. if (mType.Type == MType.MTType.List || mType.Type == MType.MTType.Task) { mType.GenericTypes = GetGenerics(codeType.FullName).Select(ctr => BuildMTypeRef(ctr)).ToArray(); } // If custom. if (mType.Type == MType.MTType.Custom) { // Finds its generics. mType.GenericTypes = GetGenerics(codeType.FullName).Select(ctr => BuildMTypeRef(ctr)).ToArray(); // Checks if type has not been added. if (!Interfaces.Any(it => it.Name == codeType.Name)) { // Adds interface. Interfaces.Add(codeType); } } return(mType); }
// Builds a model type. private MType BuildMType(Type type) { // Declarations MType mType; // New type. mType = new MType(); mType.Name = type.Name.Split('`').First(); mType.FullName = type.FullName ?? type.Name; mType.FullName = mType.FullName.Split('`').First(); // Checks type type. if (type.IsArray) { mType.Type = MType.MTType.Array; } else if (mType.FullName.StartsWith("System.Collections.Generic.List")) { mType.Type = MType.MTType.List; } else if (mType.FullName.StartsWith("System.Threading.Tasks.Task")) { mType.Type = MType.MTType.Task; } else if (mType.FullName.StartsWith("System.Collections.Generic.Dictionary")) { mType.Type = MType.MTType.Dictionary; } else if (mType.FullName.StartsWith("Microsoft.Owin.IOwinContext")) { mType.Type = MType.MTType.OwinContext; } else if (mType.FullName.StartsWith("System")) { mType.Type = MType.MTType.System; } else if (mType.FullName == ("JsonRpc.JsonError")) { mType.Type = MType.MTType.Ignore; } else if (type.IsGenericParameter) { mType.Type = MType.MTType.Generic; } else { mType.Type = MType.MTType.Custom; } // If array. if (mType.Type == MType.MTType.Array) { // Adds its type. mType.GenericTypes = new MType[] { BuildMType(type.GetElementType()) }; } // If List or Task. if (mType.Type == MType.MTType.List || mType.Type == MType.MTType.Task) { if (type.GenericTypeArguments.Any()) { mType.GenericTypes = new MType[] { BuildMType(type.GenericTypeArguments[0]) } } ; } // If custom. if (mType.Type == MType.MTType.Custom) { // Finds its generics. mType.GenericTypes = type.GenericTypeArguments.Select(t => BuildMType(t)).ToArray(); // Checks if type has already been added. if (!InterfacesTypes.Any(it => it.Name == type.Name)) { // Adds interface. InterfacesTypes.Add(type); } } return(mType); }