DOT NET FAQ'S ON GARBAGE COLLECTION

What is Delay signing ?

During development process you will need strong name keys to be exposed to developer which
is not a good practice from security aspect point of view.In such situations you can assign the key
later on and during development you an use delay signing

Following is process to delay sign an assembly:

√ First obtain your string name keys using SN.EXE.

√ Annotate the source code for the assembly with two custom attributes from System.Reflection: AssemblyKeyFileAttribute, which passes the name of the file containing the public key as a parameter to its constructor. AssemblyDelaySignAttribute, which indicates that delay signing, is being used by passing true as a parameter to its constructor. For example as shown below:

[Visual Basic]




[C#]

[assembly:AssemblyKeyFileAttribute("myKey.snk")]
[assembly:AssemblyDelaySignAttribute(true)]

The compiler inserts the public key into the assembly manifest and reserves space in the PE file for the full strong name signature. The real public key must be stored while the assembly is built so that other assemblies that reference this assembly can obtain the key to store in their own assembly reference.

√ Because the assembly does not have a valid strong name signature, the verification of that signature must be turned off. You can do this by using the –Vr option with the Strong Name tool.The following example turns off verification for an assembly called myAssembly.dll.

Sn –Vr myAssembly.dll

Just before shipping, you submit the assembly to your organization's signing authority for the actual strong name signing using the –R option with the Strong Name tool.

The following example signs an assembly called myAssembly.dll with a strong name using the sgKey.snk key pair.

Sn -R myAssembly.dll sgKey.snk

What is garbage collection?

Garbage collection is a CLR feature which automatically manages memory. Programmers forget
to release the objects while coding ..... Laziness (Remember in VB6 where one of the good practices is to set object to nothing). CLR automatically releases objects when they are no longer in use and refernced. CLR runs on non-deterministic to see the unused objects and cleans them. One side effect of this non-deterministic feature is that we cannot assume an object is destroyed when it goes out of the scope of a function.

we should avoid using destructors because before GC destroys the object it first executes destructor in that case it will have to wait for code to release the umanaged resource. resultin in additional delays in GC. So its recommended to implement IDisposable interface and write cleaup code in Dispose method and call GC.SuppressFinalize method so instructing GC not to call your constructor.

Can we force garbage collector to run ?

System.GC.Collect() forces garbage collector to run. This is not recommended but can be used if
situations arises.

What is reflection?

All .NET assemblies have metadata information stored about the types defined in modules. This
metadata information can be accessed by mechanism called as “Reflection”.System. Reflection
can be used to browse through the metadata information.

Using reflection you can also dynamically invoke methods using System.Type.Invokemember.
Below is sample source code if needed you can also get this code from CD provided, go to
“Source code” folder in “Reflection Sample” folder.

Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim Pobjtype As Type
Dim PobjObject As Object
Dim PobjButtons As New Windows.Forms.Button()
Pobjtype = PobjButtons.GetType()

For Each PobjObject In Pobjtype.GetMembers
LstDisplay.Items.Add(PobjObject.ToString())
Next
End Sub
End Class

Sample source code uses reflection to browse through “Button” class of “Windows.Forms”. If
you compile and run the program following is output as shown in “Sample Reflection Display”.
Using reflection you can also dynamically invoke a method using

“System.Type.InvokeMember”.

RELATED POST

FAQ'S ON MANIFEST

48.9

No comments:

Post a Comment