Thursday, May 27, 2010

Assembly Manifest Definition Tips

-A Data Structure responsible to store inforamtion about assembly i.e. metadata.
-metadata contain various information like: assembly's version, security identity and other metadata needed to define the scope of assembly and resolve references to resources and classes.
- can be stored in a PE file (DLL/EXE) with Microsoft Intermediate Language (MSIL) or in standalone PE file that contains only assembly manifest information.
-contains a collection of data that describes how the elements in the assembly relate to each other

Wednesday, March 24, 2010

Managed Modules- Windows Portable Executable (PE)

The above diagram shows the resulting a managed module. A managed module is a standard Windows portable executable (PE) file that requires the CLR to execute. In the future, other operating systems may use the PE file format as well.

A Managed Module is composed of the following parts:





















Part

Description
PE header This is the standard Windows PE file header, which is similar to the Common Object File Format (COFF) header. The PE header indicates the type of file--GUI, CUI, or DLL—and also has a timestamp indicating when the file was built. For modules that contain only IL code (see below, Intermediate Language Code), the bulk of the information in the PE header is ignored. For modules that contain native CPU code, this header contains
information about the native CPU code.
CLR header This header contains the information (interpreted by the CLR and utilities) that makes this a managed module. It includes the version of the CLR required, some flags, the MethodDef metadata token of the managed module’s entry point method (Main method), and the location/size of the module’s metadata, resources, strong name, some flags, and other less interesting stuff.
Metadata Every managed module contains metadata tables, of which there are 2 main types: those that describe the types and members defined in your source code, and those that describe the types and members referenced by your source code.
Intermediate Language (IL) Code This is the code that was produced by the compiler as it compiled the source
code. IL is later compiled by the CLR into native CPU instructions.

Most compilers of the past produced code targeted to a specific CPU architecture, such as x86, IA64, Alpha, or PowerPC. All CLR-compliant compilers produce intermediate language (IL) code instead. IL code is sometimes referred to as managed code, because its lifetime and execution are managed by the CLR.

Metadata is simply a set of data tables that describe what is defined in the module, such as types and their members. Also it has tables indicating what the managed module references, such as imported types and their members. Metadata is a superset of older technologies such as type libraries and IDL files.

The important thing to note is that CLR metadata is far more complete than its predecessors. And, unlike type libraries and IDL, metadata is always associated with the file that contains the IL code. In fact, the metadata is always embedded in the same EXE/DLL as the code, making it impossible to separate the two. Since the metadata and code are produced by the compiler at the same time and are bound into the resulting managed module, the metadata and the IL code it describes are never out of sync with one another.

Metadata has many uses. Here are some of them:
· Metadata removes the need for header and library files when compiling, because all the information about the referenced types/members is contained in one file along with the IL that implements those type/members. Compilers can read metadata directly from managed modules.

· Visual Studio uses metadata to help you write code. Its IntelliSense feature parses metadata to tell you what methods a type offers and what parameters that method expects.

· The CLR code verification process uses metadata to ensure that your code performs only “safe” operations. Verification is discussed shortly.

· Metadata allows an object’s fields to be serialized into a memory block, remoted to another machine, and then deserialized, recreating the object and its state on the remote machine.

· Metadata allows the garbage collector to track the lifetime of objects. For any object, the garbage collector can determine the type of the object, and from the metadata it knows which fields within that object refer to other objects.


Four of the compilers that Microsoft offers--C#, Visual Basic, JScript, and the IL Assembler--always produce managed modules, which require the CLR to execute. That is, end users must have the CLR installed on their machines in order to execute any managed modules. This situation is similar to the one that end users face with MFC or VB 6 applications:
they must have the MFC or VB DLLs installed in order to run them.


But as CLR works with assemblies[ a logical grouping of one or more managed modules or resource files] not with Modules. An assembly is the smallest unit of reuse, security, and versioning, produced as single file assembly or multi-file assembly depending on compilers/tools choice.

The figure below should help explain what assemblies are about:


A single file [managed modules and resource (or data) files] is passed to tools, producing a single PE file that represents the logical grouping of files. This PE file contains a block of data called the manifest, which is simply another set of metadata tables. These tables describe the assembly: the files that make it up, the publicly exported types implemented by the files in the assembly, and the resource or data files that are associated with it.


Tuesday, March 23, 2010

Date Variable

Many times it require to check whether the date in the particular variable is assigned which can be checked by using

If IsDate(variable of type date) Then .......

Also, in case to verify that valid value is stored in the database while retrieving, to avoid null exception, it can be checked by using datareader as

If dr.isdbnull(column index) Then...
Or
If dr(mydate column Name/Index) Is DBNull.Value Then

Usefull tips:
in VB.net empty date is 1/1/1900
to verify correct date, ensure mydate.year > 1900

Monday, May 18, 2009

ListBox Deselect Tip

In ListBox, I've found that we can not deselect a entry once we selected any one of the entry in the list, even selecting no entry area. But I've found one of the solution to fix this issue as :
Let lstBox be the listbox in a form then,

Private Sub lstBox_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lstBox.MouseDown
Try
If Not lstBox.SelectionMode = SelectionMode.None Then
lstBox.SelectedIndex = lstBox.IndexFromPoint(e.X, e.Y)
End If
If (Windows.Forms.Control.ModifierKeys And Keys.Control) <> 0 Then
If lstBox.SelectedIndex = lstBox.IndexFromPoint(e.X, e.Y) Then
LstbIMBoxList.SelectedIndex = -1
End If
End If

Catch ex As Exception
'
End Try
End Sub

This may fix the issue in many situations depending on the requirements.

Tuesday, May 5, 2009

Singleton class

 Public Class MySingletonCls

Private Shared objSingle As MySingletonCls
Private Shared bIsCreated As Boolean

Private Sub New()
'Override the default constructor
End Sub

Public Shared Function CreateInstance() As
MySingletonCls
If bIsCreated = False Then
objSingle = New
MySingletonCls()
bIsCreated = True
Return objSingle
Else
Return objSingle
End If
End Function
End Class

Friday, May 1, 2009

Tip to fetch XML using LINQ

Function FetchXML(ByVal url As Uri) As XElement

Dim x = WebRequest.Create(url)

Using r = x.GetResponse, rs As New StreamReader(r.GetResponseStream)

Return XElement.Parse(rs.ReadToEnd)

End Using

End Function

Call the function with the location of the xml file to get the corresponding XML element, that can be used for needed purpose.

Tip to handle XML based info project

Many times we need to manage the projects that just process specific information. Like if we consider a project that handles information about student(name,rno,course,class). So each student will have this information. To very easy way, if we create an XML file with one of the student. Creating a xml with this info is not very tough job.
Upto this point, half is done. Now we just need to get a class which can carry this information for various purposes and which can be written directly to xml file and also can read data from xml file( easiest way is serialize/deserialize mechanism can be very useful)

Now to get exact class that can be serialized directly to xml can be easily obtained from xsd utility of Visual Studio. just open the Visual Studio Command Prompt, and get the xsd file for your xml file and then convert this file directly to required class file. So the steps that can be needed to get the class are as below:

1. Create xml file say 'MyXml.xml'
2. run xsd MyXml.xml, will result MyXml.xsd
3. run xsd MyXml.xsd /c /l:vb, will result MyXml.vb

[the class can be constructed in various ways by using xsd, for details refer xsd documentation.]

note, in step 2 and 3, if current directory for running the command is same that is containing the MyXml.xml file then use command as such, else specify the full path of the file(s). Also target for the resulting file also can be specified(refer xsd documentation).

now we are with a class for which an object can be easily serialized/deserialized. So create collection of objects to carry all the records, applly any type of manipulations and serialize with XMLSerializer to write it to xml directly without worring about most of the validations as most are handled by serializers.