Should I mingle my safe code with my unsafe code?

China☆狼群 提交于 2019-12-05 04:31:19

An assembly is by definition the smallest unit of independently versionable, redistributable code in .NET. Therefore the first question I would ask myself is "am I ever going to want to make a new version of the unsafe code and distribute it independently of my application?"

If the answer is yes, then by all means, put that code in its own assembly.

If the answer is no, then consider other factors. For example, in "classic" .NET security you can have different assemblies running at different levels of trust in the same appdomain. (In the modern CLR the system is much simpler, there is just fully trusted code and everything else runs at the level of trust granted to the appdomain.) Your code which does something unsafe needs to be fully trusted, but the code which simply calls it could be less trusted if the unsafe code asserts the correct set of permissions.

Are you ever going to be in a situation where your safe code is partially trusted? If so, then by all means, put the unsafe code in its own assembly, and then document that this assembly must be fully trusted.

If you're not in those sorts of situations then I would not be inclined to put my unsafe code in its own assembly.

I would however be inclined to writing a pleasant managed object model on top of my unmanaged code, rather than simply exposing raw win32 calls. For example, the other day I needed to call some of the strong name verification win32 apis from C# and I just whipped up a little library that exposed them nicely, abstracting away all the nasty interop details to the private interior of the class.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!