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 婆婆高寿祝福语大全简短