Csharp: Use of Is and As operators in csharp, csharpoperators
/// <Summary> /// Geovin Du 20170622 /// </summary> /// <param name = "sender"> </param> /// <param name = "e"> </param> private void button#click (object sender, eventArgs e) {Object o = new Object (); System. boolean b1 = (o is System. object); // b1 is true System. boolean b2 = (o is Employee); // b2 is false if (o is Employee) {Employee em = (Employee) o; em. realName = "geovindu"; MessageBox. show (em. realName );} Else {MessageBox. show ("no"); // The result is 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) {// use e MessageBox in the if statement. show (employee. realName);} else {MessageBox. show ("no2 "); // The result is displayed as 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 = f Alse; Stopwatch sw = Stopwatch. startNew (); for (int I = 0; I <10000000; I ++) {B = c1.GetType () = typeof (string );//~ 60 ms B = c1 is string ;//~ 60 ms B = c2.GetType () = typeof (string );//~ 60 ms B = c2 is string ;//~ 50 ms B = oc1.GetType () = typeof (string );//~ 60 ms B = oc1 is string ;//~ 68 ms B = oc2.GetType () = typeof (string );//~ 60 ms B = oc2 is string ;//~ 64 ms B = s1.GetType () = typeof (int );//~ 130 ms B = s1 is int ;//~ 50 ms B = s2.GetType () = typeof (int );//~ 140 ms B = s2 is int ;//~ 50 ms B = os1.GetType () = typeof (int );//~ 60 ms B = os1 is int ;//~ 74 ms B = os2.GetType () = typeof (int );//~ 60 ms B = os2 is int ;//~ 68 ms B = GetType1 <string, string> (c1 );//~ 178 ms B = GetType2 <string, string> (c1 );//~ 94 ms B = Is <string, string> (c1 );//~ 70 ms B = GetType1 <string, Type> (c2 );//~ 178 ms B = GetType2 <string, Type> (c2 );//~ 96 ms B = Is <string, Type> (c2 );//~ 65 ms B = GetType1 <string, object> (oc1 );//~ 190 ms B = Is <string, object> (oc1 );//~ 69 ms B = GetType1 <string, object> (oc2 );//~ 180 ms B = Is <string, object> (oc2 );//~ 64 ms B = GetType1 <int, int> (s1 );//~ 230 ms B = GetType2 <int, int> (s1 );//~ 75 ms B = Is <int, int> (s1 );//~ 136 ms B = GetType1 <int, char> (s2 );//~ 238 ms B = GetType2 <int, char> (s2 );//~ 69 ms B = Is <int, char> (s2 );//~ 142 ms B = GetType1 <int, object> (os1 );//~ 178 ms B = Is <int, object> (os1 );//~ 69 ms B = GetType1 <int, object> (os2 );//~ 178 ms B = Is <int, object> (os2 );//~ 69 ms} 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 ("nothing") ;}}/// <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> // Employee // </summary> public class Employee {// <summary> // real name // </summary> public string realName {set; get ;}/// <summary> /// Date of Birth /// </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
/// <Summary> /// Geovin Du tu juwen // </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 M 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) {// specify 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 ());}