TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Reflection

C# Reflection for beginners and professionals with examples on overloading, method overriding, inheritance, aggregation, base, polymorphism, sealed, abstract, interface, namespaces, exception handling, file io, collections, multithreading, reflection etc.

<< Back to C

C# Reflection

In C#, reflection is a process to get metadata of a type at runtime. The System.Reflection namespace contains required classes for reflection such as:

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

The System.Reflection.Emit namespace contains classes to emit metadata.


C# Type class

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

C# Type Properties

A list of important properties of Type class are given below:

PropertyDescription
AssemblyGets the Assembly for this type.
AssemblyQualifiedNameGets 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.
IsAbstractis used to check if the type is Abstract.
IsArrayis used to check if the type is Array.
IsClassis used to check if the type is Class.
IsEnumis used to check if the type is Enum.
IsInterfaceis used to check if the type is Interface.
IsNestedis used to check if the type is Nested.
IsPrimitiveis used to check if the type is Primitive.
IsPointeris used to check if the type is Pointer.
IsNotPublicis used to check if the type is not Public.
IsPublicis used to check if the type is Public.
IsSealedis used to check if the type is Sealed.
IsSerializableis used to check if the type is Serializable.
MemberTypeis used to check 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

A list of important methods of Type class are given below:

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 a = 10;
        Type type = a.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 t = typeof(System.String);
        Console.WriteLine(t.FullName);
        Console.WriteLine(t.BaseType);
        Console.WriteLine(t.IsClass);
        Console.WriteLine(t.IsEnum);
        Console.WriteLine(t.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 t = typeof(System.String);
        
        Console.WriteLine("Constructors of {0} type...", t);
        ConstructorInfo[] ci = t.GetConstructors(BindingFlags.Public | BindingFlags.Instance);
        foreach (ConstructorInfo c in ci)
        {
            Console.WriteLine(c);
        }
    }
}

Output:

Constructors of 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 t = typeof(System.String);
        
        Console.WriteLine("Methods of {0} type...", t);
        MethodInfo[] ci = t.GetMethods(BindingFlags.Public | BindingFlags.Instance);
        foreach (MethodInfo m in ci)
        {
            Console.WriteLine(m);
        }
    }
}

Output:

Methods of System.String type...
Boolean Equals(System.Object)
Boolean Equals(System.String)
Boolean Equals(System.String, System.StringComparison)
Char get_Chars(Int32)
Void copyTo(Int32, char[], Int32, Int32)
Char[] ToCharArray()
....

C# Reflection Example: Print Fields

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

Output:

Fields of System.String type...
System.String Empty
Int32 TrimHead
Int32 TrimTail
Int32 TrimBoth
Int32 charPtrAlignConst
Int32 alignConst




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf