Monday, May 21, 2007

Built-In Attributes

What are the Built-In Attributes in .NET?
.NET Framework includes a number of attributes such as:
· DebuggableAttribute
· AssemblyTitleAttribute
· System.Diagnostics.ConditionalAttribute
· System.ObsoleteAttribute
· System.SerializableAttribute
· System.Reflection.AssemblyDelaySignAttribute


Explain the System.Diagnostics.ConditionalAttribute?
It permits sections of code to be included or excluded based on the definition of a symbol at compilation time. This attribute is contained within the System.Diagnostics namespace, which includes classes for debug, trace output, event logging, performance counters, and process information.
Notice that the defaults for a Debug build are to check both the DEBUG and TRACE options.

Note: the Conditional attribute can be used only on methods that return void – otherwise removing the call world mean that no value was returned; however, you can attribute a method that has out or ref parameters – the variables will retain their original value.

Explain the System.ObsoleteAttribute?
The Obsolete attribute can be used to mark a class, method, or any other entry in an assembly as being no longer used.
This attribute would be useful for ex, when publishing a library of classes. The Obsolete attribute provides a useful way to hint that a particular feature of your classes should no longer be used.

To do this all you should do is add the Obsolete attribute as shown here:
[Obsolete (“newname instead.”)]
public void oldname()
{
}
When you compile for debug or release, you will receive a warning from the C# compiler that you are using an obsolete method as follows:
Obsolete.cs(20, 1): warning CS0618: ‘Developer.oldname()’ is obsolete: ‘newname instead’.

Eventually, if you want to entirely drop support for oldname() method, so you need to add an extra parameter to the Obsolete attribute as follows:
[Obsolete (“you must newname instead.”, true)]
public void oldname()
{
}
When user attempts to compile this method, the compiler will generate an error and halt the compilation with the following message:
Obsolete.cs(20, 1): error CS0619: ‘Developer.oldname()’ is obsolete: ‘you must newname instead’.

What is Serialization?
Serialization is the name for storing and retrieving an object, with in a disk file, memory, or anywhere else you can think of. When serialized, all instance data is persisted to the storage medium, and when deserialized, the object is reconstructed and is indistinguished from its original instance.

What is Deserialization?
The data is read from the storage medium, and this data is assigned to instance variables of the class.
You can mark the NonSerializable attribute to mark data that does not need to be serialized, such as data that can be recomputed or calculated when necessary
To do this all you should do is add the Serializable attribute as shown here:
[Serializable]
public class Person()
{
public Person()
{
}
public int Age;

[NonSerializable]
public int WeightInPounds;
}

Explain the System.SerializableAttribute?
The SerializableAttribute is used for storing and retrieving instance data. All you need to do is add the Serializable attribute to the class, and the .NET runtime will do the rest for you.
When the runtime receives a request to serialize an object, it checks if the object’s class implements the ISerializable interface, and if it does not then the runtime checks if the class is attributed with the Serializable attribute.

If the Serializable attribute is found on the class, the .NET uses reflection to get all instance data whether public, private, or protected and to store this as the representation of the object.

To do this all you should do is add the Serializable attribute as shown here:
[Serializable]
public class Person()
{
public Person()
{
}
public int Age;
public int WeightInPounds;
}

When do you use the Formatter Object?
To store an instance of the class, use a Formatter Object – which converts the data stored within your class into a stream of bytes. The system comes with two default formatters and these Formatters have their own namespaces as follows:
· BinaryFormatter à System.Runtime.Serialization.Formatters.Binary
· SoapFormatter à System.Runtime.Serialization.Formatters.Binary

Explain the System.Reflection.AssemblyDelaySignAttribute?
The .NET Framework also permits you to delay-sign an assembly, which means that you can register it in the GAC for testing without a private key.
One scenario in which you might use delayed signing is when developing commercial software. Each assembly that is developed in-house needs to be signed with your company’s private key before being shipped to your customers. So when you compile your assembly you reference the key file before you can register the assembly in the GAC.
AssemblyDelaySign attribute defines whether the assembly will be fully signed or delay signed

What is the AssemblyKeyFile?
The AssemblyKeyFile attribute defines the file where the key is to be found. This can be either the public key file or the file containing both the public and private keys.

Registering in the GAC:
Attempting to use the Gacutil tool to register a delay-signed assembly in the GAC will generate an error message similar to this:
Microsoft ® .NET Global Assembly Cache Utility. Version 2.0.50214.0
Copyright © Microsoft Corporation. All rights reserved.

Failure adding assembly to the cache: Strong name signature could not be verified. Was the assembly built delay-signed.
The assembly is only partially signed at the moment, and by default the GAC and VS.NET will only accept assemblies with a complete strong name. You can however, instruct the GAC to skip verification of the strong name on a delay signed assembly by using the sn utility.

No comments: