c# 将变量设置到调用者程序集引用的 DLL 中

问题描述 投票:0回答:1

我有一个DLL共享到2个项目(到同一个解决方案中),这个DLL是其他2个项目使用的函数库,第一个是Windows服务(exe文件),第二个是WebService(DLL文件) )

现在,更具体地说,函数需要知道谁是调用者来自定义路径

有一种方法可以在调用者项目中设置一个变量来指示被调用的 DLL?

我尝试使用 Assembly.GetCallingAssembly().FullName 但如果它从 EXE 和 DLL 调用,它的工作方式会有所不同,而且无论如何看起来都不干净

提前致谢

c# web-services windows-services
1个回答
0
投票

我用一个“技巧”解决了:),我必须感谢@yacc让我找到正确的路径...

public static string GetCallingMethod()
    {
         string retval = "";

         StackTrace st = new StackTrace();
         StackFrame[] frames = st.GetFrames();
         foreach (StackFrame sFrame in frames)
         {
            if (sFrame.GetMethod().ReflectedType.FullName.StartsWith("MyAssembly"))
            {
                retval = sFrame.GetMethod().ReflectedType.FullName;
            }
         }

         return retval;
    }

retval 将包含程序集调用者的全名,在我的项目中,两个名称都以“MyAssembly”开头

谢谢大家的帮助

© www.soinside.com 2019 - 2024. All rights reserved.