开发者

get information on an assembly

I want to get information on an Assembly in my C# application. I use the following:

Assembly.GetCallingAssembly();

This works perfectly returning information on the calling Assembly.

I want to share this functionality with other applications, so I include this in a class in my class library.

I reference this class library in multiple applications. When I make a call to this method from my applications, it returns informat开发者_运维问答ion on the class library and not the application. Is there a way I can alter my above code to return information on the web applications assembly while still having the code included in the class library?


Instead of having the class library being intelligent why don't you have the caller pass an Assembly as argument to the method? So when you call the method from within some application you would pass Assembly.GetExecutingAssembly() and the method within the class library will now be able to fetch the assembly of the actual caller.


I'm not sure what you provide on top of reflections, but maybe you're abstracting a facility that doesn't need to be abstracted. Reflections already handles this, so why not let it do its job?

But if this API gives you back some useful information (such as plugging nicely into your database, etc), then maybe your approach makes some sense. But I still recommend that you refactor it:

  • Put this code in only one shared project/assembly, and just link that project/assembly when you need to call that functionality. Needing to duplicate code to get your job done is considered code smell.
  • Take an Assembly object as a parameter, rather than trying to determine the current assembly. This will allow more flexibility in case you come up with some code that wants to get data on a bunch of other assemblies, and will still allow you to pass the current assembly. (Note: Darin already made this point)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜