C# Getting Parent Assembly Name of Calling Assembly

前端 未结 9 1376
轮回少年
轮回少年 2021-01-01 09:02

I\'ve got a C# unit test application that I\'m working on. There are three assemblies involved - the assembly of the C# app itself, a second assembly that the app uses, and

9条回答
  •  情书的邮戳
    2021-01-01 09:40

    This works for getting the original assembly when using two assemblies in an NUnit test, without returning a NULL. Hope this helps.

    var currentAssembly = Assembly.GetExecutingAssembly();
    var callerAssemblies = new StackTrace().GetFrames()
            .Select(x => x.GetMethod().ReflectedType.Assembly).Distinct()
            .Where(x => x.GetReferencedAssemblies().Any(y => y.FullName ==     currentAssembly.FullName));
    var initialAssembly = callerAssemblies.Last();
    

提交回复
热议问题