Please login to save your progress.
Introduction to Reflection API
MemberInfo
MemberInfo class obtains information about the attributes of a member and provides access to member metadata.
For getting list of all members within Type we can use GetMembers method:
1
System.Reflection.MemberInfo[] mbrInfoArray = type.GetMembers();
other properties and methods you can check on MSDN
Examples
Get all members withing type and output its type and name
x
1
using System;
2
using System.Collections.Generic;
3
using System.Reflection;
4
using System.Text;
5
6
public class Program
7
{
8
public static void Main()
9
{
10
Type theType = Type.GetType("System.Reflection.Assembly" );
11
Console.WriteLine("\nSingle Type is {0}\n", theType );
12
MemberInfo[] mbrInfoArray =theType.GetMembers();
13
foreach ( MemberInfo mbrInfo in mbrInfoArray )
14
{
15
Console.WriteLine( "{0} {1}",mbrInfo.MemberType, mbrInfo.Name );
16
}
17
}
18
}
As you can see members can have different types:
- Constructor Specifies that the member is a constructor, representing a ConstructorInfo member.
- Custom Specifies that the member is a custom member type.
- Event Specifies that the member is an event, representing an EventInfo member.
- Field Specifies that the member is a field, representing a FieldInfo member.
- Method Specifies that the member is a method, representing a MethodInfo member.
- NestedType Specifies that the member is a nested type, extending MemberInfo.
- Property Specifies that the member is a property, representing a PropertyInfo member.
- TypeInfo Specifies that the member is a type, representing a TypeInfo member.
Some of these types we going to review in next tutorial concepts.
Page 5 of 11