Thursday, September 16, 2010

Anonymous Types

Anonymous types:  As the name itself clears, creates an object without declaring its data type/class. Since name of the data type/class is not specified --that the type is referred to as an anonymous type.

VB.Net
Person = New With {
    .FirstName = "Ram",
    .MiddleName = "Gopal",
    .LastName = "Verma" }
C#
 var Person = New With {
    FirstName = "Ram",
    MiddleName = "Gopal",
    LastName = "Verma" };
Above example is one of  declaration of anonymous typed object where once a person object is created then this object is used as if object of some of real physical class.

During compilation while the control reached at above code then the following are the steps that are performed internally.
  1. Compiler automatically generates anonymous class.
  2. It anonymously instantiate an object that contains the properties in anonymous type
  3. The instantiated object is assigned to the anonymous type variable "Person" 
 The anonymous type inherits from Object, it holds the properties specified in declaring the object.

So object of anonymous  type are used as of a normal class.

Marker Interface

An interface that consists of no method....... or an interface having nothing in the body is called marker interface. These interfaces can be useful in the scenarios while we need to mark some kind of implementations of derived class objects for different kinds of behavior.

e.g Let C be a class

C1 and C2 inherits C and also C2 implements Imarker interface. 
now suppose the object to be realistic in real world and the objects of class that implements Imarker interface are always intelligent entities.

now let 5 objects of C1 and 3 objects of C2 go for competition and clearly objects of C2 are winner. Then to check which object is winner which is not, instead of checking property of individual we just will check which is object is assignable to reference of Imarker interface. which removes lot of checks in case of complex scenarios.

Comparing instance is type of a class

There are various kinds of ways to check whether a object is instance of given class or not. The following are some of the ways that has been identified to be find this condition

Public Interface MyMarkerInterface
 
End Interface
 
Public Class myTestCls
    Implements MyMarkerInterface
         Private _m As Integer
    Public ReadOnly Property Age() As Integer
        Get
            Return _m
        End Get
    End Property
 
End Class

The above is the class that implements some interface........ So in some case I want to check whether the instance of above class is instance of Interface.


    Private Function isMarkerkedObj1(ByVal o1 As myTestClsAs Boolean
        If TypeOf o1 Is MyMarkerInterface Then
            Return True
        Else
            Return False
        End If
    End Function
 
    Private Function isMarkerkedObj2(ByVal o1 As myTestClsAs Boolean
        If o1.GetType.IsSubclassOf(GetType(MyMarkerInterface)) Then
            Return True
        Else
            Return False
        End If
    End Function
 
    Private Function isMarkerkedObj3(ByVal o1 As myTestClsAs Boolean
        If GetType(MyMarkerInterface).IsAssignableFrom(o1.GetType) Then
            Return True
        Else
            Return False
        End If
    End Function
Now all the above methods are using different methods to compare the type of object to check is this type of particular kind .

e.g. Dim obj1 as new myTestCls
-------------
------------- 
-------------
isMarkerkedObj1(obj1)
isMarkerkedObj2(obj1)
isMarkerkedObj3(obj1)

all the above calls will return true.

Note: The requirement discussed might be confusing but is very useful in many scenarios a the interface used is a marker interface which consists of nothing in the body. So in certain cases we may require to mark some objects for certain behavior then we use this interface.

Wednesday, September 1, 2010

What is a Manifest?

All the metadata needed to specify the assembly's version requirements and security identity. Metadata is needed to define the scope of the assembly and resolve references to resources and classes.

It can be stored in either a PE (Portable Executable) file (an .exe or .dll) with Microsoft intermediate language (MSIL) code or in a standalone PE (Portable Executable) file that contains only assembly manifest information.

The information contained in the assembly manifest
I. Assembly's identity: The first four items — 1. the assembly name 2. version number 3. culture 4. and strong name information.

1. Assembly name: A text string specifying the assembly's name.
2. Version number: A major and minor version number, and a revision and build number. The common language runtime uses these numbers to enforce version policy.
3. Culture: Information on the culture or language the assembly supports. This information should be used only to designate an assembly as a satellite assembly containing culture- or language-specific information. (An assembly with culture information is automatically assumed to be a satellite assembly.)
4. Strong name information: The public key from the publisher if the assembly has been given a strong name.

II. List of all files in the assembly: A hash of each file contained in the assembly and a file name. Note that all files that make up the assembly must be in the same directory as the file containing the assembly manifest.

III. Type reference information: Information used by the runtime to map a type reference to the file that contains its declaration and implementation. This is used for types that are exported from the assembly.

IV. Information on referenced assemblies: A list of other assemblies that are statically referenced by the assembly. Each reference includes the dependent assembly's name, assembly metadata (version, culture, operating system, and so on), and public key, if the assembly is strong named.