csharp: Use of Is and As operators in csharp
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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 | /// <summary> /// Geovin Du 20170622 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click( object sender, EventArgs e) { Object o = new Object(); System.Boolean b1 = (o is System.Object); //b1 为true System.Boolean b2 = (o is Employee); //b2为false if (o is Employee) { Employee em = (Employee)o; em.RealName = "geovindu" ; MessageBox.Show(em.RealName); } else { MessageBox.Show( "no" ); //结果为显示NO } Object obj; Employee employee = new Employee(); employee.RealName = "du" ; employee.Birthday = DateTime.Now; employee = o as Employee; obj = o as Employee; if (employee != null ) { //在if语句中使用e MessageBox.Show(employee.RealName); } else { MessageBox.Show( "no2" ); //结果为显示NO2 } } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button2_Click( object sender, EventArgs e) { var c1 = "" ; var c2 = typeof ( string ); object oc1 = c1; object oc2 = c2; var s1 = 0; var s2 = '.' ; object os1 = s1; object os2 = s2; bool b = false ; Stopwatch sw = Stopwatch.StartNew(); for ( int i = 0; i < 10000000; i++) { b = c1.GetType() == typeof ( string ); // ~60ms b = c1 is string ; // ~60ms b = c2.GetType() == typeof ( string ); // ~60ms b = c2 is string ; // ~50ms b = oc1.GetType() == typeof ( string ); // ~60ms b = oc1 is string ; // ~68ms b = oc2.GetType() == typeof ( string ); // ~60ms b = oc2 is string ; // ~64ms b = s1.GetType() == typeof ( int ); // ~130ms b = s1 is int ; // ~50ms b = s2.GetType() == typeof ( int ); // ~140ms b = s2 is int ; // ~50ms b = os1.GetType() == typeof ( int ); // ~60ms b = os1 is int ; // ~74ms b = os2.GetType() == typeof ( int ); // ~60ms b = os2 is int ; // ~68ms b = GetType1< string , string >(c1); // ~178ms b = GetType2< string , string >(c1); // ~94ms b = Is< string , string >(c1); // ~70ms b = GetType1< string , Type>(c2); // ~178ms b = GetType2< string , Type>(c2); // ~96ms b = Is< string , Type>(c2); // ~65ms b = GetType1< string , object >(oc1); // ~190ms b = Is< string , object >(oc1); // ~69ms b = GetType1< string , object >(oc2); // ~180ms b = Is< string , object >(oc2); // ~64ms b = GetType1< int , int >(s1); // ~230ms b = GetType2< int , int >(s1); // ~75ms b = Is< int , int >(s1); // ~136ms b = GetType1< int , char >(s2); // ~238ms b = GetType2< int , char >(s2); // ~69ms b = Is< int , char >(s2); // ~142ms b = GetType1< int , object >(os1); // ~178ms b = Is< int , object >(os1); // ~69ms b = GetType1< int , object >(os2); // ~178ms b = Is< int , object >(os2); // ~69ms } sw.Stop(); MessageBox.Show(sw.Elapsed.TotalMilliseconds.ToString()); } /// <summary> /// /// </summary> /// <typeparam name="S"></typeparam> /// <typeparam name="T"></typeparam> /// <param name="t"></param> /// <returns></returns> static bool GetType1<S, T>(T t) { return t.GetType() == typeof (S); } /// <summary> /// /// </summary> /// <typeparam name="S"></typeparam> /// <typeparam name="T"></typeparam> /// <param name="t"></param> /// <returns></returns> static bool GetType2<S, T>(T t) { return typeof (T) == typeof (S); } /// <summary> /// /// </summary> /// <typeparam name="S"></typeparam> /// <typeparam name="T"></typeparam> /// <param name="t"></param> /// <returns></returns> static bool Is<S, T>(T t) { return t is S; } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button3_Click( object sender, EventArgs e) { var cl1 = new Class1(); if (cl1 is IFormatProvider) { MessageBox.Show( "cl1 is IFormatProvider ok" ); } else { MessageBox.Show( "cl1 is IFormatProvider no" ); } if (cl1 is Object) { MessageBox.Show( "cl1 is Object ok" ); } else { MessageBox.Show( "cl1 is Object no" ); } if (cl1 is Class1) { MessageBox.Show( "cl1 is Class1 ok" ); } else { MessageBox.Show( "cl1 is Class1 no" ); } if (cl1 is Class2) { MessageBox.Show( "cl1 is Class2 ok" ); } else { MessageBox.Show( "cl1 is Class2 no" ); } var cl2 = new Class2(); if (cl2 is IFormatProvider) { MessageBox.Show( "cl2 is IFormatProvider ok" ); } else { MessageBox.Show( "cl2 is IFormatProvider no" ); } if (cl2 is Class2) { MessageBox.Show( "cl2 is Class2 ok" ); } else { MessageBox.Show( "cl2 is Class2 no" ); } if (cl2 is Class1) { MessageBox.Show( "cl2 is Class1 ok" ); } else { MessageBox.Show( "cl2 is Class1 no" ); } Class1 cl = cl2; if (cl is Class1) { MessageBox.Show( "cl is Class1 ok" ); } else { MessageBox.Show( "cl is Class1 no" ); }; if (cl is Class2) { MessageBox.Show( "cl is Class2 ok" ); } else { MessageBox.Show( "cl is Class2 no" ); }; bool isc1 = (cl is Class1); if (isc1) { MessageBox.Show( "true" ); } } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button4_Click( object sender, EventArgs e) { Object o = new Person( "Jane" ); ShowValue(o); o = new Dog( "Alaskan Malamute" ); ShowValue(o); Employee em = new Employee(); em.RealName = "geovindu" ; em.Birthday = DateTime.Now; o =em; ShowValue(o); } /// <summary> /// /// </summary> /// <param name="o"></param> public static void ShowValue( object o) { if (o is Person) { Person p = (Person)o; MessageBox.Show(p.Name); } else if (o is Dog) { Dog d = (Dog)o; MessageBox.Show(d.Breed); } else { MessageBox.Show( "啥也不是" ); } } } /// <summary> /// /// </summary> public class Class1 : IFormatProvider { public object GetFormat(Type t) { if (t.Equals( this .GetType())) return this ; return null ; } } /// <summary> /// /// </summary> public class Class2 : Class1 { public int Value { get ; set ; } } /// <summary> /// 员工 /// </summary> public class Employee { /// <summary> /// 真名 /// </summary> public string RealName { set ; get ; } /// <summary> /// 出生日期 /// </summary> public DateTime Birthday { set ; get ; } } /// <summary> /// /// </summary> public struct Person { public string Name { get ; set ; } public Person( string name) : this () { Name = name; } } /// <summary> /// /// </summary> public struct Dog { public string Breed { get ; set ; } public Dog( string breedName) : this () { Breed = breedName; } } |
TypeInfo,PropertyInfo,MethodInfo,FieldInfo
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 | /// <summary> /// Geovin Du 涂聚文 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button5_Click( object sender, EventArgs e) { Int32 indent = 0; // Display information about each assembly loading into this AppDomain. foreach (Assembly b in AppDomain.CurrentDomain.GetAssemblies()) { Display(indent, "Assembly: {0}" , b); // Display information about each module of this assembly. foreach (Module m in b.GetModules( true )) { Display(indent + 1, "Module: {0}" , m.Name); } // Display information about each type exported from this assembly. indent += 1; foreach (Type t in b.GetExportedTypes()) { Display(0, "" ); Display(indent, "Type: {0}" , t); // For each type, show its members & their custom attributes. indent += 1; foreach (MemberInfo mi in t.GetMembers()) { Display(indent, "Member: {0}" , mi.Name); DisplayAttributes(indent, mi); // If the member is a method, display information about its parameters. if (mi.MemberType == MemberTypes.Method) { foreach (ParameterInfo pi in ((MethodInfo)mi).GetParameters()) { Display(indent + 1, "Parameter: Type={0}, Name={1}" , pi.ParameterType, pi.Name); } } // If the member is a property, display information about the property's accessor methods. if (mi.MemberType == MemberTypes.Property) { foreach (MethodInfo am in ((PropertyInfo)mi).GetAccessors()) { Display(indent + 1, "Accessor method: {0}" , am); } } } indent -= 1; } indent -= 1; } } /// <summary> /// Displays the custom attributes applied to the specified member. /// </summary> /// <param name="indent"></param> /// <param name="mi"></param> public static void DisplayAttributes(Int32 indent, MemberInfo mi) { // Get the set of custom attributes; if none exist, just return. object [] attrs = mi.GetCustomAttributes( false ); if (attrs.Length == 0) { return ; } // Display the custom attributes applied to this member. Display(indent + 1, "Attributes:" ); foreach ( object o in attrs) { Display(indent + 2, "{0}" , o.ToString()); } } /// <summary> /// Display a formatted string indented by the specified amount. /// </summary> /// <param name="indent"></param> /// <param name="format"></param> /// <param name="param"></param> public static void Display(Int32 indent, string format, params object [] param) { string st = new string ( ' ' , indent * 2); MessageBox.Show(st); string s= string .Format( "format:{0},param:{1}." ,format, param); MessageBox.Show(s); } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button6_Click( object sender, EventArgs e) { // System.Reflection.MemberTypes //4.6,4.5 .net //TypeInfo t = typeof(Calendar).GetTypeInfo(); //4.6,4.5 // IEnumerable<PropertyInfo> pList = t.DeclaredProperties; //4.5,4.6 //IEnumerable<MethodInfo> mList = t.DeclaredMethods; //4.0 IEnumerable<PropertyInfo> pList = typeof (Calendar).GetProperties(); IEnumerable<MethodInfo> mList = typeof (Calendar).GetMethods(); IEnumerable<FieldInfo> fList = typeof (Calendar).GetFields(); StringBuilder sb = new StringBuilder(); sb.Append( "Properties:" ); foreach (PropertyInfo p in pList) { sb.Append( "\n" + p.DeclaringType.Name + ": " + p.Name); } sb.Append( "\nMethods:" ); foreach (MethodInfo m in mList) { sb.Append( "\n" + m.DeclaringType.Name + ": " + m.Name); } MessageBox.Show(sb.ToString()); } |
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
分类:
CSharp code
标签:
csharp
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!