What are the implications of using unsafe code

ぃ、小莉子 提交于 2019-12-04 23:51:36

Unsafe code is not verifiable, so you have to be aware of that. In a Full Trust environment, that's not a big deal, but if you have other environments which have a more restricted permission set, then this might impact you there.

You can put the implications into two buckets.

The first is how it affects your application environment. Using unsafe code requires that your assembly be run in a full trust environment. It's not possible to run in a restricted environment such as certain Click Once security settings. The reason being that unsafe code prevents the CLR from ensuring type safety. Click Once though with no security restrictions should not have a problem.

The second is what it means for the way you code. Using unsafe code typically involves using pointers and in particular, using them to performed advanced marshalling via PInvoke. There is nothing inherently wrong with either of these actions though. It just requires significantly more understanding of the CLR and marshalling than "safe" code does. Object pinning is a great example of knowledge you'd need to have a firm grasp on before you started using these features.

To add to Jared's reference to object pinning...

When using pointers to access memory directly in C#, you are vulnerable to the CLR moving an object around in memory at runtime. This means that your pointer may all of a sudden point at the wrong section of memory. The Fixed keyword will pin the object in memory so that this problem may be avoided.

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