英文:
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 SuppressFinalize
method 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论