private void ForAllNodes(Action <ObjectViewModel> action)
        {
            ObjectViewModel root = this;

            while (root.ParentObject != null)
            {
                root = root.ParentObject;
            }
            ForNode(root, action);
        }
 private static void ForNode(ObjectViewModel node, Action <ObjectViewModel> action)
 {
     action(node);
     if (node.IsChildrenLoaded)
     {
         foreach (var child in node.Children.OfType <ObjectViewModel>())
         {
             ForNode(child, action);
         }
     }
 }
Exemple #3
0
        public void ReadAssembly(string fileName)
        {
            App.Settings.LastFileName = fileName;
            Title = $"{fileName} – {CommonTitle}";
            object obj;

            try
            {
                obj = AssemblyDefinition.ReadAssembly(fileName);
            }
            catch (Exception ex)
            {
                obj = ex;
            }
            RootObject = new ObjectViewModel(Path.GetFileName(fileName), obj);
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(HintTextVisibility)));
        }
 private void SetFontWeight(PropertyInfo propInfo, ObjectViewModel item)
 {
     if (TheObject.GetType() == typeof(Mono.Cecil.AssemblyDefinition))
     {
         if (propInfo.Name == "EntryPoint" ||
             propInfo.Name == "MainModule")
         {
             ((TextBlock)item.Text).FontWeight = FontWeights.Bold;
         }
     }
     if (TheObject.GetType() == typeof(Mono.Cecil.CustomAttribute))
     {
         if (propInfo.Name == "AttributeType" ||
             propInfo.Name == "ConstructorArguments")
         {
             ((TextBlock)item.Text).FontWeight = FontWeights.Bold;
         }
     }
     if (TheObject.GetType() == typeof(Mono.Cecil.FieldDefinition))
     {
         if (propInfo.Name == "Constant" ||
             propInfo.Name == "CustomAttributes" ||
             propInfo.Name == "FieldType")
         {
             ((TextBlock)item.Text).FontWeight = FontWeights.Bold;
         }
     }
     if (TheObject.GetType() == typeof(Mono.Cecil.GenericInstanceMethod))
     {
         if (propInfo.Name == "DeclaringType" ||
             propInfo.Name == "GenericArguments" ||
             propInfo.Name == "Parameters" ||
             propInfo.Name == "ReturnType")
         {
             ((TextBlock)item.Text).FontWeight = FontWeights.Bold;
         }
     }
     if (TheObject.GetType() == typeof(Mono.Cecil.GenericInstanceType))
     {
         if (propInfo.Name == "GenericArguments")
         {
             ((TextBlock)item.Text).FontWeight = FontWeights.Bold;
         }
     }
     if (TheObject.GetType() == typeof(Mono.Cecil.GenericParameter))
     {
         if (propInfo.Name == "Constraints")
         {
             ((TextBlock)item.Text).FontWeight = FontWeights.Bold;
         }
     }
     if (TheObject.GetType() == typeof(Mono.Cecil.Cil.Instruction))
     {
         if (propInfo.Name == "OpCode" ||
             propInfo.Name == "Operand")
         {
             ((TextBlock)item.Text).FontWeight = FontWeights.Bold;
         }
     }
     if (TheObject.GetType() == typeof(Mono.Cecil.Cil.MethodBody))
     {
         if (propInfo.Name == "Instructions" ||
             propInfo.Name == "Variables")
         {
             ((TextBlock)item.Text).FontWeight = FontWeights.Bold;
         }
     }
     if (TheObject.GetType() == typeof(Mono.Cecil.MethodDefinition))
     {
         if (propInfo.Name == "Body" ||
             propInfo.Name == "CustomAttributes" ||
             propInfo.Name == "GenericParameters" ||
             propInfo.Name == "Parameters" ||
             propInfo.Name == "ReturnType")
         {
             ((TextBlock)item.Text).FontWeight = FontWeights.Bold;
         }
     }
     if (TheObject.GetType() == typeof(Mono.Cecil.MethodReference))
     {
         if (propInfo.Name == "DeclaringType" ||
             propInfo.Name == "Parameters" ||
             propInfo.Name == "ReturnType")
         {
             ((TextBlock)item.Text).FontWeight = FontWeights.Bold;
         }
     }
     if (TheObject.GetType() == typeof(Mono.Cecil.ModuleDefinition))
     {
         if (propInfo.Name == "Types")
         {
             ((TextBlock)item.Text).FontWeight = FontWeights.Bold;
         }
     }
     if (TheObject.GetType() == typeof(Mono.Cecil.ParameterDefinition))
     {
         if (propInfo.Name == "ParameterType")
         {
             ((TextBlock)item.Text).FontWeight = FontWeights.Bold;
         }
     }
     if (TheObject.GetType() == typeof(Mono.Cecil.PropertyDefinition))
     {
         if (propInfo.Name == "CustomAttributes" ||
             propInfo.Name == "GetMethod" ||
             propInfo.Name == "PropertyType" ||
             propInfo.Name == "SetMethod")
         {
             ((TextBlock)item.Text).FontWeight = FontWeights.Bold;
         }
     }
     if (TheObject.GetType() == typeof(Mono.Cecil.TypeDefinition))
     {
         if (propInfo.Name == "BaseType" ||
             propInfo.Name == "CustomAttributes" ||
             propInfo.Name == "Events" ||
             propInfo.Name == "Fields" ||
             propInfo.Name == "Interfaces" ||
             propInfo.Name == "Methods" ||
             propInfo.Name == "NestedTypes" ||
             propInfo.Name == "Properties")
         {
             ((TextBlock)item.Text).FontWeight = FontWeights.Bold;
         }
     }
 }
        protected override void LoadChildren()
        {
            bool            referenceLoop = false;
            ObjectViewModel parent        = ParentObject;

            while (parent != null)
            {
                if (parent.TheObject == TheObject)
                {
                    referenceLoop = true;
                    break;
                }
                parent = parent.ParentObject;
            }

            if (TheObject != null && referenceLoop)
            {
                var child = new InfoNode("Reference loop", Brushes.Gray, loopIcon);
                Children.Add(child);
            }
            else if (TheObject != null && canLoadChildren)
            {
                if (TheObject.GetType().IsArray)
                {
                    Array a = (Array)TheObject;
                    for (int i = 0; i < a.Length; i++)
                    {
                        object value = a.GetValue(i);
                        Children.Add(new ObjectViewModel($"[{i}]", value, TheObject.GetType().GetElementType(), fieldIcon, null, elementLevel));
                    }
                }
                else if (IsEnumerable(TheObject))
                {
                    int i = 0;
                    foreach (var value in TheObject as IEnumerable)
                    {
                        Children.Add(new ObjectViewModel($"[{i}]", value, value.GetType(), fieldIcon, null, elementLevel));
                        i++;
                    }
                }
                else
                {
                    foreach (var propInfo in TheObject.GetType()
                             .GetProperties(BindingFlags.Instance | BindingFlags.Public)
                             .Where(p => p.GetMethod != null)
                             .OrderBy(p => p.Name))
                    {
                        object value      = null;
                        Brush  valueColor = null;
                        try
                        {
                            value = propInfo.GetValue(TheObject);
                        }
                        catch (Exception ex)
                        {
                            value      = "[" + ex.Message + "]";
                            valueColor = Brushes.OrangeRed;
                        }
                        var child = new ObjectViewModel(propInfo.Name, value, propInfo.PropertyType, propertyIcon, valueColor, elementLevel);
                        Children.Add(child);
                        SetFontWeight(propInfo, child);
                        //if (TheObject.GetType() == typeof(Mono.Cecil.Cil.MethodBody) && propInfo.Name == "Instructions")
                        //{
                        //	child.IsExpanded = true;
                        //}
                    }

                    foreach (var fieldInfo in TheObject.GetType()
                             .GetFields(BindingFlags.Instance | BindingFlags.Public)
                             .OrderBy(f => f.Name))
                    {
                        object value      = null;
                        Brush  valueColor = null;
                        try
                        {
                            value = fieldInfo.GetValue(TheObject);
                        }
                        catch (Exception ex)
                        {
                            value      = "[" + ex.Message + "]";
                            valueColor = Brushes.OrangeRed;
                        }
                        Children.Add(new ObjectViewModel(fieldInfo.Name, value, fieldInfo.FieldType, fieldIcon, valueColor, elementLevel));
                    }

                    foreach (var methodInfo in TheObject.GetType()
                             .GetMethods(BindingFlags.Instance | BindingFlags.Public)
                             .Where(m => m.Name != "Clone")
                             .Where(m => m.Name != "GetHashCode")
                             .Where(m => m.Name != "GetType")
                             .Where(m => m.Name != "ToString")
                             .Where(m => !m.Name.StartsWith("get_"))
                             .Where(m => m.ReturnType != typeof(void))
                             .Where(m => m.GetParameters().Length == 0)
                             .OrderBy(m => m.Name))
                    {
                        object value      = null;
                        Brush  valueColor = null;
                        try
                        {
                            value = methodInfo.Invoke(TheObject, new object[0]);
                        }
                        catch (Exception ex)
                        {
                            value      = ex;
                            valueColor = Brushes.OrangeRed;
                        }
                        Children.Add(new ObjectViewModel(methodInfo.Name + "()", value, methodInfo.ReturnType, methodIcon, valueColor, elementLevel));
                    }
                }
            }
            IsChildrenLoaded = true;
            ForNode(this, o => o.HighlightObject());
        }