C#中通过Type类访问数据类型信息
2009-08-19 11:00
344 查看
C#中通过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:
01.
using
System;
02.
using
System.Reflection;
03.
04.
namespace
Magci.Test.Reflection
05.
{
06.
public
class
TestType
07.
{
08.
public
static
void
Main()
09.
{
10.
//基本数据类型
11.
Type intType =
typeof
(
int
);
12.
13.
//属性
14.
Console.WriteLine(
"intType.Name = "
+ intType.Name);
15.
Console.WriteLine(
"intType.FullName = "
+ intType.FullName);
16.
Console.WriteLine(
"intType.Namespace = "
+ intType.Namespace);
17.
Console.WriteLine(
"intType.IsAbstract = "
+ intType.IsAbstract);
18.
Console.WriteLine(
"intType.IsClass = "
+ intType.IsClass);
19.
Console.WriteLine(
"intType.IsEnum = "
+ intType.IsEnum);
20.
Console.WriteLine(
"intType.IsPrimitive = "
+ intType.IsPrimitive);
21.
Console.WriteLine(
"intType.IsValueType = "
+ intType.IsValueType);
22.
23.
//方法
24.
MethodInfo[] methods = intType.GetMethods();
25.
foreach
(MethodInfo method
in
methods)
26.
{
27.
Console.WriteLine(method.DeclaringType +
" "
+ method.MemberType +
" "
+ method.Name);
28.
}
29.
}
30.
}
31.
}
TestTypeView.cs:
01.
using
System;
02.
using
System.Text;
03.
using
System.Windows.Forms;
04.
using
System.Reflection;
05.
06.
namespace
Magci.Test.Reflection
07.
{
08.
public
class
TestTypeView
09.
{
10.
public
static
StringBuilder OutputText =
new
StringBuilder();
11.
12.
public
static
void
Main()
13.
{
14.
Type t =
typeof
(
double
);
15.
AnalyzeType(t);
16.
MessageBox.Show(OutputText.ToString());
17.
}
18.
19.
public
static
void
AnalyzeType(Type t)
20.
{
21.
//数据类型名
22.
OutputText.Append(
"/nType Name: "
+ t.Name);
23.
//数据类型的完全限定名,包括命名空间
24.
OutputText.Append(
"/nFull Name: "
+ t.FullName);
25.
//数据类型的命名空间
26.
OutputText.Append(
"/nNamespace: "
+ t.Namespace);
27.
28.
//直接基本类型
29.
Type tBase = t.BaseType;
30.
if
(tBase !=
null
)
31.
{
32.
OutputText.Append(
"/nBase Type: "
+ tBase.Name);
33.
}
34.
35.
//映射类型
36.
Type tUnderlyingSystem = t.UnderlyingSystemType;
37.
if
(tUnderlyingSystem !=
null
)
38.
{
39.
OutputText.Append(
"/nUnderlyingSystem Type: "
+ tUnderlyingSystem.Name);
40.
}
41.
42.
//所有公共方法
43.
OutputText.Append(
"/n/nPublic members:"
);
44.
MemberInfo[] methods = t.GetMethods();
45.
foreach
(MemberInfo method
in
methods)
46.
{
47.
OutputText.Append(
"/n"
+ method.DeclaringType +
" "
+ method.MemberType +
""
+ method.Name);
48.
}
49.
}
50.
}
51.
}
相关文章推荐
- C#中通过Type类访问数据类型信息
- C#中通过Type类访问数据类型信息
- C#中通过Type类访问数据类型信息
- 通过索引器简化C#类型信息访问
- 通过索引器简化C#类型信息访问
- c# 通过访问URL,获取并解析远程服务端返回的复杂Json数据
- OleDbType,C#,access 对应数据类型,互相对应
- MyBatis 通过包含的jdbcType类型和java中对应的数据类型
- RTTI(Run-Time Type Information,通过运行时类型信息) 运行期类型检查
- c# “Could not load type”(无法加载类型)错误信息
- C#中通过Assembly类访问程序集信息
- 用索引器简化的C#类型信息访问
- 获得某列的数据类型(DataType)等信息
- C#通过API获取接收数据窗体控件句柄,并向该句柄发送文本信息
- C# 获取ORACLE SYS.XMLTYPE "遇到不支持的 Oracle 数据类型 USERDEFINED"
- C#中通过OleDb操作Oracle Blob数据类型
- C# 网页信息采集(数据访问)
- C#关于通过反射PropertyType判读字符串类型方法
- C# 网页信息采集(数据访问)