反射对象用于在运行时获取类型信息。允许访问正在运行的程序的元数据的类位于 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# 中的反射的详细内容。