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

How C# Reflection Works With Code Examples

In the C# programming language, Reflection objects are used for obtaining type information at runtime. The classes that give access to the metadata of a running program are in the System.Reflection namespace.

The System.Reflection namespace contains required classes for reflection like as given below:

  • Type
  • MemberInfo
  • ConstructorInfo
  • MethodInfo
  • FieldInfo
  • PropertyInfo
  • TypeInfo
  • EventInfo
  • Module
  • Assembly
  • AssemblyName
  • Pointer etc.

C# Type class

C# Type class represents type declarations for interface types, class types,  enumeration types, array types, value types, etc. It is found in the System namespace. It inherits System.Reflection.MemberInfo class.

C# Type Properties

A list of important properties of Type class such as:

PropertyDescription
AssemblyGets the Assembly for this type.
AssemblyQualifiedName Gets the Assembly qualified name for this type.
AttributesGets the Attributes associated with the type.
BaseTypeGets the base or parent type.
FullNameGets the fully qualified name of the type.
IsAbstract Check if the type is Abstract.
IsArrayCheck if the type is Array.
IsClassCheck if the type is Class.
IsEnumCheck if the type is Enum.
IsInterfaceCheck if the type is Interface.
IsNestedCheck if the type is Nested.
IsPrimitiveCheck if the type is Primitive.
IsPointerCheck if the type is Pointer.
IsNotPublicCheck if the type is not Public.
IsPublicCheck if the type is Public.
IsSealedCheck if the type is Sealed.
IsSerializable Check if the type is Serializable.
MemberTypeCheck if the type is Member type of Nested type.
ModuleGets the module of the type.
NameGets the name of the type.
NamespaceGets the namespace of the type.


C# Type Methods

MethodDescription
GetConstructors() Returns all the public constructors for the Type.
GetConstructors(BindingFlags) Returns all the constructors for the Type with specified BindingFlags.
GetFields() Returns all the public fields for the Type.
GetFields(BindingFlags) Returns all the public constructors for the Type with specified BindingFlags.
GetMembers() Returns all the public members for the Type.
GetMembers(BindingFlags) Returns all the members for the Type with specified BindingFlags.
GetMethods() Returns all the public methods for the Type.
GetMethods(BindingFlags) Returns all the methods for the Type with specified BindingFlags.
GetProperties() Returns all the public properties for the Type.
GetProperties(BindingFlags) Returns all the properties for the Type with specified BindingFlags.
GetType() Gets the current Type.
GetType(String) Gets the Type for the given name.


C# Reflection Example: Get Type

using System;  

public class ReflectionExample
{
   public static void Main()
    {
        int number =15;
        Type type = number.GetType();
        Console.WriteLine(type);
    }
}  

Output

System.Int32


C# Reflection Example: Get Assembly

using System;  

using System.Reflection;
public class ReflectionExample
{
   public static void Main()
    {
        Type t = typeof(System.String);
        Console.WriteLine(t.Assembly);
   }
}  

Output

mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089


C# Reflection Example: Print Type Information

using System;  

using System.Reflection;
public class ReflectionExample
{
    public static void Main()
    {
        Type type = typeof(System.String);
        Console.WriteLine(type.FullName);
        Console.WriteLine(type.BaseType);
        Console.WriteLine(type.IsClass);
        Console.WriteLine(type.IsEnum);
        Console.WriteLine(type.IsInterface);
    }
}  

Output

System.String

System.Object

True

False

False


C# Reflection Example: Print Constructors

using System;  

using System.Reflection;
public class ReflectionExample
{
    public static void Main()
    {
        Type type = typeof(System.String);
        Console.WriteLine("Result {0} type...", type);
        ConstructorInfo[] ci = type.GetConstructors(BindingFlags.Public | BindingFlags.Instance);
        foreach (ConstructorInfo c in ci)
        {
            Console.WriteLine(c);
        }
    }
}  

Output

Result System.String type...

Void .ctor(Char*)

Void .ctor(Char*, Int32, Int32)

Void .ctor(SByte*)

Void .ctor(SByte*, Int32, Int32)

Void .ctor(SByte*, Int32, Int32, System.Text.Encoding)

Void .ctor(Char[], Int32, Int32)

Void .ctor(Char[])

Void .ctor(Char, Int32)


C# Reflection Example: Print Methods

using System;  

using System.Reflection;
public class ReflectionExample
{
    public static void Main()
    {
        Type type = typeof(System.String);
        Console.WriteLine("Method Example of {0} type...", type);
        MethodInfo[] ci = type.GetMethods(BindingFlags.Public | BindingFlags.Instance);
        foreach (MethodInfo m in ci)
        {
            Console.WriteLine(m);
        }
    }
}  

Output

Method Example of System.String type...

Char get_Chars(Int32)

Int32 get_Length()

System.String Substring(Int32, Int32)

Boolean Equals(System.Object)

Boolean Equals(System.String)

Boolean Equals(System.String, System.StringComparison)

Void CopyTo(Int32, Char[], Int32, Int32)

Char[] ToCharArray()

Char[] ToCharArray(Int32, Int32)

Int32 GetHashCode()

System.String[] Split(Char[])

System.String[] Split(Char[], Int32)

System.String[] Split(Char[], System.StringSplitOptions)

System.String[] Split(Char[], Int32, System.StringSplitOptions)

System.String[] Split(System.String[], System.StringSplitOptions)

System.String[] Split(System.String[], Int32, System.StringSplitOptions)

System.String Substring(Int32)

System.String Trim(Char[])

System.String TrimStart(Char[])

System.String TrimEnd(Char[])

Boolean IsNormalized()

Boolean IsNormalized(System.Text.NormalizationForm)

System.String Normalize()

System.String Normalize(System.Text.NormalizationForm)

Int32 CompareTo(System.Object)

Int32 CompareTo(System.String)

......


C# Reflection Example: Print Fields

using System;  

using System.Reflection;
public class ReflectionExample
{
    public static void Main()
    {
        Type type = typeof(System.String);
        Console.WriteLine("Result of {0} type...", type);
        FieldInfo[] ci = type.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.NonPublic);
        foreach (FieldInfo f in ci)
        {
            Console.WriteLine(f);
        }
    }
}  

Ouput

Result of System.String type...

System.String Empty

Int32 TrimHead

Int32 TrimTail

Int32 TrimBoth

Int32 charPtrAlignConst

Int32 alignConst