代码
using System;
using System.Reflection;
class Asminfo1
{
public static void Main(string[] args)
{
Console.WriteLine("\nReflection.MemberInfo");
//Get the Type and MemberInfo.
//Insert the fully qualified class name inside the quotation marks in the following statement.
// Type MyType = Type.GetType("System.IO.BinaryReader");
Type MyType = Type.GetType("test");
// MemberInfo[] Mymemberinfoarray = MyType.GetMembers(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly);
MemberInfo[] Mymemberinfoarray = MyType.GetMembers();
//Get and display the DeclaringType method.
Console.Write("\nThere are {0} documentable members in ", Mymemberinfoarray.Length);
Console.Write("{0}.", MyType.FullName);
foreach (MemberInfo Mymemberinfo in Mymemberinfoarray)
{
Console.Write("\n" + Mymemberinfo.Name);
}
//******************************8
System.Text.StringBuilder sb1 = new System.Text.StringBuilder();
//Type t = typeof(System.Text.StringBuilder);
Type t = sb1.GetType();
object createObject = Activator.CreateInstance(t, "I am a instance of StringBuilder!");
System.Text.StringBuilder sb = (System.Text.StringBuilder)createObject;
Console.Write(sb.ToString());
string className = t.Name;
//获取所有方法
System.Reflection.MethodInfo[] methods = t.GetMethods();
Console.Write("\n"+ "method");
foreach (System.Reflection.MethodInfo method in methods)
{
Console.Write("\n" + method.Name + System.Environment.NewLine);
}
//获取所有成员
// System.Reflection.MemberInfo[] members = t.GetMembers();
//获取所有属性
Console.Write("\n" + "property");
System.Reflection.PropertyInfo[] properties = t.GetProperties();
foreach (System.Reflection.PropertyInfo property in properties)
{
Console.Write("\n" + property.Name);
}
Console.ReadKey();
}
}
public class test
{
private int _x;
public int X
{
get { return _x; }
set { _x = value; }
}
public bool isgood;
int getX()
{
return _x;
}
}
using System.Reflection;
class Asminfo1
{
public static void Main(string[] args)
{
Console.WriteLine("\nReflection.MemberInfo");
//Get the Type and MemberInfo.
//Insert the fully qualified class name inside the quotation marks in the following statement.
// Type MyType = Type.GetType("System.IO.BinaryReader");
Type MyType = Type.GetType("test");
// MemberInfo[] Mymemberinfoarray = MyType.GetMembers(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly);
MemberInfo[] Mymemberinfoarray = MyType.GetMembers();
//Get and display the DeclaringType method.
Console.Write("\nThere are {0} documentable members in ", Mymemberinfoarray.Length);
Console.Write("{0}.", MyType.FullName);
foreach (MemberInfo Mymemberinfo in Mymemberinfoarray)
{
Console.Write("\n" + Mymemberinfo.Name);
}
//******************************8
System.Text.StringBuilder sb1 = new System.Text.StringBuilder();
//Type t = typeof(System.Text.StringBuilder);
Type t = sb1.GetType();
object createObject = Activator.CreateInstance(t, "I am a instance of StringBuilder!");
System.Text.StringBuilder sb = (System.Text.StringBuilder)createObject;
Console.Write(sb.ToString());
string className = t.Name;
//获取所有方法
System.Reflection.MethodInfo[] methods = t.GetMethods();
Console.Write("\n"+ "method");
foreach (System.Reflection.MethodInfo method in methods)
{
Console.Write("\n" + method.Name + System.Environment.NewLine);
}
//获取所有成员
// System.Reflection.MemberInfo[] members = t.GetMembers();
//获取所有属性
Console.Write("\n" + "property");
System.Reflection.PropertyInfo[] properties = t.GetProperties();
foreach (System.Reflection.PropertyInfo property in properties)
{
Console.Write("\n" + property.Name);
}
Console.ReadKey();
}
}
public class test
{
private int _x;
public int X
{
get { return _x; }
set { _x = value; }
}
public bool isgood;
int getX()
{
return _x;
}
}