System.Reflection - Reflection objects are used for obtaining type information (attribute, assembly, late binding methods & properties at run time.
namespace UnitTestVM
{
[TestClass]
public class ReflectionClass
{
public static int Id;
public static string Name;
public static void ReflectionMethod()
{
Type type = typeof(ReflectionClass); // type pointer
Debug.WriteLine("Fields Info");
FieldInfo[] fields = type.GetFields(); // Obtain all fields
foreach (var field in fields) // Loop through fields
{
string name = field.Name; // attribute name
object obj = field.GetValue(null); // attribute value
System.Type typ = obj.GetType(); //attribute type
Debug.WriteLine(name + " = " + obj + ", " + typ );
}
Debug.WriteLine("Assembly Info");
System.Reflection.Assembly info = typeof(System.Int32).Assembly;
Debug.WriteLine(info); //get assembly information
Debug.WriteLine("Custom Attributes");
System.Reflection.MemberInfo member = typeof(ReflectionClass);
object[] att = member.GetCustomAttributes(true);
for (int i = 0; i < att.Length; i++) Debug.WriteLine(att[i]);
Debug.WriteLine("Method Info");
MethodInfo[] mi = type.GetMethods();
for (int i = 0; i < mi.Length; i++) Debug.WriteLine(mi[i]);
Debug.WriteLine("Member Info");
MemberInfo[] memi = type.GetMembers();
for (int i = 0; i < memi.Length; i++) Debug.WriteLine(memi[i]);
Debug.WriteLine("Property Info");
PropertyInfo[] pi = type.GetProperties();
for (int i = 0; i < pi.Length; i++) Debug.WriteLine(pi[i]);
}
}
[TestClass]
public class SOAPTest
{
[TestMethod]
public void ReflectionTestMethod()
{
ReflectionClass.Id = 123;
ReflectionClass.Name = "C#";
ReflectionClass.ReflectionMethod(); // Invoke reflection methods
}
}
}
Fields Info
Id = 123, System.Int32
Name = C#, System.String
Assembly Info
mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
Custom Attributes
Microsoft.VisualStudio.TestTools.UnitTesting.TestClassAttribute
Method Info
Void ReflectionMethod()
System.String ToString()
Boolean Equals(System.Object)
Int32 GetHashCode()
System.Type GetType()
Member Info
Void ReflectionMethod()
System.String ToString()
Boolean Equals(System.Object)
Int32 GetHashCode()
System.Type GetType()
Void .ctor()
Int32 Id
System.String Name
Property Info
namespace UnitTestVM
{
[TestClass]
public class ReflectionClass
{
public static int Id;
public static string Name;
public static void ReflectionMethod()
{
Type type = typeof(ReflectionClass); // type pointer
Debug.WriteLine("Fields Info");
FieldInfo[] fields = type.GetFields(); // Obtain all fields
foreach (var field in fields) // Loop through fields
{
string name = field.Name; // attribute name
object obj = field.GetValue(null); // attribute value
System.Type typ = obj.GetType(); //attribute type
Debug.WriteLine(name + " = " + obj + ", " + typ );
}
Debug.WriteLine("Assembly Info");
System.Reflection.Assembly info = typeof(System.Int32).Assembly;
Debug.WriteLine(info); //get assembly information
Debug.WriteLine("Custom Attributes");
System.Reflection.MemberInfo member = typeof(ReflectionClass);
object[] att = member.GetCustomAttributes(true);
for (int i = 0; i < att.Length; i++) Debug.WriteLine(att[i]);
Debug.WriteLine("Method Info");
MethodInfo[] mi = type.GetMethods();
for (int i = 0; i < mi.Length; i++) Debug.WriteLine(mi[i]);
Debug.WriteLine("Member Info");
MemberInfo[] memi = type.GetMembers();
for (int i = 0; i < memi.Length; i++) Debug.WriteLine(memi[i]);
Debug.WriteLine("Property Info");
PropertyInfo[] pi = type.GetProperties();
for (int i = 0; i < pi.Length; i++) Debug.WriteLine(pi[i]);
}
}
[TestClass]
public class SOAPTest
{
[TestMethod]
public void ReflectionTestMethod()
{
ReflectionClass.Id = 123;
ReflectionClass.Name = "C#";
ReflectionClass.ReflectionMethod(); // Invoke reflection methods
}
}
}
Id = 123, System.Int32
Name = C#, System.String
Assembly Info
mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
Custom Attributes
Microsoft.VisualStudio.TestTools.UnitTesting.TestClassAttribute
Method Info
Void ReflectionMethod()
System.String ToString()
Boolean Equals(System.Object)
Int32 GetHashCode()
System.Type GetType()
Member Info
Void ReflectionMethod()
System.String ToString()
Boolean Equals(System.Object)
Int32 GetHashCode()
System.Type GetType()
Void .ctor()
Int32 Id
System.String Name
Property Info
No comments:
Post a Comment