如何从DLL中检索程序集信息

时间:2015-03-28 19:04:33

标签: c# asp.net .net 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;

这将返回DLL的GUID,这不是我想要的。

1 个答案:

答案 0 :(得分:0)

这对我有用:

Web应用程序中的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;
        }
    }
}