如何包装一个COM互操作对象时,我实现在C#中Dispose模式? [英] How do I implement the dispose pattern in c# when wrapping an Interop COM Object?

查看:193
本文介绍了如何包装一个COM互操作对象时,我实现在C#中Dispose模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的类包含一个互操作对象,并调用它的方法,它导致ALLOC东西。这也暴露了一个方法来释放的东西,所以我希望我应该Dispose()方法调用这样的:

  MyClass类:IDisposable接口
{
私人DllName.ComClassName comInstance;

无效的someMethod()
{
comInstance =新DllName.ComClassName();
comInstance.AllocStuff();
}

公共无效的Dispose()
{
comInstance.FreeThatStuff();
}
}

现在,我应该展开全部遵循的Dispose模式。我没有其他一次性或非托管资源释放,因此,假如comInstance进行管理(是不是互操作做了什么,包装成非托管的管理?),我认为该模式disolves为:

 公共无效的Dispose()
{
如果(comInstance!= NULL)
{
comInstance.FreeStuff();
comInstance = NULL;
}
}



哪个泄露,除非我明确地调用Dispose()上的实例MyClass的,这将使得Dispose模式有缺陷?这是否意味着comInstance必须是不受管理的,图案disolves为:

 公共无效的Dispose()
{
DisposeComInstance();
GC.SuppressFinalize(本);
}

〜MyClass的()
{
DisposeComInstance();
}

私人无效DisposeComInstance()
{
如果(comInstance!= NULL)
{
comInstance.FreeStuff();
comInstance = NULL;
}
}



编辑:




  1. 要避免弄乱我的课的充分模式,我可以只封我的课?

  2. 怎么办我知道ComClassName(和一般的类)的非托管?


解决方案

它看起来像你'已经几乎得到了它钉。我会回落到那里你有一个受保护的虚拟处置的需要,指示是否管理项目应处理的布尔参数的模式。这样,有人来了,你后面会继续正确实现IDisposable < 。/ A>

 公共无效的Dispose()
{
this.Dispose(真);
GC.SuppressFinalize(本);
}

〜MyClass的()
{
this.Dispose(假);
}

受保护的虚拟无效的Dispose(BOOL处置)
{
//如果(处置)
// {
// / /托管
//}

如果(comInstance!= NULL)
{
comInstance.FreeStuff();
comInstance = NULL;
}

// base.Dispose(处置)如果需要
}


My class contains an object from Interop and calls a method on it which causes it to alloc stuff. It also exposes a method to free that stuff, so I expect I should call this in Dispose():

class MyClass : IDisposable
{
    private DllName.ComClassName comInstance;

    void SomeMethod()
    {
        comInstance = new DllName.ComClassName();
        comInstance.AllocStuff();
    }

    public void Dispose()
    {
        comInstance.FreeThatStuff();
    }
}

Now, I should expand all that to follow the Dispose pattern. I have no other disposable or unmanaged resources to release, so assuming comInstance is managed (isn't that what Interop does, wraps unmanaged into managed?), I think the pattern disolves to:

public void Dispose()
{
    if (comInstance != null)
    {
        comInstance.FreeStuff();
        comInstance = null;
    }
}

Which leaks unless I explicitly call Dispose() on instances of MyClass, which would make the Dispose pattern flawed? So does that mean comInstance must be unmanaged, and the pattern disolves to:

public void Dispose()
{
    DisposeComInstance();
    GC.SuppressFinalize(this);
}

~MyClass()
{
    DisposeComInstance();
}

private void DisposeComInstance()
{
    if (comInstance != null)
    {
        comInstance.FreeStuff();
        comInstance = null;
    }
}

EDIT:

  1. To avoid cluttering my class with the full pattern, could I just seal my class?
  2. How do I know ComClassName (and in general any class) is unmanaged?

It looks like you've almost got it nailed. I would fall back to the pattern where you have a protected virtual Disposing that takes a boolean parameter indicating if managed items should be disposed of. That way somebody coming behind you will continue to implement IDisposable properly.

public void Dispose()
{
    this.Dispose(true);
    GC.SuppressFinalize(this);
}

~MyClass()
{
    this.Dispose(false);
}

protected virtual void Dispose(bool disposing)
{
    // if (disposing)
    // {
    //      // Managed
    // }

    if (comInstance != null)
    {
        comInstance.FreeStuff();
        comInstance = null;
    }

    // base.Dispose(disposing) if required
}

这篇关于如何包装一个COM互操作对象时,我实现在C#中Dispose模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆