Wondering when you are going to re-assign a COM object, should you first dispose of the COM object?
public class DisposeTest : IDisposable
{
public MyCOMObject MyObject { get; internal set; }
public void ReAssign()
{
//Re-assign to new COM object
MyObject = GetNewCOMObject();
}
public void Dispose()
{
if (MyObject != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(MyObject);
}
}
}
If you call ReAssign()
multiple times it will create new instances of the MyCOMObject
.
What I am not sure about is, should you first release the current MyObject
before assigning a new value?
e.g. something like
public void ReAssign()
{
//Release current object
if (MyObject != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(MyObject);
}
//Re-assign to new COM object
MyObject = GetNewCOMObject();
}