//控制台运行带代码
Console.WriteLine("列出程序集中的所有类型:");
Assembly ass = Assembly.LoadFile(@"C:\Users\liangqiang\Desktop\Test\Test1\ClassLibrary1\bin\Debug\ClassLibrary1.dll");
Type classPerson = null;
Type[] mytypes = ass.GetTypes();
foreach (Type t in mytypes)
{
Console.WriteLine(t.Name);
if (t.Name == "ClassPerson")
{
classPerson = t;
}
}
Console.WriteLine("列出ClassPerson类中的所有方法:");
MethodInfo[] mif = classPerson.GetMethods();
foreach (MethodInfo mi in mif)
{
Console.WriteLine(mi.Name);
}
Console.WriteLine("实例化ClassPerson,并调用SayHello方法:");
Object obj = Activator.CreateInstance(classPerson);
Object objName = Activator.CreateInstance(classPerson, "测试Name");
MethodInfo msayhello = classPerson.GetMethod("SayHello");
msayhello.Invoke(obj, null);
msayhello.Invoke(objName, null);
Console.ReadKey();
//类库
public ClassPerson() : this(null)
{
}
public ClassPerson(string strname)
{
name = strname;
}
private string name = null;
private string sex;
private int age;
public string Name
{
set { name = value; }
get { return name; }
}
public string Sex
{
set { sex = value; }
get { return sex; }
}
public int Age
{
set { age = value; }
get { return age; }
}
public void SayHello()
{
if (name == null)
{
Console.WriteLine("Hello,World");
}
else
{
Console.WriteLine("Hello,"+name);
}
}