Wednesday, March 24, 2010
Get Class Members using Reflection
You can find basic information of Reflection from What is Reflection.
You can get members of any class using reflection. Let’s start with constructor.
You can retrieve all constructors using “GetConstructors” method which is available in Type class object. You can get Type class object from loading assembly and find Type using “GetType” method. For loading assembly and find type please click here.
Following code snippet is used to get all constructors from any class.
private void GetConstructors(Type type)
{
ConstructorInfo[] constructors = type.GetConstructors(BindingFlags.CreateInstance | BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);
foreach (ConstructorInfo constructor in constructors)
{
string constructorString = "";
if (constructor.IsStatic)
constructorString = "static new(";
else if (constructor.IsPrivate)
constructorString = "private new(";
else if (constructor.IsPublic)
constructorString = "public new(";
ParameterInfo[] parameters = constructor.GetParameters();
foreach (ParameterInfo parameter in parameters)
{
constructorString += parameter.ParameterType.Name + " " + parameter.Name + ", ";
}
if(constructorString.EndsWith(", "))
constructorString = constructorString.Substring(0, constructorString.Length - 2);
constructorString += ")";
listBox2.Items.Add(constructorString);
}
}
Here I have not handling error. You can optimize code. I have just draft the code for demo purpose.
We need to call “GetConstructors” method of Type class object which returns ConstructorInfo class array. You can iterate that array and find out constructor information like parameters.
Same way you can find out variables which are declared into class. Following code snippet is used for that:
private void GetVariables(Type type)
{
FieldInfo[] fields = type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance);
foreach (FieldInfo field in fields)
{
listBox3.Items.Add(field.FieldType.Name + " " + field.Name);
}
}
GetFields class returns all the available variables declared into that type. Here we mentioned Binding Flags which are used to find out type of variable. If you want to find out only private variables then you have to specify “Instance” and “Private” flag only. Here I need to find out Public, Private, Protected and static variables so I have mentioned all required flags.
GetMethods method used to find out list of methods into type. Following code will help you to find out available methods:
private void GetMethods(Type type)
{
MethodInfo[] methods = type.GetMethods();
foreach (MethodInfo method in methods)
{
string methodString = "";
if (method.IsStatic)
methodString = "static " + method.Name + "(";
else if (method.IsPrivate)
methodString = "private " + method.Name + "(";
else if (method.IsPublic)
methodString = "public " + method.Name + "(";
ParameterInfo[] parameters = method.GetParameters();
foreach (ParameterInfo parameter in parameters)
{
methodString += parameter.ParameterType.Name + " " + parameter.Name + ", ";
}
if (methodString.EndsWith(", "))
methodString = methodString.Substring(0, methodString.Length - 2);
methodString += ")";
listBox4.Items.Add(methodString);
}
}
You can also download source code from here.
Labels:
GetConstructors,
GetFields,
GetMethods,
GetType,
GetTypes,
Load Assembly,
Reflection
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment