In C#, can an object method invoke the object's own d-tor? and making any reference to the object invalid?
I'm trying to build an "object control" in memory system. user can "checkout" an object, then "finish" working with it ("checkin"). and I need to be sure once the object is "checked in" - any reference to it in the user code, will become invalid.
Here is an illustrated situation
public class MyType
{
public object Object {get; private set;}
public MyType()
{
this.Object = new ... ; // initialize Object property
}
public void Finish()
{
// ... some work on this.Object
this.Object = null;
// this = null; <- kill myself ?
}
}
pubic class Consumer()
{
public void Method()
{
var myUsage = new MyType(); // underline Object is initialized
SomethingWith(myUsage.Object); // use underline Object
myUsage.Finish(); // complete the usage
SomethingWith(myUsage.Object); // <-- exception, myUsage is not valid, is null or something similar
}
public void SomethingWith(object obj)
{
obj.Method();
....
}
}