Using statement requires the object type to directly implement IDisposable
.
Looking for the easiest to do so, came up with this snippet.
public class Foo : IDisposable
{
public string? exampleProperty { get; set; }
public virtual void Dispose()
{
foreach(var property in GetType().GetProperties())
{
property.SetValue(this,null);
}
}
}
It compiles and runs, I can even use it inside using block normally. But, is that a proper manner to do it?
For investigative purposes:
using (var x = new Foo() { exampleProperty = "Xpto"})
{
//do something
}
in this case, Foo will became an Api request response