C# - Get calling method's Assembly?

前端 未结 5 806
时光取名叫无心
时光取名叫无心 2020-12-16 09:27

Is there a way in C# to get the Assembly of the calling method? (Not the current method.)

i.e. I want the executing assembly, one a

5条回答
  •  爱一瞬间的悲伤
    2020-12-16 10:03

    This is what I use:

            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;
    

提交回复
热议问题