Tuesday, April 26, 2011

How Binding Works in console application and When Late Binding is Useful?

The binding process involves searching the metadata of the target type for a member (property, field, event, or method) that has a specified name, and which matches a specified set of data types. In the case of a field or property, the specified data type would be the type of the field or property. In the case of a method (including constructors), the specified set of data types would be the types of the method parameters. In the case of an event, no data type matching is required. While the compiler compiles source code, it performs the job of matching the member and data types. The compiler can determine what member type (property, field, event, or method) to search for based on the language syntax and the semantics of the source code context. If the compiler cannot find a matching member, it reports a compiler error. If it finds a match, it stores a reference to the target method in the metadata of the output assembly, as you learned earlier. This reference metadata is used during the JIT compile process to bind the code that is being compiled to the target member. When you bind at run time, a similar process takes place, but with two differences: your code needs to perform the job of the compiler and locate the target member, and the reference metadata of the target member is stored in variables rather than in the assembly’s metadata. This process is known as late binding because it happens later than the compilation stage, which is when binding usually takes place. Do not worry that binding sounds like a difficult programming task. Most of the time, you will use the FCL DefaultBinder class to perform binding. In the FCL standard binding and invocation methods, a DefaultBinder class is automatically used by default. In the rare case that you have special binding requirements, you can provide a replacement for the DefaultBinder class by implementing your own subclass of the abstract Binder class and by using appropriate overloads of the standard binding methods.

When Late Binding is Useful?
Binding on its own is not very useful because it only locates a suitable member in preparation to interact with it. However, after you have bound to a member, you can invoke the member by using various techniques that you will learn about in the remainder of this lesson. What invocation does depends on the member type: for example, if the member is a method, invoking it means calling the method. However, if the member is a field or property, invoking it means setting the field or property value. Late binding is useful when you want to interact with an object but you do not know the object’s type at compile time, or if you cannot add a reference to an assembly at compile time. You would typically use late binding when your application provides add-in functionality. Your application can use late binding to load add-in assemblies (which third-party developers may create) that were potentially created after your application was published. By using reflection and late binding, your application can load an assembly (perhaps specified in an application configuration file), reflect to locate a suitable type to use as an add-in, create a new instance of the type, and then interact with the instance.

No comments:

Recent Posts