(1)Namespace ClassLibrarySport
{
public abstract class Sport
{
protected string name;
öffentliche abstrakte Zeichenfolge GetName();
öffentlicher abstrakter String GetDuration();
}
}
= = = = = == = == = == = == = == = == = == = == = == = == = == = == = == = == = = = = =
(2)namespace ClassLibrarySomeSports//该项目添加了对(1)的引用
{
public class Football : ClassLibrarySport.Sport
{
public Football()
{
name = "Football";
}
public override string GetName()
{
return name;
}
public override string GetDuration()
{
return „vier 15-Minuten-Viertel“;
} }
}
=
= = = = == = == = == = == = == = == = == = == = == = == = == = == = == = == = == = =
(3)namespace ConsoleAssemblyTest//该项目添加了对(1)的引用
{
class Program
{
static void Main(string[] args)
{
Assembly Assembly = Assembly.LoadFrom(@"E:ClassLibrarySomeSports
bin DebugClassLibrarySomeSports.dll");
Type[]types =assembly.GetTypes();
Console.WriteLine("Typ aus ClassLibrarySomeSports.dll abrufen:");
for (int i = 0; i <types.Length;i++)
{
Console.WriteLine(types[i].Name);
}
//
Die GetConstructor()-Methode wird verwendet, um die GetConstructor()-Methode zu verwenden.Console.WriteLine("
Use Method GetConstructor()
:");ConstructorInfo ci =types[0].GetConstructor(new Type[0]);
ClassLibrarySport.Sport sport = (ClassLibrarySport.Sport)ci.Invoke(new object[0]);
Console.WriteLine(sport.GetName() + " has " + sport.GetDuration());
//Erweiterte Activator.CreateInstance()-Funktion
//Erweiterte Assembly.CreateInstance()-Vorgänge sind null,??
Console.WriteLine("Use Method CreateInstance():");
ClassLibrarySport.Sport sport1 = (ClassLibrarySport.Sport)
Activator.CreateInstance(types[0]);
Console.WriteLine(sport1.GetName() + " has " + sport1.GetDuration());
// Die Funktion „GetDuration“ wird verwendet, um die Invoke()-Funktion zu verwenden.
object objSport = Activator.CreateInstance(types[0]);
MethodInfo-Methode = Typen[0].GetMethod("GetDuration");
object o = method.Invoke(objSport, new object[0]);
Console.WriteLine(o als String);
Console.Read();
}
}
}
= = = = = == = == = == = == = == = == = == = == = == = == = == = ==
Ausgabe:
Typ von ClassLibrarySomeSports abrufen. dll:
Fußball
Verwenden Sie die Methode GetConstructor():
Fußball hat vier 15-Minuten-Viertel.
Verwenden Sie die Methode CreateInstance():
Fußball hat vier 15-Minuten-Viertel.
Vier 15-Minuten-Viertel.
http://www.cnblogs.com/KissKnife/archive/2006/08/13 /475717.html