Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Late Binding Using Reflections

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 313
    Comment on it

    In this blog we illustrate how to get the late binding using reflections for this firstly we need to understand "what is reflection" and "what is late binding".


    What is reflection?

    Reflection is basically read the assembly content at runtime.

    Reflection is very useful if we are working with the web application because reflection provides a way to dynamically display all the properties of a label and can also been change these properties as per the requirement.

     

    What is late binding?

    Late binding is basically a binding at runtime it means compiler does not know about the object and its properties and methods.

     

    To understand how we can perform a late binding using reflection see the below example. In this example we have a class User and all attributes of the class at compile time. In this example we create an object at runtime which is the type of reflection.

     

    Code:

    using System;
    using System.Reflection;
    
    namespace ReflectionSampleProgram
    {
        public class User
        {
            public int UserID { get; set; }
            public string UserName { get; set; }
            public string Email { get; set; }
            public User()
            {
                this.UserID = -1;
                this.UserName = string.Empty;
               this.Email = string.Empty;
            }
            public User(int id, string name,string email)
            {
               this.UserID = id;
               this.UserName = name;
         this.Email = email;
            }
            public void displayName()
            {
                Console.WriteLine("Name :" + this.UserName);
            }
          public void displayUser(string name,string email)
            {
                Console.WriteLine("Name :" + name);
                Console.WriteLine("Email :" + email);
            }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                Assembly exce = Assembly.GetExecutingAssembly();
                Type Ltype =exce.GetType("ReflectionConcept.User");
                object objUser = Activator.CreateInstance(Ltype);
                MethodInfo method = Ltype.GetMethod("displayUser");
                string[] parameter = new string[2];
                parameter[0] = "Anita";
                parameter[1] = "anita.sharma@gmail.com";
               string userName=   (string)method.Invoke(objUser , parameter);
                Console.ReadLine();
            }
        }
    }
    

     

    Output:
    Name :Anita
    Email :anita.sharma@gmail.com

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: