显示(观察概念)所有对象的详细信息 [英] Displaying (Watch concept) all the object's details

查看:77
本文介绍了显示(观察概念)所有对象的详细信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码:



I have this code:

public class MyClassXYZ
{
public int X = 4;
public int Y = 3;
public int Z = 1;
public void test1()
{
}
public void test2(int x)
{
}
public void test3(int x, string y)
{
}
}
public class MyClassABC
{
public int a = 1;
public int b = 2;
public int c = 3;
}

void main()
{
MyClassXYZ objA = new MyClassXYZ();
MyClassABC objB = new MyClassABC();

Console.WriteLine("Hello, this is test");
DisplayVariablesAndValues(objA);
DisplayVariablesAndValues(objB);
}







public void DisplayVariablesAndValues(object whatobject)
{
string ClassName;
string ObjectName;
...??...
Console.WriteLine("Class Name : " + ClassName);
Console.WriteLine("Object Name : " + ObjectName);
foreach(...???....)
{
string VarName = "";
string ValName = "";
....???....
Console.WriteLine("Variable Name : "+ VarName);
Console.WriteLine("Value : "+ ValName);
}
foreach(...???....)
{
string Func = "";
string FuncArg = "";
....???....
Console.WriteLine("Function Name : "+ Func);
Console.WriteLine("Function Arguments : "+ FuncArg);
}
Console.WriteLine("-END-");
}



我想要的是,如何从函数DisplayVariablesAndValues中显示(类似于Watch)类中的所有变量和值。

假设我不知道每个班级中的变量。



输出应该是这样的:


All I want is, how to display (similar to Watch) all the variables and values in the classes from the function DisplayVariablesAndValues.
Assume that I don't know what variable in each class.

The output should be like this:

Hello, this is test
Class Name : MyClassXYZ
Object Name : objA
Variable Name : X
Value : 4
Variable Name : Y
Value : 3
Variable Name : Z
Value : 1
Function Name : test1
Function Arguments : 
Function Name : test2
Function Arguments : int
Function Name : test3
Function Arguments : int, string
-END-
Class Name : MyClassABC
Object Name : objB
Variable Name : a
Value : 1
Variable Name : b
Value : 2
Variable Name : c
Value : 3
-END-







需要帮助,谢谢

推荐答案

如果你是清楚的,你将不知道一个类实例有哪些字段和/或属性,那么Reflection 你需要使用的东西,这里的两个解决方案将引导你获得良好的资源。



以下评论假设您可以访问从中感兴趣的类的代码库;如果你是黑客,分析对象你只能参考运行时的实例,这是一个完全不同的球类游戏。



有很多方法,如果您可以修改类中的代码,实现任何事件的复杂记录,字段和/或属性值的任何更改。



有一个完整的,通过外部代码处理(通常称为AOP(面向方面​​编程))进行代码检测和记录的非常有趣的智力工作。这些想法是通过不直接修改您的基本代码来处理诸如身份验证,财产通知更改,测试等跨领域问题。



A这里有很好的日志记录策略概述:[ ^ ] 。



对于AOP,请参阅:[ ^ ],[ ^ ],[ ^ ],
If you are crystal-clear you will not know what Fields and/or Properties a Class instance has, then Reflection is what you need to use, and the two solutions here will direct you to good resources.

The comments that follow assume you do have access to the code-base from which the Classes you are interested in are derived; if you are "hacking," analyzing objects you only have references to instances of at run-time, that's a whole different ball-game.

There are many ways, if you can modify the code in the Class, to implement sophisticated logging of any Event, any change in values of Fields and/or Properties.

There is an entire, very interesting, intellectual endeavor involving "instrumentation and logging" of code via external processing of code, often referred to as AOP (Aspect Oriented Programming). The ideas is to deal with "cross-cutting concerns" such as authentication, property notification change, testing, etc., by not directly modifying your base code.

A pretty good overview of logging strategies here: [^].

For AOP, see: [^], [^], [^],


这称为反射。请从这里开始:

https: //msdn.microsoft.com/en-us/library/system.type%28v=vs.110%29.aspx [ ^ ],

https://msdn.microsoft.com/en-us/library/kyaxdd3x(v = vs.110 ).aspx [ ^ ],

https://msdn.microsoft.com/en-us/library/4ek9c21e(v=vs.110).aspx [ ^ ],

https://msdn.microsoft.com/en-us/library/4d848zkb(v = VS。 110).aspx [ ^ ],

https://msdn.microsoft.com/en-us/library/h70wxday(v = vs.110).aspx [ ^ ],

等......

或从所有成员开始,然后对它们进行分类: https:// msdn.microsoft.com/en-us/library/16z89wsw(v=vs.110).aspx [ ^ ]。



另外,请查看所有其他方法 System.Type 然后从这些方法返回的所有类型,来自 System.Reflection 的类型。这一切都非常简单。



-SA
This is called reflection. Please start here:
https://msdn.microsoft.com/en-us/library/system.type%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/kyaxdd3x(v=vs.110).aspx[^],
https://msdn.microsoft.com/en-us/library/4ek9c21e(v=vs.110).aspx[^],
https://msdn.microsoft.com/en-us/library/4d848zkb(v=vs.110).aspx[^],
https://msdn.microsoft.com/en-us/library/h70wxday(v=vs.110).aspx[^],
and so on…
or start with all members and then classify them: https://msdn.microsoft.com/en-us/library/16z89wsw(v=vs.110).aspx[^].

Also, review all other methods of System.Type and then all the types returned from these methods, the ones from System.Reflection. This is all pretty simple.

—SA


参见 .NET中的反射 [ ^ ]。


这篇关于显示(观察概念)所有对象的详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆