C#中的反射是什么?
反射对象用于在运行时获取类型信息。可以访问正在运行的程序的元数据的类位于System.Reflection命名空间中。
系统的MemberInfo对象。需要初始化反射类以发现与类关联的属性。
在下面的例子中,我们设置了目标类的对象-
System.Reflection.MemberInfo info = typeof(MyClass);
这是示例-
示例
using System;
using System.Reflection;
[AttributeUsage(AttributeTargets.All)]
public class HelpAttribute :System.Attribute{
public readonly string Url;
public string Topic { //主题是一个命名参数
get {
return topic;
}
set {
topic = value;
}
}
public HelpAttribute(string url) { //url是一个位置参数
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 早上祝福语简短句子