/// <summary>Generates the information for a quick look into the type</summary> /// <param name="type">The type reference to look into</param> /// <returns>Returns a quick look at the type information</returns> public static QuickTypeInfo GenerateInfo(TypeReference type) { // Variables QuickTypeInfo info = new QuickTypeInfo(); string[] generics = TypeInfo.GetGenericParametersString( type.GenericParameters.ToArray() ); GetNames( type.FullName.Replace("&", ""), GetNamespace(type), generics, out info.unlocalizedName, out info.nonInstancedFullName, out info.fullName, out info.namespaceName, out info.name ); info.genericParameters = GenericParametersInfo.GenerateInfoArray(type.GenericParameters); if(info.genericParameters.Length == 0) { info.genericParameters = GetGenericParameters(type.FullName); } info.isGenericType = type.IsGenericParameter && (info.unlocalizedName == info.nonInstancedFullName); return info; }
/// <summary>Generates an event information from the given event definition</summary> /// <param name="ev">The event definition to gather information from</param> /// <returns>Returns the event information generated</returns> public static EventInfo GenerateInfo(EventDefinition ev) { // Variables EventInfo info = new EventInfo(); info.name = ev.Name; info.typeInfo = QuickTypeInfo.GenerateInfo(ev.EventType); info.implementedType = QuickTypeInfo.GenerateInfo(ev.DeclaringType); info.adder = MethodInfo.GenerateInfo(ev.AddMethod); info.remover = MethodInfo.GenerateInfo(ev.RemoveMethod); info.accessor = info.adder.accessor; info.modifier = info.adder.modifier; info.isStatic = info.adder.isStatic; info.attributes = AttributeInfo.GenerateInfoArray(ev.CustomAttributes); info.fullDeclaration = ( info.accessor + " " + (info.modifier != "" ? info.modifier + " " : "") + info.typeInfo.name + " " + info.name ); if (TypeInfo.ignorePrivate && PropertyInfo.GetAccessorId(info.accessor) == 0) { info.shouldDelete = true; } return(info); }
/// <summary>Generates the information for an attribute from the given Mono.Cecil custom attribute class</summary> /// <param name="attr">The attribute to gather the information from</param> /// <returns>Returns the attribute information generated from the custom attribute</returns> public static AttributeInfo GenerateInfo(CustomAttribute attr) { // Variables AttributeInfo info = new AttributeInfo(); int i = 0; info.typeInfo = QuickTypeInfo.GenerateInfo(attr.AttributeType); info.constructorArgs = new AttributeFieldInfo[attr.ConstructorArguments.Count]; foreach (CustomAttributeArgument arg in attr.ConstructorArguments) { info.constructorArgs[i] = new AttributeFieldInfo(); info.constructorArgs[i].typeInfo = QuickTypeInfo.GenerateInfo(arg.Type); info.constructorArgs[i].name = attr.Constructor.Parameters[i].Name; info.constructorArgs[i].value = (info.constructorArgs[i].typeInfo.name != "bool" ? $"{ arg.Value }" : $"{ arg.Value }".ToLower() ); i++; } i = 0; info.properties = new AttributeFieldInfo[ attr.Fields.Count + attr.Properties.Count ]; foreach (CustomAttributeNamedArgument field in attr.Fields) { info.properties[i] = new AttributeFieldInfo(); info.properties[i].typeInfo = QuickTypeInfo.GenerateInfo(field.Argument.Type); info.properties[i].name = field.Name; info.properties[i].value = (info.properties[i].typeInfo.name != "bool" ? $"{ field.Argument.Value }" : $"{ field.Argument.Value }".ToLower() ); i++; } foreach (CustomAttributeNamedArgument property in attr.Properties) { info.properties[i] = new AttributeFieldInfo(); info.properties[i].typeInfo = QuickTypeInfo.GenerateInfo(property.Argument.Type); info.properties[i].name = property.Name; info.properties[i].value = (info.properties[i].typeInfo.name != "bool" ? $"{ property.Argument.Value }" : $"{ property.Argument.Value }".ToLower() ); i++; } info.parameterDeclaration = string.Join(", ", GetParameterDeclaration(info)); info.fullDeclaration = ( $"[{ info.typeInfo.fullName }" + (info.parameterDeclaration != "" ? $"({ info.parameterDeclaration })" : "") + "]" ); return(info); }
/// <summary>Generates a generic parameter information of the given generic parameter</summary> /// <param name="generic">The generic parameter to look into</param> /// <returns>Returns the generic parameter information</returns> public static GenericParametersInfo GenerateInfo(GenericParameter generic) { // Variables GenericParametersInfo info = new GenericParametersInfo(); int i = 0; info.unlocalizedName = UnlocalizeName(generic.Name); info.name = QuickTypeInfo.MakeNameFriendly(generic.Name); info.constraints = new QuickTypeInfo[generic.Constraints.Count]; foreach (GenericParameterConstraint constraint in generic.Constraints) { info.constraints[i++] = QuickTypeInfo.GenerateInfo(constraint.ConstraintType); } return(info); }
/// <summary>Generates the information for the parameter given the parameter definition</summary> /// <param name="parameter">The parameter definition to look into</param> /// <returns>Returns the parameter information generated from the parameter definition</returns> public static ParameterInfo GenerateInfo(ParameterDefinition parameter) { // Variables ParameterInfo info = new ParameterInfo(); info.name = parameter.Name; info.typeInfo = QuickTypeInfo.GenerateInfo(parameter.ParameterType); info.attributes = AttributeInfo.GenerateInfoArray(parameter.CustomAttributes); if (parameter.IsIn) { info.modifier = "in"; } else if (parameter.IsOut) { info.modifier = "out"; } else if (parameter.ParameterType.IsByReference) { info.modifier = "ref"; } else if (HasParamsAttribute(info.attributes)) { info.modifier = "params"; } else { info.modifier = ""; } info.isOptional = parameter.IsOptional; info.defaultValue = $"{ parameter.Constant }"; info.genericParameterDeclarations = QuickTypeInfo.GetGenericParametersAsStrings(parameter.ParameterType.FullName); info.fullDeclaration = GetFullDeclaration(info); return(info); }
/// <summary>Generates the property information from the given property definition</summary> /// <param name="property">The property information to gather information from</param> /// <returns>Returns the property information that's generated</returns> public static PropertyInfo GenerateInfo(PropertyDefinition property) { // Variables PropertyInfo info = new PropertyInfo(); info.hasGetter = (property.GetMethod != null); info.hasSetter = (property.SetMethod != null); info.getter = (info.hasGetter ? (isGeneric != -1 ? MethodInfo.GetGenericMethodInfo(_type, _currType, _currTypeRef, property.GetMethod) : MethodInfo.GenerateInfo(property.GetMethod) ) : null ); info.setter = (info.hasSetter ? (isGeneric != -1 ? MethodInfo.GetGenericMethodInfo(_type, _currType, _currTypeRef, property.SetMethod) : MethodInfo.GenerateInfo(property.SetMethod) ) : null ); if (info.getter != null && GetAccessorId(info.getter.accessor) == 0) { info.getter = null; info.hasGetter = false; } if (info.setter != null && GetAccessorId(info.setter.accessor) == 0) { info.setter = null; info.hasSetter = false; } if (!info.hasGetter && !info.hasSetter) { info.shouldDelete = true; return(info); } info.name = property.Name; info.partialFullName = property.FullName.Split("::")[1].Replace(",", ", "); info.isStatic = !property.HasThis; info.attributes = AttributeInfo.GenerateInfoArray(property.CustomAttributes); info.parameters = ParameterInfo.GenerateInfoArray(property.Parameters); info.accessor = GetAccessor(info.getter, info.setter); if (isGeneric == 1) { if (info.hasGetter) { info.typeInfo = info.getter.returnType; info.parameters = info.getter.parameters; } else { info.typeInfo = info.setter.parameters[info.setter.parameters.Length - 1].typeInfo; System.Array.Copy( info.setter.parameters, info.parameters, info.setter.parameters.Length - 1 ); } } else { info.typeInfo = QuickTypeInfo.GenerateInfo(property.PropertyType); } if (!property.HasThis) { info.modifier = "static"; } else { info.modifier = GetModifier(info.getter, info.setter); } info.implementedType = QuickTypeInfo.GenerateInfo(property.DeclaringType); info.getSetDeclaration = GetGetSetDeclaration(info.getter, info.setter, info.accessor); info.declaration = ( info.accessor + " " + (info.modifier != "" ? info.modifier + " " : "") + info.typeInfo.name + " " + (info.parameters.Length == 0 ? info.name : "this") ); info.parameterDeclaration = string.Join(", ", GetParameterDeclarations(info)); info.fullDeclaration = ( info.declaration + (info.parameterDeclaration != "" ? $"[{ info.parameterDeclaration }]" : "") + $" {{ { info.getSetDeclaration } }}" ); isGeneric = -1; return(info); }
/// <summary>Generates the method information from the given method definition</summary> /// <param name="method">The method definition to look into</param> /// <returns>Returns the method information</returns> public static MethodInfo GenerateInfo(MethodDefinition method) { // Variables MethodInfo info = new MethodInfo(); int index; info.isStatic = method.IsStatic; info.isVirtual = method.IsVirtual; info.isConstructor = method.IsConstructor; if (method.IsAssembly) { info.accessor = "internal"; } else if (method.IsFamily) { info.accessor = "protected"; } else if (method.IsPublic) { info.accessor = "public"; } else { info.accessor = "private"; } info.isProperty = method.IsGetter || method.IsSetter; info.isEvent = method.IsAddOn || method.IsRemoveOn; info.isOperator = method.Name.StartsWith("op_"); info.isConversionOperator = ( method.Name == "op_Explicit" || method.Name == "op_Implicit" ); info.implementedType = QuickTypeInfo.GenerateInfo(method.DeclaringType); info.returnType = QuickTypeInfo.GenerateInfo(method.ReturnType); if (info.isConstructor) { info.name = info.implementedType.name; index = info.name.IndexOf('<'); if (index != -1) { info.name = info.name.Substring(0, index); } } else if (info.isConversionOperator) { info.name = method.Name + "__" + info.returnType.name; } else if (info.isOperator) { info.name = method.Name.Substring(3); } else { info.name = method.Name; } info.partialFullName = method.FullName.Split("::")[1].Replace(",", ", "); if (info.isOperator) { info.partialFullName = info.name; } info.parameters = ParameterInfo.GenerateInfoArray(method.Parameters); info.genericParameters = GenericParametersInfo.GenerateInfoArray(method.GenericParameters); info.attributes = AttributeInfo.GenerateInfoArray(method.CustomAttributes); if (info.isConversionOperator) { info.modifier = $"static { method.Name.Substring(3).ToLower() } operator"; } else if (info.isOperator) { info.modifier = "static operator"; } else if (method.IsStatic) { info.modifier = "static"; } else if (method.IsAbstract) { info.modifier = "abstract"; } else if (method.IsVirtual && method.IsReuseSlot) { info.modifier = "override"; } else if (method.IsVirtual) { info.modifier = "virtual"; } else { info.modifier = ""; } info.isExtension = HasExtensionAttribute(info); info.isAbstract = method.IsAbstract; info.isOverriden = method.IsReuseSlot; info.declaration = ( info.accessor + " " + (info.modifier != "" ? info.modifier + " " : "") + (!info.isConstructor && !info.isConversionOperator ? info.returnType.name + " " : "") + (!info.isConversionOperator ? info.name : info.returnType.name) ); info.genericDeclaration = (info.genericParameters.Length > 0 && !method.IsGenericInstance ? $"<{ string.Join(',', GetGenericParameterDeclaration(info.genericParameters)) }>" : "" ); info.parameterDeclaration = string.Join(", ", GetParameterDeclaration(info)); if (info.isExtension) { info.parameterDeclaration = $"this { info.parameterDeclaration }"; } info.fullDeclaration = $"{ info.declaration }{ info.genericDeclaration }({ info.parameterDeclaration })"; info.fullDeclaration += TypeInfo.GetGenericParameterConstraints(info.genericParameters); if (TypeInfo.ignorePrivate && PropertyInfo.GetAccessorId(info.accessor) == 0) { info.shouldDelete = true; } return(info); }
/// <summary>Gets the generic instanced type information</summary> /// <param name="hash">The hashtable to change the generics into the instanced generics</param> /// <param name="info">The information of the type to look into, should be the original</param> /// <param name="ninfo">The information of the type to look into, should be generic instanced</param> /// <param name="isGeneric">Set to true if anything was changed by this method</param> /// <returns>Returns a quick look into the typing</returns> public static QuickTypeInfo GetGenericInstanceTypeInfo( Dictionary <string, QuickTypeInfo> hash, QuickTypeInfo info, QuickTypeInfo ninfo, out bool isGeneric ) { isGeneric = false; foreach (string key in hash.Keys) { // Variables string arrayPattern = $@"{ key }((?:\[,*\])+)"; if (info.unlocalizedName == key) { info.unlocalizedName = hash[key].unlocalizedName; } if (info.unlocalizedName.Contains('.')) { info.namespaceName = Regex.Replace(info.unlocalizedName, @"(.*)\..*$", "$1"); } else { info.namespaceName = ""; } if (info.fullName == key) { info.fullName = hash[key].fullName; isGeneric = true; } else if (info.fullName.Contains(key)) { // Variables string genericPattern = $@"([<,]){ key }([>,])"; info.fullName = Regex.Replace( info.fullName, genericPattern, $"$1{ hash[key].fullName }$2" ); info.fullName = Regex.Replace( info.fullName, arrayPattern, $"{ hash[key].fullName }$1" ); isGeneric = true; } } if (isGeneric) { info.name = QuickTypeInfo.DeleteNamespaceFromType( QuickTypeInfo.MakeNameFriendly(info.fullName) ); } if (info.fullName == info.name) { // Variables string arrayPattern = $@"[a-zA-Z0-9]+((?:\[,*\])+)"; info.fullName = Regex.Replace( info.fullName, arrayPattern, $"{ info.unlocalizedName }$1" ); } info.isGenericType = isGeneric && (info.unlocalizedName == info.nonInstancedFullName); return(info); }
/// <summary>Generates a generic instance of the method</summary> /// <param name="method">The method to look into</param> /// <param name="methodRef">The method reference to look into</param> /// <returns>Returns the information into the method</returns> public static MethodInfo GenerateInfo(MethodDefinition method, MethodReference methodRef) { // Variables QuickTypeInfo ninfo = QuickTypeInfo.GenerateInfo(methodRef.DeclaringType); MethodInfo info = GenerateInfo(method); Dictionary <string, QuickTypeInfo> hash = new Dictionary <string, QuickTypeInfo>(); bool isGeneric = false; int i = 0; foreach (GenericParametersInfo generic in info.implementedType.genericParameters) { // Variables QuickTypeInfo temp = new QuickTypeInfo(); temp.unlocalizedName = ninfo.genericParameters[i].unlocalizedName; temp.fullName = ninfo.genericParameters[i].name; temp.name = QuickTypeInfo.DeleteNamespaceFromType(QuickTypeInfo.MakeNameFriendly(temp.fullName)); if (temp.unlocalizedName.Contains('.')) { temp.namespaceName = Regex.Replace(temp.unlocalizedName, @"(.*)\..*$", "$1"); } else { temp.namespaceName = ""; } hash.Add(generic.name, temp); i++; } foreach (ParameterInfo parameter in info.parameters) { parameter.typeInfo = GetGenericInstanceTypeInfo(hash, parameter.typeInfo, ninfo, out isGeneric); if (isGeneric) { parameter.typeInfo.genericParameters = GenericParametersInfo.GenerateInfoArray(methodRef.Resolve().DeclaringType.GenericParameters); parameter.genericParameterDeclarations = QuickTypeInfo.GetGenericParametersAsStrings(parameter.typeInfo.fullName); parameter.fullDeclaration = ParameterInfo.GetFullDeclaration(parameter); } } info.returnType = GetGenericInstanceTypeInfo( hash, info.returnType, QuickTypeInfo.GenerateInfo(methodRef.ReturnType), out isGeneric ); info.declaration = ( info.accessor + " " + (info.modifier != "" ? info.modifier + " " : "") + (!info.isConstructor && !info.isConversionOperator ? info.returnType.name + " " : "") + (!info.isConversionOperator ? info.name : info.returnType.name) ); info.genericDeclaration = (info.genericParameters.Length > 0 && !method.IsGenericInstance ? $"<{ string.Join(',', GetGenericParameterDeclaration(info.genericParameters)) }>" : "" ); info.parameterDeclaration = string.Join(", ", GetParameterDeclaration(info)); if (info.isExtension) { info.parameterDeclaration = $"this { info.parameterDeclaration }"; } info.fullDeclaration = $"{ info.declaration }{ info.genericDeclaration }({ info.parameterDeclaration })"; info.fullDeclaration += TypeInfo.GetGenericParameterConstraints(info.genericParameters); return(info); }
/// <summary>Generates the information for the field from the field definition</summary> /// <param name="field">The field definition to look into</param> /// <returns>Returns the information of the field</returns> public static FieldInfo GenerateInfo(FieldDefinition field) { // Variables FieldInfo info = new FieldInfo(); string val = System.Text.ASCIIEncoding.ASCII.GetString(field.InitialValue); if (field.IsAssembly) { info.accessor = "internal"; } else if (field.IsFamily) { info.accessor = "protected"; } else if (field.IsPrivate) { info.accessor = "private"; } else { info.accessor = "public"; } if (TypeInfo.ignorePrivate && PropertyInfo.GetAccessorId(info.accessor) == 0) { info.shouldDelete = true; return(info); } info.name = field.Name; info.typeInfo = QuickTypeInfo.GenerateInfo(field.FieldType); info.implementedType = QuickTypeInfo.GenerateInfo(field.DeclaringType); info.value = $"{ field.Constant ?? val }"; info.isConstant = field.HasConstant; info.isStatic = field.IsStatic; info.isReadonly = field.IsInitOnly; info.attributes = AttributeInfo.GenerateInfoArray(field.CustomAttributes); if (field.HasConstant) { info.modifier = "const"; } else if (field.IsStatic && field.IsInitOnly) { info.modifier = "static readonly"; } else if (field.IsStatic) { info.modifier = "static"; } else if (field.IsInitOnly) { info.modifier = "readonly"; } else { info.modifier = ""; } info.fullDeclaration = ( $"{ info.accessor } " + (info.modifier != "" ? info.modifier + " " : "") + $"{ info.typeInfo.name } " + info.name ); if (info.isConstant) { info.fullDeclaration += $" = { info.value }"; } return(info); }