Visual Studio的即时窗口如何打印对象的所有成员?

发布于 2024-08-13 01:44:05 字数 88 浏览 7 评论 0原文

如何使用输出窗口写入对象的所有成员? Trace.WriteLine 使用方法 ToString 并且不输出所有成员。是否有 API 可以让您无需编写自己的代码?

How can I using the output window write all the members of an object? Trace.WriteLine uses method ToString and doesn't output all the members. Is there API to do it without writing own code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

豆芽 2024-08-20 01:44:05

你可以这样做:

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;

 namespace ConsoleApplication2
 {
     class Program
     {
         static void Main(string[] args)
         {
             var m = new MyClass { AString = "somestring", AnInt = 60 };

             Console.WriteLine(GetObjectInfo(m));

             Console.ReadLine();
         }

         private static string GetObjectInfo(object o)
         {
             var result = new StringBuilder();

             var t = o.GetType();

             result.AppendFormat("Type: {0}\n", t.Name);

             t.GetProperties().ToList().ForEach(pi => result.AppendFormat("{0} = {1}\n", pi.Name, pi.GetValue(o, null).ToString()));

             return result.ToString();
         }
     }

     public class MyClass
     {
         public string AString { get; set; }
         public int AnInt { get; set; }
     }
}    

You can do something like this:

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;

 namespace ConsoleApplication2
 {
     class Program
     {
         static void Main(string[] args)
         {
             var m = new MyClass { AString = "somestring", AnInt = 60 };

             Console.WriteLine(GetObjectInfo(m));

             Console.ReadLine();
         }

         private static string GetObjectInfo(object o)
         {
             var result = new StringBuilder();

             var t = o.GetType();

             result.AppendFormat("Type: {0}\n", t.Name);

             t.GetProperties().ToList().ForEach(pi => result.AppendFormat("{0} = {1}\n", pi.Name, pi.GetValue(o, null).ToString()));

             return result.ToString();
         }
     }

     public class MyClass
     {
         public string AString { get; set; }
         public int AnInt { get; set; }
     }
}    
猫卆 2024-08-20 01:44:05

它可能通过反射迭代成员。

It's probably iterating through the members via reflection.

月下凄凉 2024-08-20 01:44:05

特定对象上的 ToString() 方法被调用,如果该方法已被重写以显示所有成员,那就没问题了。然而,并非所有对象都实现了 ToString() 方法,在这种情况下,该方法返回对象类型信息。

编写一个使用

编辑:此函数将返回给定对象的属性、添加方法、事件以及您需要的所有其他内容。 (这是用 VB 编写的,这台工作电脑上没有 C#)

Function ListMembers(ByVal target As Object) As String

    Dim targetType As Type = target.GetType 

    Dim props() As Reflection.PropertyInfo = targetType.GetProperties

    Dim sb As New System.Text.StringBuilder

    For Each prop As Reflection.PropertyInfo In props
        sb.AppendLine(String.Format("{0} is a {1}", prop.Name, prop.PropertyType.FullName))
    Next

    Return sb.ToString

End Function

The ToString() method on the particular object gets called, and if that method has been overridden to show all members, then fine. However not all objects have their ToString() methods implemented, in which case the method returns the object type info.

Instead of calling ToString() write a custom function that uses reflection to enumerate the object members, and output that.

Edit: This function will return the given object's properties, add methods, events everything else you need. (It's in VB, no C# on this work PC)

Function ListMembers(ByVal target As Object) As String

    Dim targetType As Type = target.GetType 

    Dim props() As Reflection.PropertyInfo = targetType.GetProperties

    Dim sb As New System.Text.StringBuilder

    For Each prop As Reflection.PropertyInfo In props
        sb.AppendLine(String.Format("{0} is a {1}", prop.Name, prop.PropertyType.FullName))
    Next

    Return sb.ToString

End Function
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文