Www.helper3.com
热门关键字: php net javascript java
CSharp基础 专题
专题目录
您的位置:csharp > CSharp基础 专题 > C#反射
C#反射
作者:--    发布时间:2019-11-20 10:27:40

reflection对象用于在运行时获取类型信息。可以访问正在运行的程序的元数据的类在system.reflection命名空间中。

system.reflection命名空间包含允许获取有关应用程序的信息的类,并向应用程序动态添加类型,值和对象。

反射有以下应用:

  • 它允许在运行时查看属性信息。
  • 它允许检查装配中的各种类型并实例化这些类型。
  • 它允许后期绑定到方法和属性。
  • 它允许在运行时创建新的类型,可使用这些类型执行一些任务。

查看元数据

我们在上一章中提到使用反射可以查看属性信息。

需要初始化system.reflection类的memberinfo对象,以发现与类相关联的属性。可将目标类的对象定义为:

system.reflection.memberinfo info = typeof(myclass);

以下程序演示如下:

using system;

[attributeusage(attributetargets.all)]
public class helpattribute : system.attribute
{
   public readonly string url;

   public string topic   // topic is a named parameter
   {
      get
      {
         return topic;
      }
      set
      {
         topic = value;
      }
   }

   public helpattribute(string url)   // url is a positional parameter
   {
      this.url = url;
   }
   private string topic;
}

[helpattribute("information on the class myclass")]
class myclass
{
}
namespace attributeappl
{
   class program
   {
      static void main(string[] args)
      {
         system.reflection.memberinfo info = typeof(myclass);
         object[] attributes = info.getcustomattributes(true);
         for (int i = 0; i < attributes.length; i++)
         {
            system.console.writeline(attributes[i]);
         }

         console.readkey();
      }
   }
}

当它被编译并运行时,它显示附加到类myclass的自定义属性的名称:

helpattribute

例子

在这个例子中,我们使用上一章创建的debuginfo属性,并使用反射来读取rectangle类中的元数据。

using system;
using system.reflection;

namespace bugfixapplication
{
   //a custom attribute bugfix to be
   //assigned to a class and its members
   [attributeusage(attributetargets.class |
   attributetargets.constructor |
   attributetargets.field |
   attributetargets.method |
   attributetargets.property,
   allowmultiple = true)]

   public class debuginfo : system.attribute
   {
      private int bugno;
      private string developer;
      private string lastreview;
      public string message;

      public debuginfo(int bg, string dev, string d)
      {
         this.bugno = bg;
         this.developer = dev;
         this.lastreview = d;
      }

      public int bugno
      {
         get
         {
            return bugno;
         }
      }

      public string developer
      {
         get
         {
            return developer;
         }
      }

      public string lastreview
      {
         get
         {
            return lastreview;
         }
      }

      public string message
      {
         get
         {
            return message;
         }
         set
         {
            message = value;
         }
      }
   }
   [debuginfo(45, "sukida", "12/8/2017", message = "return type mismatch")]
   [debuginfo(49, "maxsu", "10/10/2017", message = "unused variable")]

   class rectangle
   {
      //member variables
      protected double length;
      protected double width;
      public rectangle(double l, double w)
      {
         length = l;
         width = w;
      }
      [debuginfo(55, "sukida", "19/10/2017", message = "return type mismatch")]
      public double getarea()
      {
         return length * width;
      }
      [debuginfo(56, "sukida", "19/10/2017")]
      public void display()
      {
         console.writeline("length: {0}", length);
         console.writeline("width: {0}", width);
         console.writeline("area: {0}", getarea());
      }
   }//end class rectangle

   class executerectangle
   {
      static void main(string[] args)
      {
         rectangle r = new rectangle(14.5, 17.5);
         r.display();
         type type = typeof(rectangle);

         //iterating through the attribtues of the rectangle class
         foreach (object attributes in type.getcustomattributes(false))
         {
            debuginfo dbi = (debuginfo)attributes;
            if (null != dbi)
            {
               console.writeline("bug no: {0}", dbi.bugno);
               console.writeline("developer: {0}", dbi.developer);
               console.writeline("last reviewed: {0}", dbi.lastreview);
               console.writeline("remarks: {0}", dbi.message);
            }
         }

         //iterating through the method attribtues
         foreach (methodinfo m in type.getmethods())
         {
            foreach (attribute a in m.getcustomattributes(true))
            {
               debuginfo dbi = (debuginfo)a;
               if (null != dbi)
               {
                  console.writeline("bug no: {0}, for method: {1}", dbi.bugno, m.name);
                  console.writeline("developer: {0}", dbi.developer);
                  console.writeline("last reviewed: {0}", dbi.lastreview);
                  console.writeline("remarks: {0}", dbi.message);
               }
            }
         }

         console.readline();
      }
   }
}

当上述代码被编译并执行时,它产生以下结果:

length: 14.5
width: 17.5
area: 253.75
bug no: 45
developer: sukida
last reviewed: 12/8/2017
remarks: return type mismatch
bug no: 49
developer: maxsu
last reviewed: 10/10/2017
remarks: unused variable
bug no: 55, for method: getarea
developer: sukida
last reviewed: 19/10/2017
remarks: return type mismatch
bug no: 56, for method: display
developer: sukida
last reviewed: 19/10/2017
remarks:

所有评论

loading......

滚动到顶部 滚动到底部
合作联系(微信):
www.helper3.com
关于本站:
宗旨
网站备案号:
粤ICP备17073857号-1

声明:
本站部分内容来自网络,如您发现本站内容侵害到您的利益,请联系本站管理员处理。