如何从 DLL 中检索程序集信息
How to Retrieve Assembly information from within DLL
我有一个 ASP.Net asmx 网站服务,它在 Properties 文件夹中有一个 AssemblyInfo.cs。我无法访问该文件中的信息。
我的假设是我应该能够调用
Assembly.GetExecutingAssembly();
然后从该文件中获取信息,但是我得到了一些其他程序集。
为了解决这个问题,我将我的程序集属性从那个文件移到了我的 Global.asax.cs 页面中。这样做之后,调用上面的行返回了我的预期值。
1.我必须这样做还是我刚刚查看了一些关于 AssemblyInfo.cs 文件的东西?
该网站还有一个 DLL,它试图使用来自同一个 AssemblyInfo.cs 的信息。我不知道如何从 DLL 中获取我需要的信息。
Assembly.GetEntryAssembly() = null
Assembly.GetCallingAssembly() = mscorlib
Assembly.GetExecutingAssembly() = DLL Assembly
2。如何获取站点的程序集信息?
using System.Runtime.InteropServices;
using GatewayProtocol;
using Steno;
[assembly: Guid("3d5900ae-aaaa-bbbb-cccc-d9e4606ca793")]
//If I remove the above line, the below Debug line **does not** return the expected Guid
namespace GWS
{
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
Logger.Init();
var assembly = Assembly.GetExecutingAssembly();
var attribute = (GuidAttribute)assembly.GetCustomAttributes(typeof(GuidAttribute), true)[0];
Debug.WriteLine(attribute.Value) //3d5900ae-aaaa-bbbb-cccc-d9e4606ca793
}
}
}
在 Logger.Init() 中,它启动一个线程,循环读取队列
var guid = ((GuidAttribute)Assembly.GetExecutingAssembly.GetCustomAttributes(typeof(GuidAttribute), true)[0]).Value;
这个returnsDLL的GUID,不是我想要的
这对我有用:
AssemblyInfo.cs 在网络应用程序中
[assembly: Guid("df21ba1d-f3a6-420c-8882-92f51cc31ae1")]
Global.asax.cs
public class Global : HttpApplication
{
void Application_Start(object sender, EventArgs e)
{
Logger.Init();
string assemblyName = Logger.AssemblyGuid; // to test under debug
}
}
WebService1.asmx.cs
namespace WebApplicationSO2
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public class WebService1 : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld() {
Assembly a = Assembly.GetExecutingAssembly();
var attribute = (GuidAttribute)a.GetCustomAttributes(typeof(GuidAttribute), true)[0];
var guidFromDll = Logger.AssemblyGuid;
return "My Guid: " + attribute.Value + " Guid from Dll: " + guidFromDll; // returns 'My Guid: df21ba1d-f3a6-420c-8882-92f51cc31ae1 Guid from Dll: df21ba1d-f3a6-420c-8882-92f51cc31ae1'
}
}
}
DLL:
namespace ClassLibrary1
{
public class Logger
{
public static string AssemblyGuid;
public static void Init() {
Assembly a = Assembly.GetCallingAssembly();
var attribute = (GuidAttribute)a.GetCustomAttributes(typeof(GuidAttribute), true)[0];
AssemblyGuid = attribute.Value;
}
}
}
我有一个 ASP.Net asmx 网站服务,它在 Properties 文件夹中有一个 AssemblyInfo.cs。我无法访问该文件中的信息。
我的假设是我应该能够调用
Assembly.GetExecutingAssembly();
然后从该文件中获取信息,但是我得到了一些其他程序集。 为了解决这个问题,我将我的程序集属性从那个文件移到了我的 Global.asax.cs 页面中。这样做之后,调用上面的行返回了我的预期值。
1.我必须这样做还是我刚刚查看了一些关于 AssemblyInfo.cs 文件的东西?
该网站还有一个 DLL,它试图使用来自同一个 AssemblyInfo.cs 的信息。我不知道如何从 DLL 中获取我需要的信息。
Assembly.GetEntryAssembly() = null
Assembly.GetCallingAssembly() = mscorlib
Assembly.GetExecutingAssembly() = DLL Assembly
2。如何获取站点的程序集信息?
using System.Runtime.InteropServices;
using GatewayProtocol;
using Steno;
[assembly: Guid("3d5900ae-aaaa-bbbb-cccc-d9e4606ca793")]
//If I remove the above line, the below Debug line **does not** return the expected Guid
namespace GWS
{
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
Logger.Init();
var assembly = Assembly.GetExecutingAssembly();
var attribute = (GuidAttribute)assembly.GetCustomAttributes(typeof(GuidAttribute), true)[0];
Debug.WriteLine(attribute.Value) //3d5900ae-aaaa-bbbb-cccc-d9e4606ca793
}
}
}
在 Logger.Init() 中,它启动一个线程,循环读取队列
var guid = ((GuidAttribute)Assembly.GetExecutingAssembly.GetCustomAttributes(typeof(GuidAttribute), true)[0]).Value;
这个returnsDLL的GUID,不是我想要的
这对我有用:
AssemblyInfo.cs 在网络应用程序中
[assembly: Guid("df21ba1d-f3a6-420c-8882-92f51cc31ae1")]
Global.asax.cs
public class Global : HttpApplication
{
void Application_Start(object sender, EventArgs e)
{
Logger.Init();
string assemblyName = Logger.AssemblyGuid; // to test under debug
}
}
WebService1.asmx.cs
namespace WebApplicationSO2
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public class WebService1 : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld() {
Assembly a = Assembly.GetExecutingAssembly();
var attribute = (GuidAttribute)a.GetCustomAttributes(typeof(GuidAttribute), true)[0];
var guidFromDll = Logger.AssemblyGuid;
return "My Guid: " + attribute.Value + " Guid from Dll: " + guidFromDll; // returns 'My Guid: df21ba1d-f3a6-420c-8882-92f51cc31ae1 Guid from Dll: df21ba1d-f3a6-420c-8882-92f51cc31ae1'
}
}
}
DLL:
namespace ClassLibrary1
{
public class Logger
{
public static string AssemblyGuid;
public static void Init() {
Assembly a = Assembly.GetCallingAssembly();
var attribute = (GuidAttribute)a.GetCustomAttributes(typeof(GuidAttribute), true)[0];
AssemblyGuid = attribute.Value;
}
}
}