find and solve || findandsolve.com
Please wait.....

How to work with reflection in C#

Reflection provides objects that describe assemblies, modules, and types in the C# programming language which is used for obtaining type information at runtime.

Assembly describes an assembly that is a reusable, versionable, and self-describing building block of a CLR ( common language runtime)  application.

In c# programming language, namespace System.Reflection will contain all the classes that provide access to the metadata of managed code to get the type information.

You can also use reflection to create type instances dynamically at runtime, get the type from an existing object or bind the type to an existing object and invoke its methods or access its fields and properties. If you define any attributes in our code, those can also access by using reflection.


C# Reflection Example


In the given below example, using reflection to get the class object properties and values in c# programming language.

using System;
using System.Collections.Generic;
using System.Reflection;
namespace FindAndSolve
{
    public class Program
    {
       public static void Main(string[] args)
       {
          List<UserInfo> items = new List<UserInfo>();
          items.Add(new UserInfo { UserId = 101, FirstName = "Sylvia", LastName = "Neupane" });
          items.Add(new UserInfo { UserId = 102, FirstName = "Sundar", LastName = "Neupane" });
          items.Add(new UserInfo { UserId = 103, FirstName = "Dipa", LastName = "Neupane" });
          items.Add(new UserInfo { UserId = 104, FirstName = "Sundar", LastName = "Neupane" });
          items.Add(new UserInfo { UserId = 105, FirstName = "Bindu", LastName = "Pandey" });
          string strmss = string.Empty;
          foreach (var user in items)
          {
             strmss = GetPropertyValues(user);
             Console.WriteLine(strmss);
          }
          Console.ReadLine();
       }
       private static string GetPropertyValues(UserInfo userInfo)
       {
          Type type = userInfo.GetType();
          PropertyInfo[] props = type.GetProperties();
          string str = "{";
          foreach (var prop in props)
          {
            str += (prop.Name + ":" + prop.GetValue(userInfo) + ":" + prop.PropertyType.Name) + ",";
          }
          return str.Remove(str.Length - 1) + "}";
       }
    }
    public class UserInfo
    {
        public int UserId { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
}


Output


{UserId:101:Int32,FirstName:Sylvia:String,LastName:Neupane:String}

{UserId:102:Int32,FirstName:Sundar:String,LastName:Neupane:String}

{UserId:103:Int32,FirstName:Dipa:String,LastName:Neupane:String}

{UserId:104:Int32,FirstName:Sundar:String,LastName:Neupane:String}

{UserId:105:Int32,FirstName:Bindu:String,LastName:Pandey:String}


If you observe given above example, I have added System.Reflection namespace to get the UserInfo class object property details. Here, I used Type & PropertyInfo classes to get the required object property details.


C# Reflection Overview


  • In the C# programming language, Reflection is useful to get the type information that describes assemblies, parameters, members, modules, members, and other entities in the managed code by examining their metadata.
  • By reflection, you can create type instances dynamically at runtime, bind or get the type from an existing object and invoke its methods or access its fields and properties.
  • Reflection provides access to perform late binding and get methods type information created at runtime.