Metadata,reflection, late binding [Resolved]

Posted by krrishbiju-15589 under C# on 9/4/2013 | Points: 10 | Views : 1958 | Status : [Member] | Replies : 6
Hi,
Please help me to understand about metadata,reflection, late binding with example.
Regards
krrish




Responses

Posted by: Bandi on: 9/4/2013 [Member] [MVP] Platinum | Points: 50

Up
0
Down

Resolved
Reflection objects are used for obtaining type information at runtime. The classes that give access to the metadata of a running program are in the System.Reflection namespace.

The System.Reflection namespace contains classes that allow you to obtain information about the application and to dynamically add types, values and objects to the application.

Uses of Reflection
Reflection has the following uses:
It allows view attribute information at runtime.
It allows examining various types in an assembly and instantiate these types.
It allows late binding to methods and properties
It allows creating new types at runtime and then performs some tasks using those types.

Examples:
// Using GetType to obtain type information: 

int i = 42;
System.Type type = i.GetType();
System.Console.WriteLine(type);


The output is: System.Int32

2) The following example uses reflection to obtain the full name of the loaded assembly.
// Using Reflection to get information from an Assembly:

System.Reflection.Assembly info = typeof(System.Int32).Assembly;
System.Console.WriteLine(info);


The output is: mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

Refer the following link for Viewing Metadata ( attribute information) by using System.reflection namespace example
http://www.tutorialspoint.com/csharp/csharp_reflection.htm

Metadata:
"Metadata is data that describes the state of the assembly and a detailed description of each type, attribute within the assembly".

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

krrishbiju-15589, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/4/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Late Binding:
What is Late Binding?
Binding is the process of locating the declaration (that is, the implementation) that corresponds to a uniquely specified type. When this process occurs at run time rather than at compile time, it is called late binding.

Definition for Late binding:
The decision to which version of the method to invoke cannot be made at compile time. That decision must be deferred and made at runtime. This is sometimes referred to as late binding.
i.e.
Each .NET program assembly have Metadata which allows the user to dynamically load an assembly and execute methods of it, this is called Late Binding.

This Late Binding requires the use of Reflection in C# and VB.NET. In VB.NET it is optional because we have implicit late binding which allows us to have Late Binding without the use of Refelection.

Refer this link for example:
http://smartypeeps.blogspot.in/2006/06/late-binding-in-c-and-vbnet.html
http://www.akadia.com/services/dotnet_late_binding.html

Early Binding Versus Late Binding
Early binding happens at compile time and the compiler has a chance to verify that all objects or types match. As a result you have a chance to catch a type mismatch at compile time. That doesn't work with all code. During late binding you may have a runtime object that you now have to convert to a specific object type (reflection), this would be an example of late binding. It's not always possible, but for the most part you will want to stick with early binding to avoid runtime errors.

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

krrishbiju-15589, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: krrishbiju-15589 on: 9/4/2013 [Member] Starter | Points: 25

Up
0
Down
Hi Chandu,
Do we require reflection for early binding in c#........
Regards
krrish

krrishbiju-15589, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/4/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
As per my knowledge reflection mechanism is not required for early binding

One example for Late Binding with Reflection
http://www.c-sharpcorner.com/UploadFile/samhaidar/LateBindingWithReflection09122005053810AM/LateBindingWithReflection.aspx

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

krrishbiju-15589, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: krrishbiju-15589 on: 9/5/2013 [Member] Starter | Points: 25

Up
0
Down
Hi Chandu,
Thank you for your reply...
I have one more doubt regarding reflection and atributes in C#.
I heard that reflection and atributes are extracting information from metadata in run time .Is it correct ? can you explain the role of
reflection and atributes in the case of using metadata.

Regards
krrish

krrishbiju-15589, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/5/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Attributes are a mechanism for adding metadata, such as compiler instructions and other data about your data, methods, and classes, to the program itself. Attributes are inserted into the metadata and are visible through ILDasm and other metadata-reading tools.

Reflection is the process by which a program can read its own metadata. A program is said to reflect on itself, extracting metadata from its assembly and using that metadata either to inform the user or to modify its own behavior.

Check the above highlighted part... Attributes are used to add/insert Metadata whereas Reflection is a process of extracting metadata...
For the attributes in the metadata usage, you need a way to access them--ideally during runtime. That way is called as reflection.
This link provides you the further information on Attributes & Metadata...
http://oreilly.com/catalog/progcsharp/chapter/ch18.html

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

krrishbiju-15589, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response