c#反射中GetMethods()和GetCustomAttributes()方法
来源:互联网 发布:帝国文明源码下载 编辑:程序博客网 时间:2024/05/28 11:50
GetMethods()实例:
using System;using System.Reflection;using System.Reflection.Emit;// Create a class having two public methods and one protected method.public class MyTypeClass{ public void MyMethods() { } public int MyMethods1() { return 3; } protected String MyMethods2() { return "hello"; }}public class TypeMain{ public static void Main() { Type myType = (typeof(MyTypeClass)); // Get the public methods. MethodInfo[] myArrayMethodInfo = myType.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly); Console.WriteLine("\nThe number of public methods is {0}.", myArrayMethodInfo.Length); // Display all the methods. DisplayMethodInfo(myArrayMethodInfo); // Get the nonpublic methods. MethodInfo[] myArrayMethodInfo1 = myType.GetMethods(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly); Console.WriteLine("\nThe number of protected methods is {0}.", myArrayMethodInfo1.Length); // Display information for all methods. DisplayMethodInfo(myArrayMethodInfo1); Console.ReadKey(); } public static void DisplayMethodInfo(MethodInfo[] myArrayMethodInfo) { // Display information for all methods. for (int i = 0; i < myArrayMethodInfo.Length; i++) { MethodInfo myMethodInfo = (MethodInfo)myArrayMethodInfo[i]; Console.WriteLine("\nThe name of the method is {0}.", myMethodInfo.Name); } }}输出:
The number of public methods is 2.The name of the method is MyMethods.The name of the method is MyMethods1.The number of protected methods is 1.The name of the method is MyMethods2.
GetCustomAttributes()实例:
using System;using System.Reflection;using System.Reflection.Emit;// Create a class having two public methods and one protected method.public class MyTypeClass{ [System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Struct, AllowMultiple = true) // multiuse attribute ] public class Author : System.Attribute { string name; public double version; public Author(string name) { this.name = name; version = 1.0; // Default value } public string GetName() { return name; } } [Author("H. Ackerman")] private class FirstClass { // ... } // No Author attribute private class SecondClass { // ... } [Author("H. Ackerman"), Author("M. Knott", version = 2.0)] private class ThirdClass { // ... } class TestAuthorAttribute { static void Main() { PrintAuthorInfo(typeof(FirstClass)); PrintAuthorInfo(typeof(SecondClass)); PrintAuthorInfo(typeof(ThirdClass)); Console.ReadKey(); } private static void PrintAuthorInfo(System.Type t) { System.Console.WriteLine("Author information for {0}", t); System.Attribute[] attrs = System.Attribute.GetCustomAttributes(t); // reflection foreach (System.Attribute attr in attrs) { if (attr is Author) { Author a = (Author)attr; System.Console.WriteLine(" {0}, version {1:f}", a.GetName(), a.version); } } } }}
输出:
Author information for MyTypeClass+FirstClass H. Ackerman, version 1.00Author information for MyTypeClass+SecondClassAuthor information for MyTypeClass+ThirdClass H. Ackerman, version 1.00 M. Knott, version 2.00
其他用法:
using System;using System.Reflection;using System.Reflection.Emit;namespace ssss{ // Create a class having two public methods and one protected method. public class MyTypeClass:tttt.asd { public void MyMethods() { } public int MyMethods1() { return 3; } public void s() { throw new NotImplementedException(); } public void s1() { throw new NotImplementedException(); } protected String MyMethods2() { return "hello"; } } public class TypeMain { public static void Main() { TypeMain aa = new TypeMain(); Assembly assembly = aa.GetType().Assembly; Type[] types = assembly.GetTypes(); Type type = typeof(tttt.asd); for (int i = 0; i < types.Length; i++) { if(type.IsAssignableFrom(types[i])&&!types[i].IsInterface) Console.WriteLine(types[i].Name); } Console.ReadKey(); } public static void DisplayMethodInfo(MethodInfo[] myArrayMethodInfo) { // Display information for all methods. for (int i = 0; i < myArrayMethodInfo.Length; i++) { MethodInfo myMethodInfo = (MethodInfo)myArrayMethodInfo[i]; Console.WriteLine("\nThe name of the method is {0}.", myMethodInfo.Name); } } }}namespace tttt{ public interface asd { }}输出:
MyTypeClass
using System;using System.Reflection;using System.Reflection.Emit;namespace TestSpace{ public class TestClass { private string _value; public TestClass() { } public TestClass(string value) { _value = value; } public string GetValue(string prefix) { if (_value == null) return "NULL"; else return prefix + " : " + _value; } public string Value { set { _value = value; } get { if (_value == null) return "NULL"; else return _value; } } }}public class TypeMain { public static void Main() { //获取类型信息 Type t = Type.GetType("TestSpace.TestClass"); //构造器的参数 object[] constuctParms = new object[] { "timmy" }; //根据类型创建对象 object dObj = Activator.CreateInstance(t, constuctParms); //获取方法的信息 MethodInfo method = t.GetMethod("GetValue"); //调用方法的一些标志位,这里的含义是Public并且是实例方法,这也是默认的值 BindingFlags flag = BindingFlags.Public | BindingFlags.Instance; //GetValue方法的参数 object[] parameters = new object[] { "Hello" }; //调用方法,用一个object接收返回值 object returnValue = method.Invoke(dObj, flag, Type.DefaultBinder, parameters, null); Console.WriteLine(returnValue); Console.ReadKey(); } }输出:
Hello : timmy
阅读全文
0 0
- c#反射中GetMethods()和GetCustomAttributes()方法
- C#反射的GetCustomAttributes方法
- 反射中getMethods和getDelcaredMethod区别
- C# 反射通过GetCustomAttributes方法,获得自定义特性
- C# 反射通过GetCustomAttributes方法,获得自定义特性
- java反射中getDeclaredMethods和getMethods的区别
- java反射中getDeclaredMethods和getMethods的区别
- 反射中getMethods 与 getDeclaredMethods 的区别
- 反射中getMethods 与 getDeclaredMethods 的区别
- java 反射机制 之 getMethods获取所有公有方法 和 getDeclaredMethods 获取本类所有方法
- getMethods、getMethod和invoke()方法的使用
- 试用Java中的反射reflect之getDeclaredMethods和getMethods
- Java中的反射reflect之getDeclaredMethods和getMethods
- 反射之getMethods()与getDeclaredMethods()
- JAVA反射机制入门(二)--getMethods()系列方法特例详解
- getMethods和getDeclaredMethods的区别
- Typeof()与 GetType() ,获取对象的所有公有属性和所有公有方法GetProperties()GetMethods()
- C# 泛型方法中反射常用方法浅析
- Python3-搭建Django框架
- windows 2003系统下邮件系统搭建
- Flume安装-配置-调试
- workerman中使用aes加密
- MyBatis Mapper XML 符号处理
- c#反射中GetMethods()和GetCustomAttributes()方法
- nmap核心结构概述
- 几十部以程序员为主角的影片
- leetcode24. Swap Nodes in Pairs
- Java编程思想笔记-二
- 未来五年最具前景的一门编程语言是什么?
- 开发者山行图,找到你的收入和位置
- 51nod 1099 任务执行顺序 贪心的构造方法
- Web前端资源汇总