Thursday, March 31, 2011

Understanding a Type by Using Reflection in Console Application.

The Member info Class:
•    Is an abstract base class for all classes that contain reflection information.
•    Contains specializations for fields, Properties, Methods, events, and nested types.

The MemberInfo class is an abstract base class that provides methods and properties that are common to all of the classes that contain reflection information. It contains common methods that apply to any reflected object.

Member
                          Description

Name                              This property returns the name of the member.

MemberType                  This property returns a MemberTypes enumeration value that
                                       Indicates the kind of member (for example, constructor, event, field,
                                       method, type, nested type, or property).

DeclaringType                This property returns the type that declares the member. If the
                                       member is inherited from a parent class, this may not be the same
                                       type as the ReflectedType property.   

ReflectedType               This property returns the type that was reflected to obtain the
                                      MemberInfo instance.

GetCustomAttributes     This method returns an array of objects that contain attributes that
                                      are associated with this member.

IsDefined                      This method returns a Boolean value that indicates whether an
                                     attribute that has the specified name is associated with this member.


The type class:
•    Provides methods and properties that describe the type and its behavior.
•    Provides methods that list a type’s members (optionally filtered by using binding flags)  

The most important of the MemberInfo subclasses is the Type class. The Type class is the class through which you access all other reflection information about a type. It provides methods and properties that describe the type’s behavior and methods in the form of other MemberInfo-derived objects.
Many of the methods and properties of the Type class describe important information
about the type. Several properties, such as IsClass, IsEnum, IsInterface, IsGenericType, IsCOMObject, IsValueType, and IsArray indicate the kind of type that is represented. The IsSubclassOf and FindInterfaces methods, and the BaseType and IsSealed properties, provide information about the inheritance hierarchy of the type. The IsPublic, IsNotPublic, and IsVisible properties describe the visibility of the type.

For example:
using System;
using System.Reflection;
using System.Runtime.Remoting;

[assembly: AssemblyTitle ("ReflectionType")]

namespace ReflectionType1
{
        
    class Program
    {

        static void Main(string[] args)
        {
                                 //memberinfo class
            memberinfomethod();

           Console.ReadLine();
        }

public static void memberinfomethod()
  {
            int i = 0;
            Type type = typeof(String);
            MemberInfo[] typeMembers = type.GetMembers(
BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance);
            foreach (MemberInfo member in typeMembers)
            {
                i++;
                Console.WriteLine("{0} ({1})",
                member.Name,
                member.MemberType);
            }
            Console.WriteLine(i);
            string str;
            Console.ReadLine();
        }
    }
}
       
Output is display below:

No comments:

Recent Posts