Answers for "Reflection: How to Use Type and TypeInfo"

C#
0

Reflection: How to Use Type and TypeInfo

//Type class represents type declarations: class types, interface types, array types, 
//value types, enumeration types, type parameters, generic type definitions, and open or 
//closed constructed generic types.

//A TypeInfo instance contains the definition for a Type, and a Type now contains only reference data.

public class Student
{
    public string FullName { get; set; }
    
    public int Class { get; set; }
    
    public DateTime DateOfBirth { get; set; }
    
    public string GetCharacteristics()
    {
    	return "";
    }
}

//And we want to get all methods and properties declared within this type. 
//Using TypeInfo we can do this in easy and clean way

TypeInfo studentInfo = typeof(Student).GetTypeInfo();
IEnumerable<PropertyInfo> declaredProperties = studentInfo.DeclaredProperties;
IEnumerable<MethodInfo> declaredMethods = studentInfo.DeclaredMethods;

//Also we can get it`s name and namespase using properties Name & Namespace

Type student = typeof(Student);
string name = student.Name;
string ns = student.Namespace;

// Also it is possible to get type of any existing object using Object.GetType method

Type type = myObject.GetType();

//BindingFlags Enumeration
//When you will search members and types you may need to use BindingFlags. BindingFlags specifies flags that control binding and the way in which the search for members and types is conducted by reflection.

//This enumeration has a FlagsAttribute attribute that allows a bitwise combination of its member values.

//Examples
//Get all types within assembly and output it`s names and properties count

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
					
public class Program
{
	public static void Main()
	{
		var assembly = Assembly.GetExecutingAssembly();
		var types = assembly.GetTypes();
		foreach(var type in types)
		{
			var typeInfo = type.GetTypeInfo();
			Console.WriteLine("Type "+ type.FullName+ " has "+typeInfo.DeclaredProperties.Count().ToString()+" properties");
		}
	}
}

namespace University {	
	public class Student
	{
		public string FullName { get; set; }		
		public int Class { get; set; }		
		public DateTime DateOfBirth { get; set; }		
		public string GetCharacteristics()
		{
			return "";
		}
	}

	namespace Department {		
		public class Professor {			
			  public string FullName { get; set; }			
		}
	}
}

//Type Program has 0 properties
//Type University.Student has 3 properties
//Type University.Department.Professor has 1 properties


//Obtain Type information using Object.GetType method
using System;
using System.Text;

public class Program
{
    public static void Main()
    { 
        var sb = new StringBuilder();
        Type type = sb.GetType();
		Console.WriteLine(type.Name);
    }
}

//StringBuilder

//Check if object is type
using System;
using System.IO;

public class Program
{
    public static void Main() 
    {
        Object someObject = new StringReader("This is a StringReader");        
        if (IsType(someObject, "System.IO.TextReader")) 
        {
            Console.WriteLine("GetType: someObject is a TextReader");
        }
    }
    public static bool IsType(object obj, string type) 
    {
        Type t = Type.GetType(type, true, true);
        return t == obj.GetType() || obj.GetType().IsSubclassOf(t);
    }    
}

//GetType: someObject is a TextReader
Posted by: Guest on August-26-2021

Code answers related to "Reflection: How to Use Type and TypeInfo"

C# Answers by Framework

Browse Popular Code Answers by Language