IDisposable 接口的实现:在使用 Blazor wasm 时,是否适用默认建议?

huangapple go评论70阅读模式
英文:

IDisposable implementation: do the default recommendations apply when using Blazor wasm?

问题

I'm wondering if the default rules specified on the Implement a Dispose method still apply when writing classes/components for Blazor (specifically on the SuppressFinalize method call, which is nowhere to be seen on Blazor WebAssembly samples I've seen).

Thanks.

英文:

I'm wondering if the default rules specified on the Implement a Dispose method still apply when writing classes/components for Blazor (specifically on the SuppressFinalizemethod call, which is nowhere to be seen on blazor wasm samples I've seen).

Thanks.

答案1

得分: 1

以下是翻译好的部分:

if the default rules specified on the Implement a Dispose method still apply ...
如果实现Dispose方法的默认规则仍然适用...

No.
不。

But then again, they don't apply to application code on other platforms either.
但再次强调,它们也不适用于其他平台上的应用程序代码。

From that same page:
从同一页中:

Therefore, we recommend that you construct System.Runtime.InteropServices.SafeHandle objects instead of implementing a finalizer.
因此,我们建议您构建System.Runtime.InteropServices.SafeHandle对象,而不是实现finalizer。

So you shouldn't write Finalizers (in C#: ~MyClass() {}) and without Finalizers you don't need SuppressFinalize. In Blazor Wasm it's all n/a anyway, you don't have unmanaged resources there.
因此,您不应编写Finalizers(在C#中:~MyClass() {}),而且没有Finalizers,您不需要SuppressFinalize。在Blazor Wasm中,这一切都无关紧要,因为您在那里没有非托管资源。

The complexity that remains is the virtual Dispose(bool) pattern. That has everything to do with derived classes that need IDisposable too.
剩下的复杂性在于virtual Dispose(bool)模式。这与需要IDisposable的派生类有关。
我们通常忽略这一点,当您可以将您的类标记为sealed时,一切都会很好。否则,请按照这一模式操作。

英文:

> if the default rules specified on the Implement a Dispose method still apply ...

No.

But then again, they don't apply to application code on other platforms either.
From that same page:

> Therefore, we recommend that you construct System.Runtime.InteropServices.SafeHandle objects instead of implementing a finalizer.

So you shouldn't write Finalizers (in C#: ~MyClass() {}) and without Finalizers you don't need SuppressFinalize. In Blazor Wasm it's all n/a anyway, you don't have unmanaged resources there.

The complexity that remains is the virtual Dispose(bool) pattern. That has everyhing to do with derived classes that need IDisposable too.
We usually ignore this, and when you can make your class sealed you're good. Otherwise follow the pattern.

huangapple
  • 本文由 发表于 2023年5月30日 05:16:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76360359.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定