处理对象 - 如何在屏幕上正确打印基本类型
我遵循 MSDN 指南 实现 Dispose 方法< /a>.我编写了简单的代码,以便更好地理解并逐步运行代码。
编辑:更改标题以更好地适应问题
这是代码:
class Program {
static void Main(string[] args) {
Base0 base0 = new Base0();
base0.Dispose();
Console.WriteLine();
Sub1 sub1 = new Sub1();
sub1.Dispose();
Console.ReadLine();
}
}
class Base0 : IDisposable {
private bool disposed;
public Base0() {
Console.WriteLine("Creating Base0!");
this.disposed = false;
// allocating some resources
}
public void Dispose() {
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing) {
Console.WriteLine("Disposing " + this.GetType().ToString() + "!");
if (!this.disposed) {
if (disposing) {
// disposing all managed resources
}
// disposing all unmanaged resources
}
}
public void DoSomething() {
if (this.disposed) {
throw new ObjectDisposedException(this.GetType().ToString());
}
}
~Base0() {
Dispose(false);
}
}
class Sub1 : Base0 {
private bool disposed;
public Sub1() {
Console.WriteLine("Creating Sub1!");
this.disposed = false;
// allocating some resources
}
protected override void Dispose(bool disposing) {
Console.WriteLine("Disposing " + this.GetType().ToString() + "!");
if (!this.disposed) {
try {
if (disposing) {
// disposing all managed resources
}
// disposing all unmanaged resources
}
finally {
base.Dispose(disposing);
}
}
}
}
这是输出:
Creating Base0!
Disposing DisposeFinalizeMethods.Base0!
Creating Base0!
Creating Sub1!
Disposing DisposeFinalizeMethods.Sub1!
Disposing DisposeFinalizeMethods.Sub1!
我很困惑,因为我预计最后一行会说“Diusing ... Base0!”,基础类型。
代码按预期执行,我已经“一步一步”检查了很多次,我理解它,但有一些东西我错过了。我缺少什么?
I am following MSDN guideline of implementing a Dispose Method. I've written my simple code to better understand and run the code step by step.
EDITED: changed title to better fit the problem
This is the code:
class Program {
static void Main(string[] args) {
Base0 base0 = new Base0();
base0.Dispose();
Console.WriteLine();
Sub1 sub1 = new Sub1();
sub1.Dispose();
Console.ReadLine();
}
}
class Base0 : IDisposable {
private bool disposed;
public Base0() {
Console.WriteLine("Creating Base0!");
this.disposed = false;
// allocating some resources
}
public void Dispose() {
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing) {
Console.WriteLine("Disposing " + this.GetType().ToString() + "!");
if (!this.disposed) {
if (disposing) {
// disposing all managed resources
}
// disposing all unmanaged resources
}
}
public void DoSomething() {
if (this.disposed) {
throw new ObjectDisposedException(this.GetType().ToString());
}
}
~Base0() {
Dispose(false);
}
}
class Sub1 : Base0 {
private bool disposed;
public Sub1() {
Console.WriteLine("Creating Sub1!");
this.disposed = false;
// allocating some resources
}
protected override void Dispose(bool disposing) {
Console.WriteLine("Disposing " + this.GetType().ToString() + "!");
if (!this.disposed) {
try {
if (disposing) {
// disposing all managed resources
}
// disposing all unmanaged resources
}
finally {
base.Dispose(disposing);
}
}
}
}
This is the output:
Creating Base0!
Disposing DisposeFinalizeMethods.Base0!
Creating Base0!
Creating Sub1!
Disposing DisposeFinalizeMethods.Sub1!
Disposing DisposeFinalizeMethods.Sub1!
I am confused because I expected that the last line would be saying "Diposing ... Base0!", the base type.
The code executes as it should, I've checked it 'step by step' many times, I understand it but there is something that I've missed. What am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
好的,这不是关于 Dispose 或 IDisposable,而是关于 GetType。
this.GetType()
是对虚拟方法的调用。当在基类中调用时,它将给出实际(派生)类型的类型。重现:
将打印
BB
。OK, this is not about Dispose or IDisposable but about GetType.
this.GetType()
is a call to a virtual method. When called in a base-class it will give the Type of the actual (derived) type.To reproduce:
Will print
BB
.您必须在 Base0 的 Dispose 方法中使用
typeof(Base0)
。 GetType 始终返回实际实例化的类型。You would have to use
typeof(Base0)
in Base0's Dispose method. GetType always returns the type that was actually instantiated.由于调用者(调用的类型)对象的类型为
Sub1
,因此您将看到它两次。这是虚拟方法,因此调用基类
GetType
将返回调用者类型,这是正确的。
As the caller (type that calls) object is of type
Sub1
you will see it two times.This is virtual method so calling on base class
GetType
will return caller's typeIt's correct.
如果您想找出参数的编译时类型,使用下面所示的通用方法可能会有所帮助:
评估 test() 将产生(为了可读性而添加换行符)
The getKnownType method will return the compile-time type of the variable or expression that's passed to it, even if the object in question happens to have a more deeply derived type at run-time.
If you want to find out the compile-time type of a parameter, it may be helpful to use the generic method illustrated below:
Evaluating test() will yield (line-break added for readability)
The getKnownType method will return the compile-time type of the variable or expression that's passed to it, even if the object in question happens to have a more deeply derived type at run-time.