I need to write a Registration Free COM Interop library see MSDN link
One of the requirements is, and I quote
"For a .NET-based class to be compatible with registry-free activation from COM, the class must have a default constructor and must be public."
As I read it, I need to create the following... (this technically works, and I have no problem instantiating this via COM)
[ComVisible(true)]
[Guid("...")]
public interface ITest
{
X();
Y();
}
[ComVisible(true)]
[Guid("...")]
[ClassInterface(ClassInterfaceType.AutoDispatch)]
public class Test : ITest
{
private string x;
public Test() // default constructor
{
}
X()
{
// do something with "x" BUT "x" MUST be initialized before this method is called
}
Y()
{
// do something else with "x" BUT "x" MUST be initialized before this method is called
}
}
I'm looking for the best way to ensure that this class is initialized before any methods are called (via the interface), so, other than a constructor, whats my next best option to initialize "x"? As far as I can tell overloading the constructor with a parameter isn't an option - Usually I would initialize this class with a constructor that took parameters, however with Registration Free COM, I don't have that option (or do I??).
I think my alternative is an "Initialize" function, such as...
public interface ITest
{
Initialize(string x);
X();
Y();
}
public class Test : ITest
{
private string x;
private bool Initialized;
public Test() // default constructor
{
Initialized = false;
}
Initialize(string x)
{
this.x = x;
Initialized = true;
}
X()
{
if (Initialized)
{
// do something with x
}
else
{
throw...
}
}
Y()
{
if (Initialized)
{
// do something else with x
}
else
{
throw...
}
}
}
I feel that this is messy, but doable... but what better option am I missing?