public class L
{
static void Main()
{
#region ZL1这里我们用映射获得类一里面的成员!
Console.WriteLine("\n\n我们获取类一里面的成员!");
Type types = typeof(L1);
//获取字段!
//获取一个字段的方法
FieldInfo fid =types.GetField("name");
Console.WriteLine("L1里面的字段:"+fid.GetValue(l));
//获取两个字段的方法
FieldInfo[] fd = types.GetFields();
for (int i = 0; i < fd.Length;i++)
{
Console.WriteLine("连环输出:"+fd[i].Name);
}
Console.WriteLine();
//获取函数
Type[] tyl = new Type[2];
tyl[0]=typeof(string);
tyl[1]=typeof (int);
ConstructorInfo HS =types.GetConstructor(tyl);
Console.WriteLine("构造函数输出:"+HS.ToString());
//获取方法
MethodInfo ff = types.GetMethod("F1");
Console.WriteLine("输出方法:"+ff);
Console.WriteLine("\n\n我们获取类一里面的成员!");
#endregion
//--------------------------------------------------------------------------------------------------------------------
#region
L2这里我们获取类而里面的成员
Console.WriteLine("\n\n利用映射获取类而里面的成远!\n\n");
//获取类二里面的字段!
Type tp2=typeof (l2);
Console.WriteLine("类二的名称:{0}",tp2.Name);
FieldInfo[] getf = tp2.GetFields();
Console.WriteLine("将两个字段同时输出:\n\n");
for (int i = 0; i < getf.Length; i++)
{
Console.WriteLine("字段输出:"+getf[i].Name);
}
//获取类2里面的方法
MethodInfo mf = tp2.GetMethod("F2");
Console.WriteLine("方法输出:"+mf);
//获取类2里面的构造函数
Type [] tp=new Type [2];
tp[0]=typeof (string );
tp[1]=typeof (int);
ConstructorInfo cn = tp2.GetConstructor(tp);
Console.WriteLine("类2里面的构造函数:"+cn);
#endregion
}
}
public class L1
{
//字段!
public string name;
public int age;
//构造函数
public L1()
{
Console.WriteLine("类1的构造函数!");
}
public L1(string name, int age)
{
Console.WriteLine("☆★●→" + name + "■■&▲" + age);
}
//方法
public string F1()
{
return "欢迎:" + name + "!!!";
}
}
public class l2 : L1
{
public string a;
public int b;
public l2()
{
Console.WriteLine("类2输出:");
}
public l2(string a, int b) : base(a, b)
{
Console.WriteLine("利用构造函数输出在父类里面就可以输出了!\n\n");
Console.WriteLine("从新" + a + "二遍输出:■■&▲" + b);
}
public void F2()
{
Console.WriteLine("传说中的:" + a + "\n\n");
}
}