public static bool IsDeprecated(ITypeDefOrRef type) { var td = type.ResolveTypeDef(); if (td == null) { return(false); } bool foundByRefLikeMarker = false; foreach (var ca in td.CustomAttributes) { if (ca.TypeFullName != "System.ObsoleteAttribute") { continue; } if (ca.ConstructorArguments.Count != 2) { return(true); } if (!(ca.ConstructorArguments[0].Value is UTF8String s && s == ByRefLikeMarker)) { return(true); } if (!(ca.ConstructorArguments[1].Value is bool b && b)) { return(true); } foundByRefLikeMarker = true; } return(foundByRefLikeMarker && !IsByRefLike(td)); }
private void InitializeAnalyzer() { foundMethods = new ConcurrentDictionary <MethodDef, int>(); var baseMethods = TypesHierarchyHelpers.FindBaseMethods(analyzedMethod).ToArray(); if (baseMethods.Length > 0) { baseMethod = baseMethods[baseMethods.Length - 1]; } else { baseMethod = analyzedMethod; } possibleTypes = new List <ITypeDefOrRef>(); ITypeDefOrRef type = analyzedMethod.DeclaringType.BaseType; while (type != null) { possibleTypes.Add(type); var resolvedType = type.ResolveTypeDef(); type = resolvedType == null ? null : resolvedType.BaseType; } }
static void BuildMemberReferences(ConfuserContext context, ITypeDefOrRef typeDefOrRef, string boundMemberName, Instruction nameInstruction) { var service = context.Registry.GetService <INameService>(); var boundMemberTypeDef = typeDefOrRef.ResolveTypeDef(); if (boundMemberTypeDef is null) { return; } var currentType = boundMemberTypeDef; while (currentType != null) { foreach (var refMethod in currentType.FindMethods(boundMemberName)) { service.AddReference(refMethod, new StringMemberNameReference(nameInstruction, refMethod)); service.ReduceRenameMode(refMethod, RenameMode.Reflection); } currentType = currentType.BaseType.ResolveTypeDef(); } }
public BaseTypesEntryNode(ITypeDefOrRef tr, bool isInterface) { if (tr == null) throw new ArgumentNullException("tr"); this.tr = tr; this.def = tr.ResolveTypeDef(); this.isInterface = isInterface; this.LazyLoading = true; }
static TypeDef?ResolveWithinSameModule(ITypeDefOrRef type) { if (!(type is null) && type.Scope == type.Module) { return(type.ResolveTypeDef()); } return(null); }
public static TypeSig ThisType(ITypeDefOrRef type) { var typeDef = type.ResolveTypeDef(); if (typeDef?.IsValueType ?? false || type.IsValueType) { return(new ByRefSig(type.ToTypeSig())); } return(type.ToTypeSig()); }
public static bool IsDeprecated(ITypeDefOrRef type) { var td = type.ResolveTypeDef(); if (td == null) { return(false); } return(IsDeprecated(td.CustomAttributes)); }
public BaseTypesEntryNode(ITypeDefOrRef tr, bool isInterface) { if (tr == null) { throw new ArgumentNullException("tr"); } this.tr = tr; this.def = tr.ResolveTypeDef(); this.isInterface = isInterface; this.LazyLoading = true; }
public static MemberSpecialFlags GetMemberSpecialFlags(ITypeDefOrRef type) { var flags = MemberSpecialFlags.None; if (IsAwaitableType(type.ResolveTypeDef())) { flags |= MemberSpecialFlags.Awaitable; } return(flags); }
public static TypeDef ResolveWithinSameModule(this ITypeDefOrRef type) { if (type != null && type.Scope == type.Module) { return(type.ResolveTypeDef()); } else { return(null); } }
public static bool IsAnonymousType(this ITypeDefOrRef type) { if (type == null) { return(false); } if (string.IsNullOrEmpty(type.Namespace) && type.HasGeneratedName() && (type.Name.String.Contains("AnonType") || type.Name.String.Contains("AnonymousType"))) { TypeDef td = type.ResolveTypeDef(); return(td != null && td.IsCompilerGenerated()); } return(false); }
public override void ActivateItem(System.Windows.RoutedEventArgs e) { // on item activation, try to resolve once again (maybe the user loaded the assembly in the meantime) if (def == null) { def = tr.ResolveTypeDef(); if (def != null) { this.LazyLoading = true; } // re-load children } e.Handled = ActivateItem(this, def); }
/// <summary> /// Always resolves the type, whether there is a <see cref="TypeResolver"/> or not. /// </summary> /// <param name="typeResolver">The type resolver.</param> /// <param name="typeDefOrRef">Type of the base.</param> /// <returns></returns> public static TypeDef SafeResolve(this TypeResolver typeResolver, ITypeDefOrRef typeDefOrRef) { TypeDef typeDefinition; if (typeResolver == null) { typeDefinition = typeDefOrRef.ResolveTypeDef(); } else { typeDefinition = typeResolver.Resolve(typeDefOrRef); } return(typeDefinition); }
public static bool IsAnonymousType(this ITypeDefOrRef type) { if (type == null) { return(false); } string name; if (type.HasGeneratedName() && string.IsNullOrEmpty(type.GetNamespaceInternal()) && ((name = type.Name).Contains("AnonType") || name.Contains("AnonymousType"))) { TypeDef td = type.ResolveTypeDef(); return(td != null && td.IsCompilerGenerated()); } return(false); }
/// <summary> /// Gets whether the type supports collection initializers. /// </summary> static bool IsCollectionType(ITypeDefOrRef tr) { if (tr == null) { return(false); } TypeDef td = tr.ResolveTypeDef(); while (td != null) { if (td.Interfaces.Any(intf => intf.Interface.Name == "IEnumerable" && intf.Interface.Namespace == "System.Collections")) { return(true); } td = td.BaseType != null?td.BaseType.ResolveTypeDef() : null; } return(false); }
/// <summary> /// Find a MethodDef from a declaring type and some MethodData. Will generate /// a list of possible MethodSigs and check against each of them, returning the /// first-found MethodDef that matches the method name and signature. /// </summary> /// <param name="declaringType">Declaring type</param> /// <param name="data">MethodData</param> /// <param name="detectedSig">The detected MethodSig</param> /// <returns>MethodDef if found, null if none found</returns> MethodDef FindMethodCheckBaseType(ITypeDefOrRef declaringType, MethodData data, out MethodSig detectedSig) { detectedSig = null; TypeDef declaringDef = declaringType.ResolveTypeDef(); if (declaringDef == null) { return(null); } MethodDef method = null; MethodSig methodSig = GetMethodSig(data); var possibleSigs = PossibleMethodSigs(declaringType, methodSig, data); detectedSig = possibleSigs.FirstOrDefault(sig => { return((method = declaringDef.FindMethodCheckBaseType(data.Name, sig)) != null); }); return(method); }
public override string ToString() { var td = EnumType.ResolveTypeDef(); if (td != null) { var s = ModelUtils.GetEnumFieldName(td, Value); if (s != null) { return(string.Format("{0}.{1}", EnumType, s)); } } if (!IsArray) { return(string.Format("({0}){1}", EnumType == null ? (object)"Unknown Enum" : EnumType, Value)); } var list = Value as System.Collections.IList; if (list == null) { return(string.Format("({0}[])null", EnumType == null ? (object)"Unknown Enum" : EnumType)); } var sb = new StringBuilder(); sb.Append(string.Format("new {0}[] {{", EnumType == null ? (object)"Unknown Enum" : EnumType)); for (int i = 0; i < list.Count; i++) { if (i > 0) { sb.Append(','); } sb.Append(' '); var s = ModelUtils.GetEnumFieldName(td, list[i]); sb.Append(s ?? (Value == null ? "null" : Value.ToString())); } sb.Append(" }"); return(sb.ToString()); }
public override string ToString() { var td = EnumType.ResolveTypeDef(); if (td != null) { var s = ModelUtils.GetEnumFieldName(td, Value); if (s != null) { return($"{EnumType}.{s}"); } } if (!IsArray) { return($"({(EnumType ?? (object)dnSpy_AsmEditor_Resources.UnknownEnum)}){Value}"); } var list = Value as System.Collections.IList; if (list == null) { return($"({(EnumType ?? (object)dnSpy_AsmEditor_Resources.UnknownEnum)}[])null"); } var sb = new StringBuilder(); sb.Append($"new {(EnumType ?? (object)dnSpy_AsmEditor_Resources.UnknownEnum)}[] {{"); for (int i = 0; i < list.Count; i++) { if (i > 0) { sb.Append(','); } sb.Append(' '); var s = ModelUtils.GetEnumFieldName(td, list[i]); sb.Append(s ?? (Value == null ? "null" : Value.ToString())); } sb.Append(" }"); return(sb.ToString()); }
private int GetSize(ITypeDefOrRef refOrDef, bool topmost = true) { int ret = 0; TypeDef target = refOrDef.ResolveTypeDef(); if (target == null) { return(GetSize(refOrDef.ReflectionFullName)); } if (!topmost && target.BaseType != null && target.BaseType.Name == "ValueType") { //ret += 1; } foreach (FieldDef fd in target.Fields) { if (fd.FieldType.TryGetTypeDef() != null) { int size = GetSize(fd.FieldType.ToTypeDefOrRef(), false); ret += size; } else { int size = GetSize(fd.FieldType.ReflectionFullName); ret += size; } } if (ret % 4 != 0) { int rem = ret % 4; ret -= rem; ret += 4; } return(ret); }
private static int GetSize(ITypeDefOrRef refOrDef, bool topmost = true) { int ret = 0; TypeDef target = refOrDef.ResolveTypeDef(); if ((typeof(object).Assembly.ManifestModule.Name).Contains(target.Module.Assembly.Name)) { return(System(refOrDef.FullName)); } if (!topmost && target.BaseType != null && target.BaseType.Name == "ValueType") { //ret += 1; } foreach (FieldDef fd in target.Fields) { if (fd.FieldType.TryGetTypeDef() != null) { int size = GetSize(fd.FieldType.ToTypeDefOrRef(), false); ret += size; } else { int size = System(fd.FieldSig.Type.FullName); ret += size; } } if (ret % 4 != 0) { int rem = ret % 4; ret -= rem; ret += 4; } return(ret); }
private IEnumerable <string> GetImplementedTypes(TypeDef type) { ITypeDefOrRef typeRef = type; while (true) { var typeDef = typeRef.ResolveTypeDef(); if (typeDef == null) { break; } // Get interfaces of typeDef var interfaces = new Queue <ITypeDefOrRef>(typeDef.Interfaces.Select(x => x.Interface)); while (interfaces.Count > 0) { var i = interfaces.Dequeue(); yield return(i.ScopeType.ToString()); var resolvedInterface = i.ResolveTypeDef(); if (resolvedInterface != null && resolvedInterface.HasInterfaces) { foreach (var n in resolvedInterface.Interfaces) { interfaces.Enqueue(n.Interface); } } } typeRef = typeDef.BaseType; if (typeRef == null) { break; } yield return(typeRef.ScopeType.ToString()); // TODO: PERF don't convert to string } }
/// <summary> /// Find a MethodDef from a declaring type and some MethodData. Will generate /// a list of possible MethodSigs and check against each of them, returning the /// first-found MethodDef that matches the method name and signature. /// </summary> /// <param name="declaringType">Declaring type</param> /// <param name="data">MethodData</param> /// <param name="detectedSig">The detected MethodSig</param> /// <returns>MethodDef if found, null if none found</returns> MethodDef FindMethodCheckBaseType(ITypeDefOrRef declaringType, MethodData data, out MethodSig detectedSig) { detectedSig = null; TypeDef declaringDef = declaringType.ResolveTypeDef(); if (declaringDef == null) return null; MethodDef method = null; MethodSig methodSig = GetMethodSig(data); var possibleSigs = PossibleMethodSigs(declaringType, methodSig, data); detectedSig = possibleSigs.FirstOrDefault(sig => { return (method = declaringDef.FindMethodCheckBaseType(data.Name, sig)) != null; }); return method; }
void WriteToolTipWithClassInfo(ITextOutput output, ITypeDefOrRef type) { var td = type.ResolveTypeDef(); MethodDef invoke; if (IsDelegate(td) && (invoke = td.FindMethod("Invoke")) != null && invoke.MethodSig != null) { output.Write("delegate", TextTokenType.Keyword); output.WriteSpace(); var writer = new MethodWriter(this, output, invoke); writer.WriteReturnType(); // Always print the namespace here because that's what VS does. I.e., ignore // TOOLTIP_USE_NAMESPACES. WriteToolTipType(output, td, true); writer.WriteGenericArguments(); writer.WriteMethodParameterList('(', ')'); return; } if (td == null) { base.WriteToolTip(output, type, null); return; } string keyword; if (td.IsEnum) keyword = "enum"; else if (td.IsValueType) keyword = "struct"; else if (td.IsInterface) keyword = "interface"; else keyword = "class"; output.Write(keyword, TextTokenType.Keyword); output.WriteSpace(); // Always print the namespace here because that's what VS does. I.e., ignore // TOOLTIP_USE_NAMESPACES. WriteToolTipType(output, type, true, false); }
private int GetSizeOfficialMSDNStruct(ITypeDefOrRef refOrDef, bool topmost = true) { int ret = 0; TypeDef target = refOrDef.ResolveTypeDef(); if (target == null) { return(GetSize(refOrDef.ReflectionFullName)); } if (target.BaseType != null && target.BaseType.Name == "ValueType") { ret += 1; } foreach (FieldDef fd in target.Fields) { if (fd.FieldType.TryGetTypeDef() != null) { int size = GetSize(fd.FieldType.ToTypeDefOrRef(), false); int alignment = 0; alignment = 8; if (ret % alignment != 0) { int rem = ret % alignment; ret -= rem; ret += alignment; } ret += size; } else { int size = GetSize(fd.FieldType.ReflectionFullName); int alignment = 0; if (size == 4) { alignment = 4; } else if (size == 2) { alignment = 2; } else if (size == 1) { alignment = 1; } else if (size == 8) { alignment = 4; } else if (size == 16) { alignment = 4; // TODO: Check this } if (ret % alignment != 0) { int rem = ret % alignment; ret -= rem; ret += alignment; } ret += size; } } return(ret); }
public override object VisitObjectCreateExpression(ObjectCreateExpression objectCreateExpression, object data) { if (objectCreateExpression.Arguments.Count == 2) { Expression obj = objectCreateExpression.Arguments.First(); Expression func = objectCreateExpression.Arguments.Last(); Annotation annotation = func.Annotation <Annotation>(); if (annotation != null) { IdentifierExpression methodIdent = (IdentifierExpression)((InvocationExpression)func).Arguments.Single(); IMethod method = methodIdent.Annotation <IMethod>(); if (method != null) { if (HandleAnonymousMethod(objectCreateExpression, obj, method)) { return(null); } var ilRanges = objectCreateExpression.GetAllRecursiveILRanges(); // Perform the transformation to "new Action(obj.func)". obj.Remove(); methodIdent.Remove(); if (!annotation.IsVirtual && obj is ThisReferenceExpression) { // maybe it's getting the pointer of a base method? if (method.DeclaringType.ResolveTypeDef() != context.CurrentType) { obj = new BaseReferenceExpression().WithAnnotation(method.DeclaringType); } } if (!annotation.IsVirtual && obj is NullReferenceExpression && method.MethodSig != null && !method.MethodSig.HasThis) { // We're loading a static method. // However it is possible to load extension methods with an instance, so we compare the number of arguments: bool isExtensionMethod = false; ITypeDefOrRef delegateType = objectCreateExpression.Type.Annotation <ITypeDefOrRef>(); if (delegateType != null) { TypeDef delegateTypeDef = delegateType.ResolveTypeDef(); if (delegateTypeDef != null) { MethodDef invokeMethod = delegateTypeDef.Methods.FirstOrDefault(m => m.Name == "Invoke"); if (invokeMethod != null) { isExtensionMethod = (invokeMethod.Parameters.GetNumberOfNormalParameters() + 1 == method.MethodSig.GetParameters().Count); } } } if (!isExtensionMethod) { obj = new TypeReferenceExpression { Type = AstBuilder.ConvertType(method.DeclaringType) }; } } // now transform the identifier into a member reference MemberReferenceExpression mre = new MemberReferenceExpression(); mre.Target = obj; mre.MemberNameToken = (Identifier)methodIdent.IdentifierToken.Clone(); methodIdent.TypeArguments.MoveTo(mre.TypeArguments); mre.AddAnnotation(method); objectCreateExpression.Arguments.Clear(); objectCreateExpression.Arguments.Add(mre); objectCreateExpression.AddAnnotation(ilRanges); return(null); } } } return(base.VisitObjectCreateExpression(objectCreateExpression, data)); }
public BaseTypesAnalysis(ITypeDefOrRef type) { this.type = type; typeDef = type.ResolveTypeDef(); }
static TypeDef ResolveWithinSameModule(ITypeDefOrRef type) { if (type != null && type.Scope == type.Module) return type.ResolveTypeDef(); return null; }
void WriteToolTip(ITypeDefOrRef type) { var td = type.ResolveTypeDef(); MethodDef invoke; if (IsDelegate(td) && (invoke = td.FindMethod("Invoke")) != null && invoke.MethodSig != null) { OutputWrite("delegate", TextTokenKind.Keyword); WriteSpace(); var info = new MethodInfo(invoke); WriteModuleName(info); WriteReturnType(info); // Always print the namespace here because that's what VS does WriteType(td, true, ShowTypeKeywords); WriteGenericArguments(info); WriteMethodParameterList(info, "(", ")"); return; } if (td == null) { Write(type); return; } string keyword; if (td.IsEnum) keyword = "enum"; else if (td.IsValueType) keyword = "struct"; else if (td.IsInterface) keyword = "interface"; else keyword = "class"; OutputWrite(keyword, TextTokenKind.Keyword); WriteSpace(); // Always print the namespace here because that's what VS does WriteType(type, true, false); }
public DerivedTypesAnalysis(ITypeDefOrRef type) { this.type = type; typeDef = type.ResolveTypeDef(); }