C#中通过Type类访问数据类型信息
2014-04-15 22:25
393 查看
Type 类
表示类型声明:类类型、接口类型、数组类型、值类型、枚举类型、类型参数、泛型类型定义,以及开放或封闭构造的泛型类型。Type | 初始化 Type 类的新实例 |
1.获取给定类型的Type引用有3种方式:
a.使用typeof运算符,如Type t = typeof(int);
b.使用GetType()方法,如int i;Type t = i.GetType();
c.使用Type类的静态方法GetType(),如Type t =Type.GetType("System.Double");
2.Type的属性:
Name:数据类型名;
FullName:数据类型的完全限定名,包括命名空间;
Namespace:数据类型的命名空间;
BaseType:直接基本类型;
UnderlyingSystemType:映射类型;
3.Type的方法:
GetMethod():返回一个方法的信息;
GetMethods():返回所有方法的信息。
TestType.cs:
[c#]
view plaincopyprint?
using System;
using System.Reflection;
namespace Magci.Test.Reflection
{
public class TestType
{
public static void Main()
{
//基本数据类型
Type intType = typeof(int);
//属性
Console.WriteLine("intType.Name = " + intType.Name);
Console.WriteLine("intType.FullName = " + intType.FullName);
Console.WriteLine("intType.Namespace = " + intType.Namespace);
Console.WriteLine("intType.IsAbstract = " + intType.IsAbstract);
Console.WriteLine("intType.IsClass = " + intType.IsClass);
Console.WriteLine("intType.IsEnum = " + intType.IsEnum);
Console.WriteLine("intType.IsPrimitive = " + intType.IsPrimitive);
Console.WriteLine("intType.IsValueType = " + intType.IsValueType);
//方法
MethodInfo[] methods = intType.GetMethods();
foreach (MethodInfo method in methods)
{
Console.WriteLine(method.DeclaringType + " " + method.MemberType + " " + method.Name);
}
}
}
}
using System; using System.Reflection; namespace Magci.Test.Reflection { public class TestType { public static void Main() { //基本数据类型 Type intType = typeof(int); //属性 Console.WriteLine("intType.Name = " + intType.Name); Console.WriteLine("intType.FullName = " + intType.FullName); Console.WriteLine("intType.Namespace = " + intType.Namespace); Console.WriteLine("intType.IsAbstract = " + intType.IsAbstract); Console.WriteLine("intType.IsClass = " + intType.IsClass); Console.WriteLine("intType.IsEnum = " + intType.IsEnum); Console.WriteLine("intType.IsPrimitive = " + intType.IsPrimitive); Console.WriteLine("intType.IsValueType = " + intType.IsValueType); //方法 MethodInfo[] methods = intType.GetMethods(); foreach (MethodInfo method in methods) { Console.WriteLine(method.DeclaringType + " " + method.MemberType + " " + method.Name); } } } }
TestTypeView.cs:
[c#]
view plaincopyprint?
using System;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
namespace Magci.Test.Reflection
{
public class TestTypeView
{
public static StringBuilder OutputText = new StringBuilder();
public static void Main()
{
Type t = typeof(double);
AnalyzeType(t);
MessageBox.Show(OutputText.ToString());
}
public static void AnalyzeType(Type t)
{
//数据类型名
OutputText.Append("/nType Name: " + t.Name);
//数据类型的完全限定名,包括命名空间
OutputText.Append("/nFull Name: " + t.FullName);
//数据类型的命名空间
OutputText.Append("/nNamespace: " + t.Namespace);
//直接基本类型
Type tBase = t.BaseType;
if (tBase != null)
{
OutputText.Append("/nBase Type: " + tBase.Name);
}
//映射类型
Type tUnderlyingSystem = t.UnderlyingSystemType;
if (tUnderlyingSystem != null)
{
OutputText.Append("/nUnderlyingSystem Type: " + tUnderlyingSystem.Name);
}
//所有公共方法
OutputText.Append("/n/nPublic members:");
MemberInfo[] methods = t.GetMethods();
foreach (MemberInfo method in methods)
{
OutputText.Append("/n" + method.DeclaringType + " " + method.MemberType + "" + method.Name);
}
}
}
}
相关文章推荐
- C#中通过Type类访问数据类型信息
- C#中通过Type类访问数据类型信息
- C#中通过Type类访问数据类型信息
- 通过索引器简化C#类型信息访问
- 通过索引器简化C#类型信息访问
- OleDbType,C#,access 对应数据类型,互相对应
- C#关于通过反射PropertyType判读字符串类型方法
- C#中通过Assembly类访问程序集信息
- c# “Could not load type”(无法加载类型)错误信息
- 分享一段C#反射代码-[Type是反射的入口]--[查看类型信息]--[动态生成对象]
- MyBatis 通过包含的jdbcType类型和java中对应的数据类型
- FLEX 通过web 服务访问.NET数据信息
- C#中通过OleDb操作Oracle Blob数据类型
- RTTI(Run-Time Type Information,通过运行时类型信息) 运行期类型检查
- SQL 通过syscolumns.xtype动态查找指定数据类型字段所包含的数据
- C# 获取ORACLE SYS.XMLTYPE "遇到不支持的 Oracle 数据类型 USERDEFINED"
- 用索引器简化的C#类型信息访问
- 微信通过openID发送消息/后台post、get提交并接收数据 C# .NET 配置404,500等错误信息 连接字符串
- C# 网页信息采集(数据访问)
- 通过封装编写Book类。要求:类具有私有属性书名title、页数pageNum、类型type(默认为计算机类),并为这三个属性分别编写set和get方法。其中,页数不能少于200页,否则输出错误信息,