Wednesday, April 20, 2011

Late Binding by Using FCL Methods in console application.

The features of using FCL methods are:
•    They are a more complex (and more flexible) way to perform late binding.
•    They are suitable when you must determine what member to call at run time (for example from a configuration file or by reflection).
•    They can carry a significant performance penalty unless you optimize.

To let bind by using FCL methods:
1.    Create a type instance.
2.    Determine the member that you should invoke.
3.    Use invoke member (slow) or one of the method info subclass methods (faster, no binding required) to invoke the member.

In some circumstances, it may be necessary for your application to dynamically determine which member to invoke before it invokes it. Sometimes, it may be necessary for your application to reflect the members of the type to determine an appropriate member; on other occasions, it may read information from an application configuration
file or other source that specifies the member to invoke.
               The Type class provides a general-purpose tool, the InvokeMember method, which enables you to bind to and invoke a member in a single method call. Binding carries a performance penalty when it is used repeatedly, so the Type class also provides other methods that enable you to locate a member, store a reference to that member in an instance of a MemberInfo subclass, and then invoke the member directly through the reference variable.

For Example:

using System;
using System.Reflection;
using System.Runtime.Remoting;

[assembly: AssemblyTitle ("mod7_less1")]

namespace Mod7_less1
    class Program

        static void Main(string[] args)
            string s = "hello";
           //fcl method
        public static void fclmethod()
            //early binding

            //late binding
            Type consoleType = typeof(Console);
            BindingFlags.InvokeMethod | BindingFlags.Static | BindingFlags.Public,
            new object[] { "{0},{1}", "Helloworld!", "abc" });


output is display below:

No comments:

Recent Posts