关于Object类的GetType()方法

cbspy 2003-12-14 08:53:41
.Net中,object类的GetType()方法不是虚方法,也就是说派生类不能通过重写它来给客户端隐藏自己的类型,既然不能重写,那么我想知道派生类什么时候有机会可以提供自己正确的类型信息?另外如果这样,我对派生类调用这个GetType()方法,得到的是不是仍然是object的类型?这样就不对了,很疑惑,希望高手来帮我解释一下。
...全文
360 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
mmkk 2003-12-15
  • 打赏
  • 举报
回复
这只能说明通过GetType来获取instance的真实类型有点不太可靠,而且可以很容易的绕过这个GetType方法来获取instance的真实类型阿
还是上面那个代码
Type.GetType(instance.ToString());
这样就可以了
cbspy 2003-12-15
  • 打赏
  • 举报
回复
谢谢mmkk()帮我澄清疑惑~:)
cbspy 2003-12-14
  • 打赏
  • 举报
回复
既然这样,那么也就是说类型是不安全的了。
mmkk 2003-12-14
  • 打赏
  • 举报
回复
的确是可以通过new一个新的GetType函数来使得派生类调用此方法.
class MyTestGetType
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//

MyTestGetType instance = new MyTestGetType();
Console.WriteLine(instance.GetType());
}

public new Type GetType()
{
return typeof(System.String);
}
}

结果将显示为System.String,而不是Namespace_Name.MyTestGetType(Namespace上面代码中没有体现出来)
zhengguoc 2003-12-14
  • 打赏
  • 举报
回复
来个例子,一切便知,来也!
using System;
using System.Reflection;
using System.Security;

public class MyClass1
{
public MyClass1(int i){}
public static void Main()
{
try
{
Type myType = typeof(MyClass1);
Type[] types = new Type[1];
types[0] = typeof(int);
// Get the public instance constructor that takes an integer parameter.
ConstructorInfo constructorInfoObj = myType.GetConstructor(
BindingFlags.Instance | BindingFlags.Public, null,
CallingConventions.HasThis, types, null);
if(constructorInfoObj != null)
{
Console.WriteLine("The constructor of MyClass1 that is a public " +
"instance method and takes an integer as a parameter is: ");
Console.WriteLine(constructorInfoObj.ToString());
}
else
{
Console.WriteLine("The constructor of MyClass1 that is a public instance " +
"method and takes an integer as a parameter is not available.");
}
}
catch(ArgumentNullException e)
{
Console.WriteLine("ArgumentNullException: " + e.Message);
}
catch(ArgumentException e)
{
Console.WriteLine("ArgumentException: " + e.Message);
}
catch(SecurityException e)
{
Console.WriteLine("SecurityException: " + e.Message);
}
catch(Exception e)
{
Console.WriteLine("Exception: " + e.Message);
}
}
}

[C++]
#using <mscorlib.dll>

using namespace System;
using namespace System::Reflection;
using namespace System::Security;

public __gc class MyClass1 {
public:
MyClass1(int i) {}
}
;
int main() {
try {
Type* myType = __typeof(MyClass1);
Type* types[] = new Type*[1];
types->Item[0] = __typeof(int);
// Get the public instance constructor that takes an integer parameter.
ConstructorInfo* constructorInfoObj = myType->GetConstructor(static_cast<BindingFlags>(BindingFlags::Instance | BindingFlags::Public), 0,
CallingConventions::HasThis, types, 0);
if (constructorInfoObj != 0) {
Console::WriteLine(S"The constructor of MyClass1 that is a public instance method and takes an integer as a parameter is: ");
Console::WriteLine(constructorInfoObj);
} else {
Console::WriteLine(S"The constructor of MyClass1 that is a public instance method and takes an integer as a parameter is not available.");
}
} catch (ArgumentNullException* e) {
Console::WriteLine(S"ArgumentNullException: {0}", e->Message);
} catch (ArgumentException* e) {
Console::WriteLine(S"ArgumentException: {0}", e->Message);
} catch (SecurityException* e) {
Console::WriteLine(S"SecurityException: {0}", e->Message);
} catch (Exception* e) {
Console::WriteLine(S"Exception: {0}", e->Message);
}
}

[JScript] 没有可用于 JScript 的示例。若要查看 Visual Basic、C# 或 C++ 示例,请单击页左上角的“语言筛选器”按钮 。

要求
zhengguoc 2003-12-14
  • 打赏
  • 举报
回复
派生类调用gettype()或typeof()得到的是派生类的类型定义!类型是
type类型,含有派生类的方法,字段等类型信息。
zhengguoc 2003-12-14
  • 打赏
  • 举报
回复
不可以,gettype()是不能重写的。可以用typeof()或派生类的getType()
获得类型信息,包括派生类自己。
cbspy 2003-12-14
  • 打赏
  • 举报
回复
ms肯定在GetType()方法中结合了this和反射来确定当前实例的类型。
cbspy 2003-12-14
  • 打赏
  • 举报
回复
如果我用new关键字在派生类里覆盖GetType()呢?我手头没有调试器,在网吧,没办法验证。
cbspy 2003-12-14
  • 打赏
  • 举报
回复
谢谢rock1981() 和yunhi()
yunhi 2003-12-14
  • 打赏
  • 举报
回复
或者GetType()有可能是一个extern 方法
yunhi 2003-12-14
  • 打赏
  • 举报
回复
public Type GetType();

Type 实例,表示当前实例的确切运行时类型。

下列代码示例说明 GetType 返回当前实例的运行时类型。
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

*/
由于ms不开放源代码,但是你可以想象一下,它的代码中可以使用this关键字呐。
rock1981 2003-12-14
  • 打赏
  • 举报
回复
你可以用反射!

62,046

社区成员

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

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

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

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