Sunday 23 September 2018

Work with Reflection using C#.Net :: A Comprehensive approach for retrieving of metadata information

Agenda :
            Dear all In this article i would like to emphasize the essence of Work with Reflection using C#.Net :: A Comprehensive approach for retrieving of metadata information.This was demonstrated with the help of C#.Net using Linq To SQl.

Description :
          In previous articles I explained different articles related toC#.Net,Asp.Net,Sql Server,XML and DotNetInterviewQuestions Related articles.Let's know how we will work with Work with Reflection using C#.Net :: A Comprehensive approach for retrieving of metadata information



About Reflection :

  • Reflection is used to retrieve the metadata information on types at Runtime.
  • Metadata describes about assemblies, modules and types.
  • In order to work with reflection we have to use the System.Runtime Namespace
  • We can use reflection to dynamically  create an instance of a type, bind the type to an existing object.


Types defined in System.Reflection :

  • Assembly
  • Module
  • Enum
  • MethodInfo
  • ConstructorInfo
  • MemberInfo
  • ParameterInfo
  • TypeInfo
  • FieldInfo
  • EventInfo
  • PropertyInfo
Steps to implement Reflection Concept:

  • Create a Console Application
  • Declare a Class
  • Declare a Properties Inside a Class
  • Declare a Constructor inside a class
  • Declare a Method inside a class
  • Create a type and access properties, constructor and method information of a class 
Complete Example Source Code :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
namespace FMTDemo
{
    class P1
    {
        static void Main(string[] args)
        {
            Type type = typeof(MyClass);
            PropertyInfo[] propertyInfo = type.GetProperties();
            Console.ForegroundColor = ConsoleColor.DarkRed;
            Console.WriteLine("List of Properties....");
            Console.BackgroundColor = ConsoleColor.Blue;
            foreach (var item in propertyInfo)
            {
                Console.WriteLine(item.Name);
            }

            ConstructorInfo[] constructorInfo = type.GetConstructors();
            Console.ForegroundColor = ConsoleColor.DarkRed;
            Console.WriteLine("List of Constructors...");
            Console.BackgroundColor = ConsoleColor.White;
            foreach (ConstructorInfo item in constructorInfo)
            {
                Console.WriteLine(item);
            }

            MethodInfo[] methodInfo = type.GetMethods();
            Console.ForegroundColor = ConsoleColor.DarkRed;
            Console.WriteLine("List of Methods...");
            Console.BackgroundColor = ConsoleColor.Yellow;
            foreach (var item in methodInfo)
            {
                Console.WriteLine(item.Name);
            }
            Console.ReadLine();
        }
    }
    class MyClass
    {
        public int MyProperty { get; set; }
        public string Name { get; set; }
        public bool IsDataExists { get; set; }
        public DateTime DateOfJoin { get; set; }
        public MyClass()
        {

        }
        public void A()
        {
            Console.WriteLine("A");
        }
        public void B()
        {
            Console.WriteLine("B");
        }
        public void C()
        {
            Console.WriteLine("C");
        }
    }
}


No comments :

Post a Comment