[英]is it possible in C# to know who called a static property/accessor?
My code: 我的代码:
public class CLASS_A {
public static Dictionary<int, CLASS_A> List = new Dictionary<int, CLASS_A>;
public static PP_CLASS pp = null;
public static CLASS_A ID
{
get
{
int key = get_threadID;
if (List.ContainsKey(key))
return List[key];
else
return null;
}
set
{
int key = get_threadID;
List[key] = value;
}
}
public virtual void init(lib, name)
{
...
if (name != "")
{
if (pp == null)
PP = this;
}
...
}
}
So whichever thread calls init, it's id is used to store this (whoever called). 所以无论哪个线程调用init,它的id都用于存储它(无论谁调用)。 Eg my list looks like this:
我的列表看起来像这样:
45 = CLASS_A_object0
67 = CLASS_A_object1
...
But now when a different thread calls a method on pp, say CLASS_A.pp.setWelcome
, this will return null for pp, and throws null exception! 但是现在当一个不同的线程在pp上调用一个方法时,比如
CLASS_A.pp.setWelcome
,这将为pp返回null,并抛出null异常! Because when set is called the thread id will be different and won't be in the list. 因为当调用set时,线程id将不同,并且不会在列表中。
So is it possible that I know which object called so that I can do reverse lookup? 那么有可能我知道哪个对象被调用以便我可以进行反向查找吗? Or maybe a different solution?
或者可能是另一种解决方案
Why I want this: Initially we were connecting to one device so that was ok. 为什么我想要这个:最初我们连接到一个设备,这样就可以了。 Now there are multiple devices with each one having its own ip/port.
现在有多个设备,每个设备都有自己的ip /端口。 Initial code had just
public static PP_CLASS pp = null;
初始代码只有
public static PP_CLASS pp = null;
So others will be just calling methods on pp using class name and things were good. 所以其他人只会使用类名调用pp上的方法,事情很好。
Previous behaviour: The software picks list of devices from a file and since pp is static it only talks to the first device. 以前的行为:软件从文件中选择设备列表,因为pp是静态的,它只与第一个设备通信。 I added that
pp==null
line which i forgot in my initial post. 我添加了
pp==null
line,我忘记了我的帖子。 So when the code starts pp==null
will be true and first device is assigned, but now for other devices pp==null
will be false and thus I am not able to talk to other devices. 因此,当代码启动时,
pp==null
将为真,并且第一个设备已分配,但现在对于其他设备, pp==null
将为false,因此我无法与其他设备通信。
Please let me know if more details are needed. 如果需要更多详细信息,请与我们联系。
Beginning with C# 5.0 (August 2012) there is a new feature " Caller Info Attribute ". 从C#5.0开始(2012年8月),有一个新功能“ 来电者信息属性 ”。 If your classes are stored in separate files, you can make use of the
CallerFilePathAttribute
to register which class actually has called. 如果您的类存储在单独的文件中,则可以使用
CallerFilePathAttribute
来注册实际调用的类。
Example from MSDN : 来自MSDN的示例:
// using System.Runtime.CompilerServices
// using System.Diagnostics;
public void DoProcessing()
{
TraceMessage("Something happened.");
}
public void TraceMessage(string message,
[CallerMemberName] string memberName = "",
[CallerFilePath] string sourceFilePath = "",
[CallerLineNumber] int sourceLineNumber = 0)
{
Trace.WriteLine("message: " + message);
Trace.WriteLine("member name: " + memberName);
Trace.WriteLine("source file path: " + sourceFilePath);
Trace.WriteLine("source line number: " + sourceLineNumber);
}
// Sample Output:
// message: Something happened.
// member name: DoProcessing
// source file path: c:\Users\username\Documents\Visual Studio 2012\Projects\CallerInfoCS\CallerInfoCS\Form1.cs
// source line number: 31
You could try examining the Stack Trace. 您可以尝试检查堆栈跟踪。
var trace = new System.Diagnostics.StackTrace();
or to get the calling line only it should be something like: 或者只获取调用行应该是这样的:
var caller = new System.Diagnostics.StackTrace().GetFrame(1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.