typeof 和 GetType() 有啥区别??

yuna106 2004-11-06 12:52:54
typeof 和 GetType() 有啥区别??
...全文
769 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
好贴!!!!!
fancyhsq 2005-04-10
  • 打赏
  • 举报
回复
接分好了..
muziruyue 2005-04-10
  • 打赏
  • 举报
回复
来晚一步,接分
hawk234 2005-04-10
  • 打赏
  • 举报
回复
upp
g961681 2005-04-10
  • 打赏
  • 举报
回复
好贴
happyjun2000 2005-04-10
  • 打赏
  • 举报
回复
受教
CWYCN 2005-04-10
  • 打赏
  • 举报
回复
UP
minghui000 2005-04-10
  • 打赏
  • 举报
回复
接分
linguicheng 2005-01-10
  • 打赏
  • 举报
回复
来晚一步,接分
morality 2004-12-29
  • 打赏
  • 举报
回复
typeof 运算符用于获得某一类型的 System.Type 对象。typeof 表达式采用以下形式:

typeof(type)
其中:

type
要获得其 System.Type 对象的类型。

备注
不能重载 typeof 运算符。
若要获得一个表达式的运行时类型,可以使用 .NET Framework 方法 GetType。
示例
// cs_operator_typeof.cs
// Using typeof operator
using System;
using System.Reflection;

public class MyClass
{
public int intI;
public void MyMeth()
{
}

public static void Main()
{
Type t = typeof(MyClass);

// alternatively, you could use
// MyClass t1 = new MyClass();
// Type t = t1.GetType();

MethodInfo[] x = t.GetMethods();
foreach (MethodInfo xtemp in x)
{
Console.WriteLine(xtemp.ToString());
}

Console.WriteLine();

MemberInfo[] x2 = t.GetMembers();
foreach (MemberInfo xtemp2 in x2)
{
Console.WriteLine(xtemp2.ToString());
}
}
}
输出
Int32 GetHashCode()
Boolean Equals(System.Object)
System.String ToString()
Void MyMeth()
Void Main()
System.Type GetType()

Int32 intI
Int32 GetHashCode()
Boolean Equals(System.Object)
System.String ToString()
Void MyMeth()
Void Main()
System.Type GetType()
Void .ctor()
示例
// cs_operator_typeof2.cs
// Using GetType method
using System;
class GetTypeTest
{
public 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

//////////////////////////////////
Object.GetType 方法

获取当前实例的 Type。

返回值
Type 实例,表示当前实例的确切运行时类型。
备注
对于具有相同运行时类型的两个对象 x 和 y,Object.ReferenceEquals(x.GetType(),y.GetType()) 返回 true。
Type 对象公开与当前 Object 的类关联的元数据。
示例
[C#, JScript] 下列代码示例说明 GetType 返回当前实例的运行时类型。
[C#]
using System;

public class MyBaseClass: Object {
}

public class MyDerivedClass: MyBaseClass {
}

public class Test {

public static void Main() {
MyBaseClass myBase = new MyBaseClass();
MyDerivedClass myDerived = new MyDerivedClass();
object o = myDerived;
MyBaseClass b = myDerived;

Console.WriteLine("mybase: Type is {0}", myBase.GetType());
Console.WriteLine("myDerived: Type is {0}", myDerived.GetType());
Console.WriteLine("object o = myDerived: Type is {0}", o.GetType());
Console.WriteLine("MyBaseClass b = myDerived: Type is {0}", b.GetType());
}
}


/*

This code produces the following output.

mybase: Type is MyBaseClass
myDerived: Type is MyDerivedClass
object o = myDerived: Type is MyDerivedClass
MyBaseClass b = myDerived: Type is MyDerivedClass

*/
a7lang 2004-12-29
  • 打赏
  • 举报
回复
澄清概念
xxdneu 2004-11-06
  • 打赏
  • 举报
回复
一个是语句一个是object的方法,typeof是语句,根据类名来得到Type对象,而getType是个方法,是对象的方法

Type t = typeof(String);

String s="";
Type t = s.GetType()
Yok 2004-11-06
  • 打赏
  • 举报
回复
typeof(类名)
变量名.GetType()
lonelydreamsym 2004-11-06
  • 打赏
  • 举报
回复
看看,谢了

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧