如何在 C# 中使用 Console.Log、Print_R()、Debug.Trace?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14448160/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Console.Log, Print_R(), Debug.Trace in C#?
提问by JREAM
PHP has a function called print_r() and var_dump() that will display all the contents of an item. It makes it very easy to figure out what things are.
PHP 有一个名为 print_r() 和 var_dump() 的函数,它们将显示一个项目的所有内容。它使弄清楚事物是什么变得非常容易。
Is there something like that in C#?
C#中有类似的东西吗?
I know there is a Console.WriteLine("Hello");
in C#, but does this work in the MVC? Can I do some type of debug.trace()
like flash does into a debug console while I run the application?
我知道Console.WriteLine("Hello");
C# 中有一个,但这在 MVC 中有效吗?debug.trace()
当我运行应用程序时,我可以在调试控制台中执行某种类似 flash 的操作吗?
采纳答案by Paul Sullivan
System.Diagnostics.Debug.WriteLine("blah");
and in order to show all variables in the object you would have to override its ToString() method or write a method which returns all the info you need from the object. i.e.
并且为了显示对象中的所有变量,您必须覆盖它的 ToString() 方法或编写一个方法来返回您需要的所有信息。IE
class Blah{
string mol = "The meaning of life is";
int a = 42;
public override string ToString()
{
return String.Format("{0} {1}", mol, a);
}
}
System.Diagnostics.Debug.WriteLine(new Blah().ToString());
In short there is nothing in built but it can be done.
简而言之,没有什么是内置的,但它可以做到。
If you must print ALL the objects info without overriding or adding logic on a class by class level then you are in the realms of reflection to iterate the objects PropertInfo array
如果您必须打印所有对象信息而不按类级别在类上覆盖或添加逻辑,那么您处于反射领域以迭代对象PropertInfo 数组