请帮忙.-如何获取调用该方法的实例的名称. [英] Please Help.-How do I get the name of instance which called that method.
本文介绍了请帮忙.-如何获取调用该方法的实例的名称.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何获取调用该方法的实例的名称.
How do I get the name of instance which called that method.
class Program
{
static void Main(string[] args)
{
Program p1 = new Program();
Program K1 = new Program();
K1.Print_InstanceName();
p1.Print_InstanceName();
}
public void Print_InstanceName()
{
Console.WriteLine("Object that called this method= "+ /**Get the name of that instance which called this method.**/);
}
预期的输出如下:
调用此方法的对象= p1
调用此方法的对象= K1
Expected output is like following:
Object that called this method= p1
Object that called this method= K1
推荐答案
您不能:如果执行此操作怎么办:
You can''t: what if I do this:
Program p1 = new Program();
Program k1 = p1;
K1.Print_InstanceName();
p1.Print_InstanceName();
您希望打印什么?
在您的示例中,p1和K1不是Program类的实例-它们是引用实例的变量.对象通常不具有Name属性(控件具有),但是即使它们具有对象,它们也不会反映您在代码中为变量指定的名称.
What would you expect to print?
In your example, p1 and K1 are not instances to the Program class - they are variables that reference the instances. Objects generally do not have a Name property (controls do) but even if they did, they would not reflect the name that you give to variables in your code.
这篇关于请帮忙.-如何获取调用该方法的实例的名称.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文