Beispiel #1
0
 public static bool CanAnalyze(IMemberRef?member, IDecompiler decompiler)
 {
     member = ResolveReference(member);
     return(member is TypeDef ||
            member is FieldDef ||
            member is MethodDef ||
            PropertyNode.CanShow(member, decompiler) ||
            EventNode.CanShow(member, decompiler));
 }
Beispiel #2
0
 public bool Write(IDecompilerOutput output, IMemberRef?member)
 {
     if (member is null || member is GenericParam)
     {
         return(false);
     }
     Write(output, member);
     return(true);
 }
Beispiel #3
0
 public static AnalyzerTreeNodeData?TryCreateAnalyzer(IMemberRef?member, IDecompiler decompiler)
 {
     if (CanShow(member, decompiler))
     {
         return(new PropertyNode((PropertyDef)member !));
     }
     else
     {
         return(null);
     }
 }
Beispiel #4
0
        static IMemberRef?FindMember(ModuleDef module, string key, Func <TypeDef, IEnumerable <IMemberRef> > memberSelector)
        {
            int parenPos = key.IndexOf('(');
            int dotPos;

            if (parenPos > 0)
            {
                dotPos = key.LastIndexOf('.', parenPos - 1, parenPos);
            }
            else
            {
                dotPos = key.LastIndexOf('.');
            }
            if (dotPos < 0)
            {
                return(null);
            }
            TypeDef?type = FindType(module, key.Substring(2, dotPos - 2));

            if (type is null)
            {
                return(null);
            }
            string shortName;

            if (parenPos > 0)
            {
                shortName = key.Substring(dotPos + 1, parenPos - (dotPos + 1));
            }
            else
            {
                shortName = key.Substring(dotPos + 1);
            }
            IMemberRef?shortNameMatch = null;
            var        sb             = new StringBuilder();

            foreach (IMemberRef member in memberSelector(type))
            {
                var memberKey = GetKey(member, sb);
                if (memberKey?.CheckEquals(key) == true)
                {
                    return(member);
                }
                if (shortName == member.Name.Replace('.', '#'))
                {
                    shortNameMatch = member;
                }
            }
            // if there's no match by ID string (key), return the match by name.
            return(shortNameMatch);
        }
Beispiel #5
0
 static IMemberDef?ResolveDef(IMemberRef?mr)
 {
     if (mr is ITypeDefOrRef)
     {
         return(((ITypeDefOrRef)mr).ResolveTypeDef());
     }
     if (mr is IMethod && ((IMethod)mr).IsMethod)
     {
         return(((IMethod)mr).ResolveMethodDef());
     }
     if (mr is IField)
     {
         return(((IField)mr).ResolveFieldDef());
     }
     return(mr as IMemberDef);
 }
Beispiel #6
0
        static bool IsPublic(IMemberRef?memberRef)
        {
            var def = Resolve(memberRef);

            if (def is TypeDef)
            {
                return(IsAccessible((TypeDef)def));
            }

            var md = def as IMemberDef;

            if (md is null)
            {
                return(false);
            }
            if (!IsAccessible(md.DeclaringType))
            {
                return(false);
            }

            if (def is MethodDef method)
            {
                return(IsAccessible(method));
            }

            if (def is FieldDef field)
            {
                return(IsAccessible(field));
            }

            if (def is PropertyDef prop)
            {
                return(IsAccessible(prop));
            }

            if (def is EventDef evt)
            {
                return(IsAccessible(evt));
            }

            return(false);
        }
Beispiel #7
0
        public static bool Write(IDecompilerOutput output, IMemberRef?member)
        {
            if (member is IMethod method && method.IsMethod)
            {
                method.WriteMethodTo(output);
                return(true);
            }

            if (member is IField field && field.IsField)
            {
                field.WriteFieldTo(output);
                return(true);
            }

            if (member is PropertyDef prop)
            {
                var dis = new ReflectionDisassembler(output, false, new DisassemblerOptions(0, new System.Threading.CancellationToken(), null));
                dis.DisassembleProperty(prop, false);
                return(true);
            }

            if (member is EventDef evt)
            {
                var dis = new ReflectionDisassembler(output, false, new DisassemblerOptions(0, new System.Threading.CancellationToken(), null));
                dis.DisassembleEvent(evt, false);
                return(true);
            }

            if (member is ITypeDefOrRef type)
            {
                type.WriteTo(output, ILNameSyntax.TypeName);
                return(true);
            }

            return(false);
        }
Beispiel #8
0
 bool ISimpleILPrinter.Write(IDecompilerOutput output, IMemberRef?member) => ILDecompilerUtils.Write(output, member);
Beispiel #9
0
        static IMemberDef?Resolve(IMemberRef?memberRef)
        {
            var member = ResolveDef(memberRef);
            var md     = member as MethodDef;

            if (md is null)
            {
                return(member);
            }

            if (md.SemanticsAttributes == 0)
            {
                return(member);
            }

            // Find the property or event and return it instead

            foreach (var prop in md.DeclaringType.Properties)
            {
                foreach (var md2 in prop.GetMethods)
                {
                    if (md2 == md)
                    {
                        return(prop);
                    }
                }
                foreach (var md2 in prop.SetMethods)
                {
                    if (md2 == md)
                    {
                        return(prop);
                    }
                }
                foreach (var md2 in prop.OtherMethods)
                {
                    if (md2 == md)
                    {
                        return(prop);
                    }
                }
            }

            foreach (var evt in md.DeclaringType.Events)
            {
                if (evt.AddMethod == md)
                {
                    return(evt);
                }
                if (evt.InvokeMethod == md)
                {
                    return(evt);
                }
                if (evt.RemoveMethod == md)
                {
                    return(evt);
                }
                foreach (var md2 in evt.OtherMethods)
                {
                    if (md2 == md)
                    {
                        return(evt);
                    }
                }
            }

            // Shouldn't be here
            return(member);
        }
Beispiel #10
0
 public static bool CanShow(IMemberRef?member, IDecompiler decompiler) => member is PropertyDef;
Beispiel #11
0
 /// <summary>
 /// Gets an XML doc key
 /// </summary>
 /// <param name="member">Member</param>
 /// <param name="b">String builder</param>
 /// <returns></returns>
 public static StringBuilder?GetKey(IMemberRef?member, StringBuilder b)
 {
     if (member is null)
     {
         return(null);
     }
     b.Clear();
     if (member is ITypeDefOrRef)
     {
         b.Append("T:");
         AppendTypeName(b, ((ITypeDefOrRef)member).ToTypeSig());
     }
     else
     {
         if (member.IsField)
         {
             b.Append("F:");
         }
         else if (member.IsPropertyDef)
         {
             b.Append("P:");
         }
         else if (member.IsEventDef)
         {
             b.Append("E:");
         }
         else if (member.IsMethod)
         {
             b.Append("M:");
         }
         AppendTypeName(b, member.DeclaringType.ToTypeSig());
         b.Append('.');
         b.Append(member.Name.Replace('.', '#'));
         IList <Parameter>?parameters;
         TypeSig?          explicitReturnType = null;
         if (member.IsPropertyDef)
         {
             parameters = GetParameters((PropertyDef)member).ToList();
         }
         else if (member.IsMethod)
         {
             var mr = (IMethod)member;
             if (mr.NumberOfGenericParameters > 0)
             {
                 b.Append("``");
                 b.Append(mr.NumberOfGenericParameters);
             }
             parameters = mr.GetParameters();
             if (mr.Name == "op_Implicit" || mr.Name == "op_Explicit")
             {
                 explicitReturnType = mr.MethodSig.GetRetType();
             }
         }
         else
         {
             parameters = null;
         }
         if (parameters is not null && parameters.Any(a => a.IsNormalMethodParameter))
         {
             b.Append('(');
             for (int i = 0; i < parameters.Count; i++)
             {
                 var param = parameters[i];
                 if (!param.IsNormalMethodParameter)
                 {
                     continue;
                 }
                 if (param.MethodSigIndex > 0)
                 {
                     b.Append(',');
                 }
                 AppendTypeName(b, param.Type);
             }
             b.Append(')');
         }
         if (explicitReturnType is not null)
         {
             b.Append('~');
             AppendTypeName(b, explicitReturnType);
         }
     }
     return(b);
 }
 static bool CheckEquals(IMemberRef?mr1, IMemberRef?mr2) =>
 new SigComparer(SigComparerOptions.CompareDeclaringTypes | SigComparerOptions.PrivateScopeIsComparable).Equals(mr1, mr2);
        /// <summary>
        /// Gets a member color
        /// </summary>
        /// <param name="memberRef">Member</param>
        /// <returns></returns>
        public virtual object GetColor(IMemberRef?memberRef)
        {
            if (memberRef is null)
            {
                return(BoxedTextColor.Text);
            }

            if (memberRef.IsField)
            {
                var fd = ((IField)memberRef).ResolveFieldDef();
                if (fd is null)
                {
                    return(BoxedTextColor.InstanceField);
                }
                if (fd.DeclaringType.IsEnum)
                {
                    return(BoxedTextColor.EnumField);
                }
                if (fd.IsLiteral)
                {
                    return(BoxedTextColor.LiteralField);
                }
                if (fd.IsStatic)
                {
                    return(BoxedTextColor.StaticField);
                }
                return(BoxedTextColor.InstanceField);
            }
            if (memberRef.IsMethod)
            {
                var mr = (IMethod)memberRef;
                if (mr.MethodSig is null)
                {
                    return(BoxedTextColor.InstanceMethod);
                }
                var md = mr.ResolveMethodDef();
                if (!(md is null) && md.IsConstructor)
                {
                    return(GetColor(md.DeclaringType));
                }
                if (!mr.MethodSig.HasThis)
                {
                    if (!(md is null) && md.IsDefined(systemRuntimeCompilerServicesString, extensionAttributeString))
                    {
                        return(BoxedTextColor.ExtensionMethod);
                    }
                    return(BoxedTextColor.StaticMethod);
                }
                return(BoxedTextColor.InstanceMethod);
            }
            if (memberRef.IsPropertyDef)
            {
                var p = (PropertyDef)memberRef;
                return(GetColor(p.GetMethod ?? p.SetMethod, BoxedTextColor.StaticProperty, BoxedTextColor.InstanceProperty));
            }
            if (memberRef.IsEventDef)
            {
                var e = (EventDef)memberRef;
                return(GetColor(e.AddMethod ?? e.RemoveMethod ?? e.InvokeMethod, BoxedTextColor.StaticEvent, BoxedTextColor.InstanceEvent));
            }

            if (memberRef is TypeDef td)
            {
                return(GetColor(td));
            }

            if (memberRef is TypeRef tr)
            {
                return(GetColor(tr));
            }

            if (memberRef is TypeSpec ts)
            {
                if (ts.TypeSig is GenericSig gsig)
                {
                    return(GetColor(gsig));
                }
                return(BoxedTextColor.Type);
            }

            if (memberRef is GenericParam gp)
            {
                return(GetColor(gp));
            }

            // It can be a MemberRef if it doesn't have a field or method sig (invalid metadata)
            if (memberRef.IsMemberRef)
            {
                return(BoxedTextColor.Text);
            }

            return(BoxedTextColor.Text);
        }
Beispiel #14
0
 public static bool CanShow(IMemberRef?member, IDecompiler decompiler) => member is EventDef;
Beispiel #15
0
        public static void Analyze(IDsToolWindowService toolWindowService, Lazy <IAnalyzerService> analyzerService, IDecompiler decompiler, IMemberRef?member)
        {
            var memberDef = ResolveReference(member);

            if (memberDef is TypeDef type)
            {
                toolWindowService.Show(AnalyzerToolWindowContent.THE_GUID);
                analyzerService.Value.Add(new TypeNode(type));
            }

            if (memberDef is FieldDef field)
            {
                toolWindowService.Show(AnalyzerToolWindowContent.THE_GUID);
                analyzerService.Value.Add(new FieldNode(field));
            }

            if (memberDef is MethodDef method)
            {
                toolWindowService.Show(AnalyzerToolWindowContent.THE_GUID);
                analyzerService.Value.Add(new MethodNode(method));
            }

            var propertyAnalyzer = PropertyNode.TryCreateAnalyzer(member, decompiler);

            if (!(propertyAnalyzer is null))
            {
                toolWindowService.Show(AnalyzerToolWindowContent.THE_GUID);
                analyzerService.Value.Add(propertyAnalyzer);
            }

            var eventAnalyzer = EventNode.TryCreateAnalyzer(member, decompiler);

            if (!(eventAnalyzer is null))
            {
                toolWindowService.Show(AnalyzerToolWindowContent.THE_GUID);
                analyzerService.Value.Add(eventAnalyzer);
            }
        }
Beispiel #16
0
 protected static bool CheckEquals(IMemberRef?mr1, IMemberRef?mr2) => Helpers.CheckEquals(mr1, mr2);