在C#中的反思
反射对象用于在运行时获取类型信息。可以访问正在运行的程序的元数据的类位于System.Reflection命名空间中。
以下是反射的应用程序-
它允许在运行时查看视图属性信息。
它允许检查装配中的各种类型并实例化这些类型。
它允许后期绑定到方法和属性
它允许在运行时创建新类型,然后使用这些类型执行一些任务。
让我们看一个例子-
示例
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();
}
}
}热门推荐
10 新年门口花束祝福语简短
11 盘锦结婚祝福语大全简短
12 父母生日祝福语 简短独特
13 家庭恩爱祝福语简短英文
14 高考俄语祝福语大全简短
15 虎年祝福语 诗句唯美简短
16 生日婚礼祝福语简短精辟
17 虎年喝酒拜年祝福语简短
18 教师闺蜜祝福语简短