C#反射机制

匿名 (未验证) 提交于 2019-12-02 22:06:11

,,.,!

!

, .,a,

dll dll

dll

dll dll

dll

未知 dll

dll. dll

dll

Sport.cs.

using System; public abstract class Sport {     protected string name;     public abstract string GetDuration();     public abstract string GetName(); }

csc /t:library Sport.cs

SomeSports.cs

using System; public class Football : Sport {     public Football()     {         name = "Football";     }     public override string GetDuration()     {         return "four 15 minute quarters";     }     public override string GetName()     {         return name;     } } public class Hockey : Sport {     public Hockey()     {         name = "Hockey";     }     public override string GetDuration()     {         return "three 20 minute periods";     }     public override string GetName()     {         return name;     } } public class Soccer : Sport {     public Soccer()     {         name = "Soccer";     }     public override string GetDuration()     {         return "two 45 minute halves";     }     public override string GetName()     {         return name;     } }

csc /t:library /r:Sport.dll SomeSports.cs

dll

AssemblyDemo cs.

using System; using System.Reflection; public class AssemblyDemo {     public static void Main(string[] args)     {         int i, j;         //==========================         //First the command line arguments are evaluated.if there isn't         //at least one,a usage message is printed         //=================================         if (args.GetLength(0) < 1)         {             Console.WriteLine("usage is AssemblyDemo<library_name>");         }         else         {             //========================             // An Assembly object is obtained from the command line argument             //========================             Assembly assembly = Assembly.LoadFrom(args[0]);             Type[] types = assembly.GetTypes();             Console.WriteLine(assembly.GetName().Name + "contains the following types");             for (i = 0; i < types.GetLength(0); ++i)             {                 Console.WriteLine("\r(" + i + ") " + types[i].Name);             }             i = types.Length - 1;             Console.Write("make selection(0-" + i + ");");             j = Convert.ToInt32(Console.ReadLine());             Console.WriteLine();             if (types[j].IsSubclassOf(typeof(Sport)))             {                 ConstructorInfo ci = types[j].GetConstructor(new Type[0]);                 Sport sport = (Sport)ci.Invoke(new Object[0]);                 Console.WriteLine(sport.GetName() + "has" + sport.GetDuration());             }             else             {                 Console.WriteLine(types[j].Name + "is not a sub-class of Sport");             }         }     } }

csc /r:Sport.dll AssemblyDemo.cs

ssemblyDemo SomeSports.dll

ockeyhasthree 20 minute periods.

――“反射就是一种机制,通过这种机制,我们能知道一些位知程序集的详细信息!”;通过上一篇

Module Module

Module

ModuleDemo.cs

// csc /r:Sport.dll ModuleDemo.cs

using System; using System.Reflection; public class ModuleDemo {     public static void Main(string[] args)     {         //=======================         // Am Module object is obtained representing the         // SomeSports.dll library file         //=======================         Assembly assembly = Assembly.Load("SomeSports");         Module module = assembly.GetModule("SomeSports.dll");         //======================         //Search the module for the type named "Football"         Type[] types = module.FindTypes(Module.FilterTypeName, "Football");         if (types.Length != 0)         {             ConstructorInfo ci = types[0].GetConstructor(new Type[0]);             Sport sport = (Sport)ci.Invoke(new Object[0]);             Console.WriteLine(sport.GetName() + " has " + sport.GetDuration());         }         else         {             Console.WriteLine("type not found");         }     } }

csc /r:Sport.dll ModuleDemo.cs MouduleDemo

:Football has four 15 minute quarters

C#

文章来源: C#反射机制
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!