【转】C# typeof()实例详解

摘要:
System.Typetype=typeof(int);inti=0;System.Typetype=i.GetType();使用System.Reflection;publicvoidSampleMethod(){}staticvoidMain(){Typet=typeof(SampleClass);

转自:http://www.cnblogs.com/lm3515/archive/2010/09/02/1815725.html

typeof(C# 参考)

用于获取类型的 System.Type 对象。typeof 表达式采用以下形式:

System.Type type = typeof(int);
【转】C# typeof()实例详解第1张 备注

若要获取表达式的运行时类型,可以使用 .NET Framework 方法 GetType,如下所示:

int i = 0;
System.Type type = i.GetType();

typeof 运算符也能用于公开的泛型类型。具有不止一个类型参数的类型的规范中必须有适当数量的逗号。不能重载 typeof 运算符。

示例 
// cs_operator_typeof.cs
using System;
using System.Reflection;
public class SampleClass
{
public int sampleMember;
public void SampleMethod() {}
static void Main()
{
Type t = typeof(SampleClass);
// Alternatively, you could use
// SampleClass obj = new SampleClass();
// Type t = obj.GetType();
Console.WriteLine("Methods:");
MethodInfo[] methodInfo = t.GetMethods();
foreach (MethodInfo mInfo in methodInfo)
Console.WriteLine(mInfo.ToString());
Console.WriteLine("Members:");
MemberInfo[] memberInfo = t.GetMembers();
foreach (MemberInfo mInfo in memberInfo)
Console.WriteLine(mInfo.ToString());
}
}
输出
Methods:
Void SampleMethod()
System.Type GetType()
System.String ToString()
Boolean Equals(System.Object)
Int32 GetHashCode()
Members:
Void SampleMethod()
System.Type GetType()
System.String ToString()
Boolean Equals(System.Object)
Int32 GetHashCode()
Void .ctor()
Int32 sampleMember
此示例使用 GetType 方法确定用来包含数值计算的结果的类型。这取决于结果数字的存储要求。
 
// cs_operator_typeof2.cs
using System;
class GetTypeTest
{
static void Main()
{
int radius = 3;
Console.WriteLine("Area = {0}", radius * radius * Math.PI);
Console.WriteLine("The type is {0}",
(radius * radius * Math.PI).GetType()
);
}
}
输出
Area = 28.2743338823081
The type is System.Double

  

免责声明:文章转载自《【转】C# typeof()实例详解》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇C#学习笔记(6)——大项目增删改查IOS UIImage类方法总结下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

[C]x字符转义序列

概述       x转义的定义是这样的 转义符 字符值 输出结果 xh[h...] 具有此十六进制码的字符 输出此字符 问题      看似x后面可以接受1或n个十六进制的字符,但是如果你把一个超过ff分十六进制数赋值给一个char变量,会得到一个"Out of range"的提示; #include <stdio.h> #incl...

用c#开发微信(3)基于Senparc.Weixin框架的接收普通消息处理 (源码下载)

本文讲述使用Senparc.Weixin框架来快速处理各种接收的普通消息。这里的消息指的是传统的微信公众平台消息交互,微信用户向公众号发送消息后,公众号回复消息给微信用户。包括以下7种类型: 1 文本消息 2 图片消息 3 语音消息 4 视频消息 5 小视频消息 6 地理位置消息 7 链接消息 实现非常简单,自定义一个继承MessageHandler的类,...

Boost库简介

Boost库由C++标准委员会库工作组成员发起,其中有些内容有望成为下一代C++标准库内容。在C++社区中影响甚大,是不折不扣的“准”标准库。 字符串和文本处理库 Conversion库:对C++类型转换的增强,提供更强的类型安全转换、更高效的类型安全保护、进行范围检查的数值转换和词法转换。 Format库:实现类似printf的格式化对象,可以把参数格式化...

整理了几种字符串截取方法

一、 (Substring);(Remove);(Replace) 1、取字符串的前i个字符 (1)string str1=str.Substring(0,i); (2)string str1=str.Remove(i,str.Length-i); 2、去掉字符串的前i个字符 string str1=str.Remove(0,i); string st...

【封装那些事】 未利用封装

未利用封装 客户代码使用显式类型检查(使用一系列if-else或switch语句检查对象的类型),而不利用出层次结构内已封装的类型变化时,将导致这种坏味。 为什么要利用封装? 一种臭名昭著的坏味是,在客户代码中使用条件语句(if-else或switch语句)来显式地检查类型,并根据类型执行相应的操作。我们这里讨论的是:要检查的类型都封装在了层次结构中,但...

spring boot 实现多个 interceptor 并指定顺序

首先我们创建Interceptor,实现HandlerInterceptor覆写方法:一、下面我创建了三个拦截器:MyInterceptor,UserInterceptor,StudentInterceptor @Componentpublic class MyInterceptor implements HandlerInterceptor { @Ov...