处理对象-如何正确地在屏幕上打印基本类型

本文关键字:打印 类型 屏幕 对象 正确地 处理 | 更新日期: 2023-09-27 18:02:01

我遵循MSDN实现Dispose方法的指导方针。我已经编写了简单的代码,以便更好地理解和一步一步地运行代码。

EDITED:更改标题以更适合问题

这是代码:

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!

我很困惑,因为我以为最后一行会说"处置……"Base0!",基本类型。

代码执行,因为它应该,我已经检查了它"一步一步"很多次,我理解它,但有一些东西我错过了。我错过了什么?

处理对象-如何正确地在屏幕上打印基本类型

好的,这不是关于Dispose或IDisposable,而是关于GetType。

this.GetType()是对虚方法的调用。在基类中调用时,它将给出实际(派生)类型的Type。

复制:

class A
{
    public virtual void Print()
    {
        Console.Write(this.GetType().Name);
    }
}
class B : A
{
    public override void Print()
    {
        base.Print();
        Console.Write(this.GetType().Name);
    }
}

        var b = new B();
        b.Print();

将打印BB

您必须在Base0的Dispose方法中使用typeof(Base0)。GetType总是返回实际实例化的类型。

由于调用者(调用类型)对象是Sub1类型,您将看到它两次。

这是一个虚方法,所以调用基类GetType将返回调用者的类型

这是正确的。

如果您想了解参数的编译时类型,使用下面所示的泛型方法可能会有所帮助:

<>之前静态类typeGetter{静态类型getKnownType(this T it)其中T: class{返回typeof (T);}静态字符串测试(){IEnumerable<先。myThing>();返回字符串。Format("运行时类型{0}但编译时类型{1}",myth. gettype ().ToString(), myth. getknowntype ().ToString());}}之前

计算test()将产生(为可读性添加换行符)

"运行时类型System.Collections.Generic.List 1[System.IO.MemoryStream] but
  compile-time type System.Collections.Generic.IEnumerable 1[System.IO.Stream]"
getKnownType方法将返回传递给它的变量或表达式的编译时类型,即使所讨论的对象恰好在运行时具有更深层次的派生类型。