private static IList <SnoopMethodInformation> GetMethodInfos(object o) { if (o == null) { return(new ObservableCollection <SnoopMethodInformation>()); } Type t = o.GetType(); var methods = t.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.InvokeMethod); var methodsToReturn = new List <SnoopMethodInformation>(); foreach (var method in methods) { if (method.IsSpecialName) { continue; } var info = new SnoopMethodInformation(method); info.MethodName = method.Name; methodsToReturn.Add(info); } methodsToReturn.Sort(); return(methodsToReturn); }
void TryToInvokeMethod(SnoopMethodInformation selectedMethod, object[] parameters) { try { var returnValue = selectedMethod.MethodInfo.Invoke(Target, parameters); if (returnValue == null) { SetNullReturnType(selectedMethod); return; } resultStringContainer.Visibility = textBlockResult.Visibility = textBlockResultLabel.Visibility = Visibility.Visible; textBlockResultLabel.Text = "Result as string: "; textBlockResult.Text = returnValue.ToString(); var properties = returnValue.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public); //var properties = PropertyInformation.GetAllProperties(returnValue, new Attribute[] { new PropertyFilterAttribute(PropertyFilterOptions.All) }); if (properties.Length == 0) { resultProperties.Visibility = Visibility.Collapsed; } else { resultProperties.Visibility = Visibility.Visible; propertyInspector.RootTarget = returnValue; } } catch (Exception ex) { var message = ex.InnerException != null ? ex.InnerException.Message : ex.Message; MessageBox.Show(message, "Error invoking method"); } }
private void SetNullReturnType(SnoopMethodInformation selectedMethod) { if (selectedMethod.MethodInfo.ReturnType == typeof(void)) { this.resultStringContainer.Visibility = this.resultProperties.Visibility = System.Windows.Visibility.Collapsed; } else { this.resultProperties.Visibility = System.Windows.Visibility.Collapsed; this.resultStringContainer.Visibility = System.Windows.Visibility.Visible; this.textBlockResult.Text = string.Empty; this.textBlockResultLabel.Text = "Method evaluated to null"; this.textBlockResult.Visibility = System.Windows.Visibility.Collapsed; } }
private void comboBoxMethodChanged(object sender, EventArgs e) { var selectedMethod = this.comboBoxMethods.SelectedValue as SnoopMethodInformation; if (selectedMethod == null || this.Target == null) { return; } var parameters = selectedMethod.GetParameters(this.Target.GetType()); this.itemsControlParameters.ItemsSource = parameters; this.parametersContainer.Visibility = parameters.Count == 0 ? Visibility.Collapsed : Visibility.Visible; this.resultProperties.Visibility = this.resultStringContainer.Visibility = Visibility.Collapsed; _previousMethodInformation = selectedMethod; }
private void TryToInvokeMethod(SnoopMethodInformation selectedMethod, object[] parameters) { try { var returnValue = selectedMethod.MethodInfo.Invoke(this.Target, parameters); if (returnValue == null) { SetNullReturnType(selectedMethod); return; } else { this.resultStringContainer.Visibility = this.textBlockResult.Visibility = this.textBlockResultLabel.Visibility = System.Windows.Visibility.Visible; } this.textBlockResultLabel.Text = "Result as string: "; this.textBlockResult.Text = returnValue.ToString(); var properties = returnValue.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public); //var properties = PropertyInformation.GetAllProperties(returnValue, new Attribute[] { new PropertyFilterAttribute(PropertyFilterOptions.All) }); if (properties.Length == 0) { this.resultProperties.Visibility = System.Windows.Visibility.Collapsed; } else { this.resultProperties.Visibility = System.Windows.Visibility.Visible; this.propertyInspector.RootTarget = returnValue; } } catch (Exception exception) { ErrorDialog.ShowDialog(exception, $"Error invoking method '{selectedMethod.MethodName}'", exceptionAlreadyHandled: true); } }