Pure Code
Unless you have been developing in a cave, you have most likely heard the term "Safe
Code". But what exactly does this mean? Instead of safe code though, lets talk about
"Pure Code".
Applications can be compiled and executed on the .NET platform, yet still be not
"pure" .NET applications. "Pure" is a subjective term, and not necessarily an official
one. I will define a pure .NET application as having the following attributes:
- 100% Safe Code
- 100% Managed Code
- P/Invoke Free
The ultimate goal of any .NET application is to use 100 % pure code and isolate
all other code into separate assemblies. In a 100% .NET world all applications will
run only pure code. Only device drivers, encryption, compression, and other such
items shall run as unsafe code.
Managed Code
Managed code is code that is executed under the control of the .NET framework. Code
that can be managed by the .NET framework is IL, or Intermediate Language based
code. Unmanaged code contains CPU specific instructions produced by assembly language
mnemonics, or a traditional compiler such as C++, or Delphi (.NET versions).
Languages such as C# and Visual Basic.NET produce only managed code. However Delphi,
and especially C++ can produce mixed applications which contain both managed and
unmanaged code.
Mixed mode applications can integrate with the .NET framework and allow easier phased
migration from Win32. Mixed mode assemblies are also necessary for integrating with
hardware. However, since a mixed mode application contains CPU and operating system
specific information, this ties the .NET application to a specific CPU and operating
system. Such applications therefore use .NET, but are do not run completely under
the .NET framework and are not compatible with other .NET implementations such as
Mono, the 64 bit .NET framework, and other future implementations.
Many server environments have security restrictions that only permit applications
to be run if they are pure managed code. Unmanaged code items must be explicitly
trusted by the administrator and are reserved for device drivers and the like.
In short, if any part of an application is unmanaged, the whole application should
be considered unmanaged. If certain items such as hardware must be integrated with
a managed wrapper is created. All such items should be created and deployed as separate
isolated assemblies for easier management.
Safe Code
Safe code is code that strictly follows the rules of type safety of the .NET framework.
Unsafe code is old code that does not follow such rules. Unsafe code permits use
of pointers, machine specific byte orders, and language specific types. Unsafe code
requires additional permissions to execute.
Assemblies can be checked with a tool called PEVerify to determine if it contains
unsafe code. PEVerify is part of the .NET framework SDK.
|