csharp: Use of Is and As operators in csharp
/// <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
/// <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());
}
csharp: Use of Is and As operators in csharp的更多相关文章
- What's Assembly - CSharp - Editor - first pass.dll? Best How to Fix Assembly - CSharp - Editor - first pass.dll Error Guide
If you've found yourself here, I'm guessing that you're getting Assembly - CSharp - Editor - first p ...
- 德卡Z90读卡器读取社保卡,德卡Z90读卡器CSharp示例程序源码
前言,最近学习调用 医保卡业务,使用德卡读卡器,主要就是调用一个DLL,动态库文件. 借着自学的机会把心得体会都记录下来,方便感兴趣的小伙伴学习与讨论. 内容均系原创,欢迎大家转载分享,但转载的同时别 ...
- CSharp使用ANTLR4生成简单计算Parser
ANTLR简介 ANTLR (ANother Tool for Language Recognition) is a powerful parser generator for reading, pr ...
- VS2012中丢失ArcGIS模板的解决方法
VS2012中丢失ArcGIS模板的解决方法 由于ArcGIS10.0(for .NET)默认是用VS2010作为开发工具的,所以在先安装VS2012后装ArcGIS10.0 桌面版及ArcObjec ...
- C# EF增删改查
1.增 //1.创建一个EF数据上下文对象 MyDBEntities context=new MyDBEntities(); //2.将要添加的数据,封装成对象 Users user = new Us ...
- Python字符串格式化
一.使用格式化符来格式化字符串: Python支持的所有格式化符 格式化符 意义 'd' 返回要格式化对象的十进制表示,如果可以 'i' 返回要格式化对象的十进制表示,如果可以 'o' 返回要格式化对 ...
- mvc Razor 视图中找不到 ViewBag的定义
在Razor 视图中,我们有时会看到 ViewBag.Title 下会划一个红线,当鼠标放上去的时候会提示这样的一个错误: 找不到编译动态表达式所需的一种或多种类型,是否缺少引用? 但在项目启动运行时 ...
- 在VisualStudio2012环境下安装ArcEngine 10.0
因为ArcEngine10.0默认对应的开发工具为VS2010,在安装了VS2012的情况下安装ArcEngine10.0(注意:我自己的环境为VS2012和ArcEngine10.0,对于其他版本在 ...
- 使用XmlHelper添加节点C#代码
接着上一篇:http://keleyi.com/a/bjac/ttssua0f.htm在前篇文章中,给出了C# XML文件操作类XmlHelper的代码,以及使用该类的一个例子,即使用XmlHelpe ...
随机推荐
- Oracle服务器和客户端安装在同一台机器的情况
最近重装了系统,所有的开发环境需要重新部署一下,因此重新安装了Oracle,结果原来没有问题,这一次又碰到了几个问题(tns12154和tns03505),让我好一搞啊.不过又重新对Oracle加深了 ...
- ng4 路由多参数传参以及接收
import { Router } from '@angular/router'; constructor( private router:Router, ) { } goApplicationDet ...
- 数据结构(一): 键值对 Map
Map基本介绍 Map 也称为:映射表/关联数组,基本思想就是键值对的关联,可以用键来查找值. Java标准的类库包含了Map的几种基本的实现,包括:HashMap,TreeMap,LinkedHas ...
- 《你不知道的javascript》读书笔记2
概述 放假读完了<你不知道的javascript>上篇,学到了很多东西,记录下来,供以后开发时参考,相信对其他人也有用. 这篇笔记是这本书的下半部分,上半部分请见<你不知道的java ...
- MySQL:基础架构和工作流程
[参考文章]:01|基础架构:一条查询语句的执行流程 1. 基本架构 大体来说,MySQL可以分为Server层和存储引擎两部分. Server层包括链接器,分析器,优化器,执行器等,涵盖大多数核心服 ...
- MongoDB索引管理-索引的创建、查看、删除
索引是提高查询查询效率最有效的手段.索引是一种特殊的数据结构,索引以易于遍历的形式存储了数据的部分内容(如:一个特定的字段或一组字段值),索引会按一定规则对存储值进行排序,而且索引的存储位置在内存中, ...
- Ubuntu下安装程序的三种方法(转)
引言 在Ubuntu当中,安装应用程序我所知道的有三种方法,分别是apt-get,dpkg安装deb和make install安装源码包三种.下面针对每一种方法各举例来说明. 一.apt-get方法 ...
- 常用的npm指令总结
一.安装指令,通常是全局安装 npm install <package name> -g 二.移除全局安装包 npm uninstall <package name> -g 三 ...
- Java 面试基础总结(一)
1.九种基本数据类型的大小以及它们的封装类 java提供的九种基本数据类型:boolean.byte(1).char(2).short(2).int(4).long(8).float(4).doubl ...
- hdfs-03-hdfs客户端操作
1, hdfs中两种连接方式 1), 网页 2), 客户端 文件的切块大小和存储的副本数量,都是由客户端决定! 所谓的由客户端决定,是通过配置参数来定的 hdfs的客户端会读以下两个参数,来决定切块大 ...