为什么Assembly.GetExecutingAssembly在NuGet包内返回不同的结果?

问题描述

我是创建NuGet包的新手,并且在各种环境中运行了以下代码段:

/// <summary>
/// Tries to find type by name in the executing assembly and after that
/// in referenced assemblies.
/// </summary>
/// <param name="typeName">Name of the type to find (can be full or assembly qualified name as well).</param>
/// <returns>Type found using the given name (or null if not found).</returns>
public static Type FindType(string typeName)
{
    if (typeName == null) throw new ArgumentNullException(nameof(typeName));

    // Helper method for finding the type in an assembly
    Type Finder(Assembly ass) => ass?.GetTypes().FirstOrDefault(type =>
        typeName.In(type.Name,type.FullName,type.AssemblyQualifiedName)
    );

    // Get the current assembly
    var executingAssembly = Assembly.GetExecutingAssembly();

    // Check if the type is inside the current assembly
    var targetType = Finder(executingAssembly);

    // Go through all of the referenced assemblies
    foreach (var assName in executingAssembly.GetReferencedAssemblies())
    {
        // If the type was found,return it
        if (targetType != null)
            return targetType;

        // Check if the type is inside the assembly
        targetType = Finder(Assembly.Load(assName));
    }

    return null; // Type wasn't found,return null
}

如果我将其作为本地函数或通过引用的项目运行,则可以正常工作,但是当我创建NuGet包并使用NuGet包内部方法的实现调用该方法时,它将返回null。

方法Assembly.GetExecutingAssembly声称它返回了The assembly that contains the code that is currently executing,但是从NuGet程序包运行它时却得到了不同的结果。

如果将方法包装在NuGet包中,该如何从方法中获取正确的输出?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...