c# - Passing an IDisposable object by reference causes an error? -


I am trying to create a common method for disposal of an object, named DisposeObject ( )

To ensure that I am disposing of objects indicated by a parent reference, I am trying to pass an object from context.

But I got a compilation error

The 'riff' argument type does not match the parameter type

Below (simplified) code In, apply both _Baz and _Bar .

alt text <br /> </ p> <ol> <Li> Why am I getting this error? </ Li> <li> Is there any way to do this? </ Li> </ ol> <p> <strong> [UPDATE] </ strong> has been given so far By the answers, unless I set an IDisposable argument to null, I can pass an object without object, using the <code> ref </ code> now I am having another problem that the disposable object Set to <code> null </ code> or not <code> DisposeObjec T </ code> in the law <br /> </ p> <p> This is the complete source of perfection: </ p> <pre> <code> public square fu: idispozazal {private bar _Bar; private hawk _Baz; private Bool _IsDisposed; ~ Fu (wrong);} Public Disposal (Bull Settlement) {If (! _IsDisposed) {If Settlement} {DisposeObject (ref _Baz); Disposes Object (ref__Bar);}} _IsDisposed = True;} Private Wide Disposal Object (Refused IDSOSBABAB Obj) {try {if (obj == null) return; obj.Dispose (); Obj = null; } Grip (object disoxaxexception) {/ * already disposed ... ...} public nullification () {remove (true); GC.SuppressFinalize (this); }} Public square bar: IDisposable {public zero extraction} () {}} Public square falcon: IDisposable {public zero extract () {}} </ code> </ pre> <p> <strong> [RESULT] </ strong > <br /> I removed the code which is <code> obj = null; </ Code> sets the argument for logic within <code> DisposeObject </ code> then the last code is created. </ P> <pre> <code> Generating a public zero (disposing of hair) {if (! _IsDisposed) {if (disposal) {execution object (_Baz); DisposeObject (_Bar); }} _IsDisposed = true; } Private Wide Disposable Objects {try {if (obj == null) return; Obj.Dispose (); } Hold (Object DisexxException) {/ * Dispau already ... ...}} </ code> </ pre> </ div> <p> <div class =

You do not need to pass the context, because you are passing a reference type. You should remove the ref keyword from your method definition and do this and you should not have any problem, although I'm not sure whether this is simply more effective or obvious than simply Dispose ( (It is different from the fact that you do not need to insert it for a clear implementation and it checks for a null for you.)

< Strong> edit

dance, while I hope to discuss which Surrounded by topic, he proved to be useful for you, your original intention does not seem like something that Snbvniy. To pass something as referee , you can not pass a variable that is different than the ref parameter (in other words, you can not pass A variable declared as a class or other interface which implements IDisposable if ref parameter IDisposable ). Because the ref parameter allows to collar back to the caller, you will open the possibility of storing incompatible types in your variable.

Here is your best bet to assign blank if you want what you want if you want to check out null and on that function Exceptions that want to ignore are ok, but in this scenario ref will not work for you, yet unfortunately.


Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -