CSharp: Reflection in donet 6
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace GeovinduDu { [AttributeUsage( AttributeTargets.Class | AttributeTargets.Constructor | AttributeTargets.Field | AttributeTargets.Method | AttributeTargets.Property, AllowMultiple = true )] public class HelpAttribute : Attribute { ///// <summary> ///// ///// </summary> ///// <param name="Description_in"></param> //public HelpAttribute(String Description_in, string author) //{ // this.description = Description_in; // this.author = author; //} /// <summary> /// /// </summary> /// <param name="Description_in"></param> /// <param name="author"></param> /// <param name="bg"></param> /// <param name="dev"></param> /// <param name="d"></param> public HelpAttribute(String Description_in, string author, int bg, string dev, string d) { this .description = Description_in; this .author = author; this .bugNo = bg; this .developer = dev; this .lastReview = d; } /// <summary> /// /// </summary> protected String description; protected string author; /// <summary> /// /// </summary> public String Description { get { return this .description; } } /// <summary> /// /// </summary> public String Author { get { return this .author; } } private int bugNo; private string developer; private string lastReview; public string message; ///// <summary> ///// ///// </summary> ///// <param name="bg"></param> ///// <param name="dev"></param> ///// <param name="d"></param> //public HelpAttribute(int bg, string dev, string d) //{ // this.bugNo = bg; // this.developer = dev; // this.lastReview = d; //} /// <summary> /// /// </summary> public int BugNo { get { return bugNo; } } /// <summary> /// 开发者 /// </summary> public string Developer { get { return developer; } } /// <summary> /// /// </summary> public string LastReview { get { return lastReview; } } /// <summary> /// /// </summary> public string Message { get { return message; } set { message = value; } } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Diagnostics; using static System.Console; namespace GeovinduDu { /// <summary> /// /// </summary> [Help( "这是一门无所事事的对象" , "geovindu" ,1, "geovindu" , "涂聚文" ,message = "message" )] [Help( "它包含一个什么都不做的方法" , "geovindu" ,1, "geovindu" , "涂聚文" , message = "message" )] [Help(description: "geovindu" ,author: "geovindu" ,developer: "geovindu" ,bugNo:1,lastReview: "geovindu" ,message = "geovindu" )] public class geovindu { /// <summary> /// /// </summary> /// <param name="inputvalue"></param> [Help( "这是一种什么都不做的方法" , "geovindu" ,2, "geovindu" , "涂聚文" , message = "message" )] [Help(description: "geovindu" , author: "geovindu" , developer: "geovindu" , bugNo: 1, lastReview: "geovindu" , message = "geovindu" )] public void DuMethod( string inputvalue) { WriteLine( "geovindu,Geovin Du,涂聚文" +inputvalue); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | using static System.Console; using System.Reflection; using System; using System.Diagnostics; using System.Runtime.InteropServices; namespace GeovinduDu { /// <summary> /// /// </summary> public class Program { /// <summary> /// /// </summary> /// <param name="args"></param> public static void Main( string [] args) { int a = 10; // 00001010 int b = 6; // 00000110 WriteLine($ "a = {a}" ); WriteLine($ "b = {b}" ); WriteLine($ "a & b = {a & b}" ); // 2-bit column only WriteLine($ "a | b = {a | b}" ); // 8, 4, and 2-bit columns WriteLine($ "a ^ b = {a ^ b}" ); // 8 and 4-bit columns string s = $ "a ^ b = {a ^ b}" .ToString(); WriteLine( "geovindu:" + s); // 01010000 left-shift a by three bit columns WriteLine($ "a << 3 = {a << 3}" ); // multiply a by 8 WriteLine($ "a * 8 = {a * 8}" ); // 00000011 right-shift b by one bit column WriteLine($ "b >> 1 = {b >> 1}" ); WriteLine(); WriteLine( "Outputting integers as binary:" ); WriteLine($ "a = {ToBinaryString(a)}" ); WriteLine($ "b = {ToBinaryString(b)}" ); WriteLine($ "a & b = {ToBinaryString(a & b)}" ); WriteLine($ "a | b = {ToBinaryString(a | b)}" ); WriteLine($ "a ^ b = {ToBinaryString(a ^ b)}" ); WriteLine(); Assembly? assembly = Assembly.GetEntryAssembly(); if (assembly == null ) return ; // loop through the assemblies that this app references foreach (AssemblyName name in assembly.GetReferencedAssemblies()) { // load the assembly so we can read its details Assembly Dugeovindu = Assembly.Load(name); // declare a variable to count the number of methods int methodCount = 0; string methodname = "" ; // loop through all the types in the assembly foreach (TypeInfo t in Dugeovindu.DefinedTypes) { // add up the counts of methods methodCount += t.GetMethods().Count(); } foreach ( var propertyInfo in Dugeovindu.GetType().GetProperties(BindingFlags.NonPublic | BindingFlags.Instance)) { methodname = propertyInfo.Name; } //output the count of types and their methods // WriteLine("{0:N0} types with {1:N0} methods in {2} assembly {3}.", arg0: Dugeovindu.DefinedTypes.Count(), arg1: methodCount, arg2: name.Name); WriteLine(name.Name); } System.Console.WriteLine(); System.Reflection.MemberInfo info = typeof (geovindu); object [] attributes = info.GetCustomAttributes( true ); for ( int i = 0; i < attributes.Length; i++) { System.Console.WriteLine(attributes[i]); } System.Console.WriteLine(); var Duattributes = Attribute.GetCustomAttributes( typeof (geovindu), typeof (HelpAttribute)); if (attributes.Any()) { foreach ( var attribute in Duattributes) { Console.WriteLine( "Attribute Value: " + ((HelpAttribute)attribute).Description); Console.WriteLine( "Attribute Value: " + ((HelpAttribute)attribute).Author); Console.WriteLine( "Type ID: " + attribute.TypeId); } } System.Console.WriteLine(); foreach ( object attrib in info.GetCustomAttributes( true )) { Console.WriteLine( "Attribute Value: " + ((HelpAttribute)attrib).Description); Console.WriteLine( "Attribute Value: " + ((HelpAttribute)attrib).Author); } System.Console.WriteLine(); MethodInfo[] methodInfos = typeof (geovindu).GetMethods(); // writes all the property names foreach (MethodInfo methodInfo in methodInfos) { Console.WriteLine(methodInfo.Name + "... " ); var values = methodInfo.GetParameters(); //方法中的参数 if (values != null ) { foreach (ParameterInfo dus in values) { Console.WriteLine(dus.Name + ":" + dus.ParameterType.Name); } } } System.Console.WriteLine(); // 遍历方法特性 foreach (MethodInfo m in methodInfos) { foreach (Attribute du in m.GetCustomAttributes( true )) { HelpAttribute geovin = du as HelpAttribute; // if ( null != geovin) { Console.WriteLine( "Bug no: {0}, for Method: {1}" , geovin.BugNo, m.Name); Console.WriteLine( "Developer: {0}" , geovin.Developer); Console.WriteLine( "Last Reviewed: {0}" , geovin.LastReview); Console.WriteLine( "Remarks: {0}" , geovin.Message); } } } } /// <summary> /// /// </summary> /// <param name="value"></param> /// <returns></returns> static string ToBinaryString( int value) { return Convert.ToString(value, toBase: 2).PadLeft(8, '0' ); } } } |
输出:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | a = 10 b = 6 a & b = 2 a | b = 14 a ^ b = 12 geovindu:a ^ b = 12 a << 3 = 80 a * 8 = 80 b >> 1 = 3 Outputting integers as binary: a = 00001010 b = 00000110 a & b = 00000010 a | b = 00001110 a ^ b = 00001100 System.Runtime System.Console System.Linq GeovinduDu.HelpAttribute GeovinduDu.HelpAttribute GeovinduDu.HelpAttribute Attribute Value: 这是一门无所事事的对象 Attribute Value: geovindu Type ID: GeovinduDu.HelpAttribute Attribute Value: 它包含一个什么都不做的方法 Attribute Value: geovindu Type ID: GeovinduDu.HelpAttribute Attribute Value: geovindu Attribute Value: geovindu Type ID: GeovinduDu.HelpAttribute Attribute Value: 这是一门无所事事的对象 Attribute Value: geovindu Attribute Value: 它包含一个什么都不做的方法 Attribute Value: geovindu Attribute Value: geovindu Attribute Value: geovindu DuMethod... inputvalue:String GetType... ToString... Equals... obj:Object GetHashCode... Bug no: 2, for Method: DuMethod Developer: geovindu Last Reviewed: 涂聚文 Remarks: message Bug no: 1, for Method: DuMethod Developer: geovindu Last Reviewed: geovindu Remarks: geovindu |
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
分类:
CSharp code
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
2015-10-29 javascript: detect mobile devices or browser
2013-10-29 sql: 生日赠品中的相关算法