C# 中的反射

c# 中的反射

反射对象用于在运行时获取类型信息。允许访问正在运行的程序的元数据的类位于 System.Reflection 命名空间中。

以下是 Reflections 的应用 –

它允许在运行时查看属性信息。

它允许检查程序集中的各种类型并实例化这些类型。

它允许后期绑定到方法和属性

它允许在运行时创建新类型,然后使用这些类型执行一些任务。

ul>

让我们看一个例子 –

示例

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();      }   }}

以上就是C# 中的反射的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1434975.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
C# 中的泛型委托是什么?
上一篇 2025年12月17日 10:27:29
C# 相当于 Java 函数式接口
下一篇 2025年12月17日 10:27:51

相关推荐

发表回复

登录后才能评论
关注微信