C# - 获取调用方法的程序集?

45
在C#中,是否有一种方法可以获取调用方法的程序集?(而不是当前方法)
例如,我想要执行堆栈中上面的程序集。

3
更改标签;[assembly] 用于汇编语言问题。 - Stephen Canon
6个回答

62

试试这个

Assembly.GetCallingAssembly();

我想要调用方法的汇编代码,而不是当前方法的。 - David Pfeffer
13
该错误会返回调用方法的程序集,而非当前方法的程序集。GetExecutingAssembly 方法可返回当前方法的程序集。Stecya 是正确的。 - Rob Levine
7
内联可能会导致此操作不可靠 - 请参见http://msdn.microsoft.com/en-us/library/system.reflection.assembly.getcallingassembly.aspx中的备注部分。 - Joe
9
那就不要将它内联 :) [MethodImplAttribute(MethodImplOptions.NoInlining)] - MadSkunk
2
还要注意尾调用。例如,return Assembly.GetCallingAssembly();就是一个尾调用,编译器可能会对其进行优化,从而返回意外的结果。尾调用优化不能通过属性来防止,只能避免在返回语句中直接“内联”调用函数。 - Matt

15

2
当JIT内联方法时,这将会出现错误。 - JaredPar
JIT 何时内联方法? - Piotr Kula
这是一种JIT优化,因此它根据其内部实现来决定。但是可以通过属性来防止内联:https://dev59.com/oW435IYBdhLWcg3w3EEi - RQDQ

7

这是我使用的工具:

        var stackFrames = new StackTrace().GetFrames();
        if(stackFrames==null) return null;
        var executingAssembly = Assembly.GetExecutingAssembly();
        foreach (var frame in stackFrames)
        {
            var assembly = frame.GetMethod().DeclaringType.Assembly;
            if (assembly != executingAssembly)
            {
                return assembly;
            }
        }
        return null;

6

很抱歉,无法可靠地了解是谁在给您打电话。有些人可能会建议使用堆栈跟踪,但由于JIT内联,这是不可靠的。没有可靠的方法/程序集可以获取调用您方法的方法/程序集。


4
"Stecya之前向我提到了Assembly.GetCallingAssembly(),这个怎么办呢?" - David Pfeffer

1
我写了这个程序来获取调用程序集的AssemblyInfo.cs属性--所以使用了"GetAssembly(int stackTraceLevel)"方法来完成所需操作...
所有这些内容也可以在我的博客http://lancelarsen.com/reading-values-from-assemblyinfo-cs/上找到。
祝愉快。
/// <summary>
/// Gets the values from the AssemblyInfo.cs file for the previous assembly
/// </summary>
/// <example>
/// AssemblyInfoCalling assembly = new AssemblyInfoCalling();
/// string company1 = assembly.Company;
/// string product1 = assembly.Product;
/// string copyright1 = assembly.Copyright;
/// string trademark1 = assembly.Trademark;
/// string title1 = assembly.Title;
/// string description1 = assembly.Description;
/// string configuration1 = assembly.Configuration;
/// string fileversion1 = assembly.FileVersion;
/// Version version1 = assembly.Version;
/// string versionFull1 = assembly.VersionFull;
/// string versionMajor1 = assembly.VersionMajor;
/// string versionMinor1 = assembly.VersionMinor;
/// string versionBuild1 = assembly.VersionBuild;
/// string versionRevision1 = assembly.VersionRevision;
/// </example>
public class AssemblyInfoCalling
{
    /// <summary>
    /// Initializes a new instance of the <see cref="AssemblyInfoCalling"/> class.
    /// </summary>
    /// <param name="traceLevel">The trace level needed to get correct assembly 
    /// - will need to adjust based on where you put these classes in your project(s).</param>
    public AssemblyInfoCalling(int traceLevel = 4)
    {
        //----------------------------------------------------------------------
        // Default to "3" as the number of levels back in the stack trace to get the 
        //  correct assembly for "calling" assembly
        //----------------------------------------------------------------------
        StackTraceLevel = traceLevel;
    }

    //----------------------------------------------------------------------
    // Standard assembly attributes
    //----------------------------------------------------------------------
    public string Company { get { return GetCallingAssemblyAttribute<AssemblyCompanyAttribute>(a => a.Company); } }
    public string Product { get { return GetCallingAssemblyAttribute<AssemblyProductAttribute>(a => a.Product); } }
    public string Copyright { get { return GetCallingAssemblyAttribute<AssemblyCopyrightAttribute>(a => a.Copyright); } }
    public string Trademark { get { return GetCallingAssemblyAttribute<AssemblyTrademarkAttribute>(a => a.Trademark); } }
    public string Title { get { return GetCallingAssemblyAttribute<AssemblyTitleAttribute>(a => a.Title); } }
    public string Description { get { return GetCallingAssemblyAttribute<AssemblyDescriptionAttribute>(a => a.Description); } }
    public string Configuration { get { return GetCallingAssemblyAttribute<AssemblyDescriptionAttribute>(a => a.Description); } }
    public string FileVersion { get { return GetCallingAssemblyAttribute<AssemblyFileVersionAttribute>(a => a.Version); } }

    //----------------------------------------------------------------------
    // Version attributes
    //----------------------------------------------------------------------
    public static Version Version
    {
        get
        {
            //----------------------------------------------------------------------
            // Get the assembly, return empty if null
            //----------------------------------------------------------------------
            Assembly assembly = GetAssembly(StackTraceLevel);
            return assembly == null ? new Version() : assembly.GetName().Version;
        }
    }
    public string VersionFull { get { return Version.ToString(); } }
    public string VersionMajor { get { return Version.Major.ToString(); } }
    public string VersionMinor { get { return Version.Minor.ToString(); } }
    public string VersionBuild { get { return Version.Build.ToString(); } }
    public string VersionRevision { get { return Version.Revision.ToString(); } }

    //----------------------------------------------------------------------
    // Set how deep in the stack trace we're looking - allows for customized changes
    //----------------------------------------------------------------------
    public static int StackTraceLevel { get; set; }

    //----------------------------------------------------------------------
    // Custom Attributes
    //----------------------------------------------------------------------
    public string Location 
    { 
        get
        {
            try
            {
                return GetCallingAssemblyAttribute<AssemblyLocationAttribute>(a => a.Value);
            }
            catch (NullReferenceException)
            {
                return string.Empty;
            }

        } 
    }

    /// <summary>
    /// Gets the calling assembly attribute.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="value">The value.</param>
    /// <example>return GetCallingAssemblyAttribute&lt;AssemblyCompanyAttribute&gt;(a => a.Company);</example>
    /// <returns></returns>
    private string GetCallingAssemblyAttribute<T>(Func<T, string> value) where T : Attribute
    {
        //----------------------------------------------------------------------
        // Get the assembly, return empty if null
        //----------------------------------------------------------------------
        Assembly assembly = GetAssembly(StackTraceLevel);
        if (assembly == null) return string.Empty;

        //----------------------------------------------------------------------
        // Get the attribute value
        //----------------------------------------------------------------------
        T attribute = (T) Attribute.GetCustomAttribute(assembly, typeof (T));
        return value.Invoke(attribute);
    }

    /// <summary>
    /// Go through the stack and gets the assembly
    /// </summary>
    /// <param name="stackTraceLevel">The stack trace level.</param>
    /// <returns></returns>
    private static Assembly GetAssembly(int stackTraceLevel)
    {
        //----------------------------------------------------------------------
        // Get the stack frame, returning null if none
        //----------------------------------------------------------------------
        StackTrace stackTrace = new StackTrace();
        StackFrame[] stackFrames = stackTrace.GetFrames();
        if (stackFrames == null) return null;

        //----------------------------------------------------------------------
        // Get the declaring type from the associated stack frame, returning null if nonw
        //----------------------------------------------------------------------
        var declaringType = stackFrames[stackTraceLevel].GetMethod().DeclaringType;
        if (declaringType == null) return null;

        //----------------------------------------------------------------------
        // Return the assembly
        //----------------------------------------------------------------------
        var assembly = declaringType.Assembly;
        return assembly;
    }
}

0

在寻找类似内容时发现了这个。在我的情况下,是使用 Assembly.GetEntryAssembly()

用例

程序 A 运行 CodeDomProvider.CompileAssemblyFromSource 来嵌入资源并编译程序 B。程序 B 中的 DLL 读取资源,但不知道程序集(上述问题得到解决)。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接