(1)namespace ClassLibrarySport
{
public abstract class Sport
{
nama string yang dilindungi;
string abstrak publik GetName();
string abstrak publik GetDuration();
}
}
= = = = = == = == = == = == = == = == = == = == = == = == = == = == = == = == = = = = =
(2)namespace ClassLibrarySomeSports//该项目添加了对(1)的引用
{
Sepak Bola kelas publik : ClassLibrarySport.Olahraga
{
Sepak Bola publik()
{
nama = "Sepak Bola";
}
public override string GetName()
{
nama kembali;
}
public override string GetDuration()
{
return "empat seperempat 15 menit";
}
}
}
= = = = = == = == = == = == = == = == = == = == = == = == = == = == = == = == = == = =
(3)namespace ConsoleAssemblyTest//该项目添加了对(1)的引用
{
class Program
{
static void Main(string[] args)
{
Majelis perakitan = Majelis.LoadFrom(@"E:ClassLibrarySomeSports
bin DebugClassLibrarySomeSports.dll");
Tipe[] tipe = perakitan.GetTypes();
Console.WriteLine("Dapatkan Jenis Dari ClassLibrarySomeSports.dll:");
for (int i = 0; i < tipe.Panjang; i++)
{
Console.WriteLine(tipe[i].Nama);
}
//使用GetConstructor()方法获取对应类型的构造器,从而构造出该类型的对象
Console.WriteLine("Gunakan Metode GetConstructor():");
ConstructorInfo ci = tipe[0].GetConstructor(Tipe baru[0]);
ClassLibrarySport.Sport sport = (ClassLibrarySport.Sport)ci.Invoke(objek baru[0]);
Console.WriteLine(sport.GetName() + " memiliki " + sport.GetDuration());
//使用Activator.CreateInstance()方法构造出该类型的对象
//使用assembly.CreateInstance()返回为null,??
Console.WriteLine("Gunakan Metode CreateInstance():");
ClassLibrarySport.Sport sport1 = (ClassLibrarySport.Sport)
Aktivator.CreateInstance(types[0]);
Console.WriteLine(sport1.GetName() + " memiliki " + sport1.GetDuration());
//反射指定类型中的名称为“GetDuration”的方法,通过Invoke()方法执行该方法
object objSport = Activator.CreateInstance(types[0]);
MethodInfo metode = tipe[0].GetMethod("GetDuration");
objek o = metode.Panggil(objSport, objek baru[0]);
Console.WriteLine(o sebagai string);
Konsol.Baca();
}
}
}
= = = = = == = == = == = == = == = == = == = == = == = == = == = ==
Output:
Dapatkan Jenis Dari ClassLibrarySomeSports. dll:
Sepak Bola
Metode Penggunaan GetConstructor():
Sepak bola memiliki empat kuarter 15 menit
Gunakan Metode CreateInstance():
Sepak bola memiliki empat kuarter 15 menit
empat kuarter 15 menit
http://www.cnblogs.com/KissKnife/archive/2006/08/13 /475717.html