Saturday 19 May 2018

c# - Is this how to "Implement IDisposable correctly"?

I ran Code Analysis on one of my projects, and it gave me two finger wags, namely:


CA1063 Implement IDisposable correctly Provide an overridable implementation of Dispose(bool) on 'UserStore' or mark the type as sealed. A call to Dispose(false) should only clean up native resources. A call to Dispose(true) should clean up both managed and native resources.


...on this line of code:


public class UserStore : IUserStore, IUserPasswordStore,  IUserClaimStore

...and this:


CA1063 Implement IDisposable correctly Modify 'UserStore.Dispose()' so that it calls Dispose(true), then calls GC.SuppressFinalize on the current object instance ('this' or 'Me' in Visual Basic), and then returns.


...on my empty Dispose:


public void Dispose()
{
//
}

Can I assassinate two avians with one petrified dirtclod by doing this:


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

? Is that what it's suggesting?


UPDATE


To answer my question (sort of), apparently not - I figured I'd just add the code and see if the Code Analysis would then feed back the yearned-for "'NRBQ.API.UserStore.Dispose()': no suitable method found to override"

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...