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 生日广州男生祝福语简短