google ads

Sunday, May 31, 2009

CODE ACCESS SECURITY

The CLR is also burdened with the responsibility of security. An integral
part of the .NET Runtime is something called Code Access Security
(CAS), which associates a certain amount of “trust” with code, depending
on the code’s origins (the local file system, intranet, Internet, etc.).
The CLR is responsible for making sure that the code it executes stays
within its designated security boundaries. This could include such
things as reading and writing files from the user’s hard drive, making
registry entries, and so forth.
You can modify the permissions that are granted to code from a certain
location using a utility called CASPOL.EXE. You could specify, for
example, that all code originating from www.codenotes.com be granted
more privileges than other code that comes from the Internet. Examples
of CASPOL.EXE and an in-depth discussion of Code Access Security
can be found at aNET010006.

Topic: .NET Runtime Classes
In the example earlier in this chapter, all three languages used the Console.
WriteLine() method to print “Hello World” to the screen. The
.NET Runtime classes eliminate the need to master a different set of
APIs for different languages. Instead, developers need only familiarize
themselves with the appropriate Runtime classes and then call them
from the language of their choice.
The .NET Runtime includes classes for many programmatic tasks,
including data access, GUI design, messaging, and many more. It also
acts as a wrapper around the Win32 API, eliminating the need to directly
communicate with this cryptic C-style interface. The most difficult part
of using the Runtime is figuring out which class you need to accomplish
the task at hand. A complete list of the .NET Runtime classes can be
found at aNET010007.

NAMESPACES
The .NET Runtime classes are organized in hierarchical manner using
namespaces. Namespaces provide a scope (or container) in which types
are defined. All of the .NET Runtime classes, for example, can be found
in the System namespace. In the “Hello World” example we had to in-
form the compiler that the Console class could be found in the System
namespace by qualifying it (System.Console). Namespaces can also be
nested. The System.IO namespace, for example, contains a number of
classes for I/O operations, whereas the System.Collections namespace
contains classes for common data structures such as arrays.
In the Hello World example we directly addressed the namespace.
You will frequently see code that uses implicit namespace referencing to
make it more concise. Each language uses a different keyword to include
the contents of a namespace. We could have written the Hello
World program in VB.NET as follows:
'VB.NET "Hello World" Program.

Imports System
Module HelloWorld
Sub Main
'Use the .NET Runtime Console method WriteLine,
'to output "Hello World" on the screen:
Console.WriteLine("Hello World!")
End Sub
End Module
Listing 1.4 VB.NET Hello World program using namespaces
Notice that we added the Imports System line and no longer have to
qualify the Console object as System.Console. In C#, you can perform
the same action with the “using” keyword:
// C# "Hello World" Program.
// Implicit namespace referencing
using System;
public class HelloWorld {
static public void Main () {
Console.WriteLine("Hello World.");
}
}
Listing 1.5 C# Hello World program using namespaces
As you can see, implicitly referencing namespaces can save you a lot of
typing and make your code easier to read. You will use namespaces
throughout the .NET framework to:
An Introduction to the .NET Framework . 13
• Access the .NET Runtime classes
• Access custom classes authored by other developers
• Provide a namespace for your own classes, to avoid naming conflicts
with other classes and namespaces
We will use namespaces throughout this CodeNote as we develop our
own .NET components.

No comments:

Post a Comment