C# Getting Parent Assembly Name of Calling Assembly

前端 未结 9 1301
轮回少年
轮回少年 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:30

    This will return the the initial Assembly that references your currentAssembly.

    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();
    

提交回复
热议问题