I've researched this question quite a bit, and while I've found a lot about C# and parameterized properties (using an indexer is the only way), I haven't found an actual answer to my question.
First, what I'm trying to do:
I have an existing COM DLL written in VB6 and I'm trying to create a C# DLL that uses a similar interface. I say similar because the VB6 DLL is only used with late binding, so it doesn't have to have the same GUIDs for the calls (that is, it doesn't have to be "binary compatible"). This VB6 COM DLL uses parameterized properties in a few places, which I know aren't supported by C#.
When using a VB6 COM DLL with parameterized properties, the reference in C# will access them as methods in the form "get_PropName" and "set_PropName". However, I'm going in the opposite direction: I'm not trying to access the VB6 DLL in C#, I'm trying to make a C# COM DLL compatible with a VB6 DLL.
So, the question is: How do I make getter and setter methods in a C# COM DLL that appear as a single parameterized property when used by VB6?
For example, say the VB6 property is defined as follows:
Public Property Get MyProperty(Param1 As String, Param2 as String) As String
End Property
Public Property Let MyProperty(Param1 As String, Param2 As String, NewValue As String)
End Property
The equivalent in C# would be something like this:
public string get_MyProperty(string Param1, string Param2)
{
}
public void set_MyProperty(string Param1, string Param2, ref string NewValue)
{
}
So, how would I make those C# methods look like (and function like) a single parameterized property when used by VB6?
I tried creating two methods, one called "set_PropName" and the other "get_PropName", hoping it would figure out that they're supposed to be a single parameterized property when used by VB6, but that didn't work; they appeared as two different method calls from VB6.
I thought maybe some attributes needed to be applied to them in C# so that they'd be seen as a single parameterized property in COM and VB6, but I couldn't find any that seemed appropriate.
I also tried overloading the methods, removing "get_" and "set_", hoping it would see them as a single property, but that didn't work either. That one generated this error in VB6: "Property let procedure not defined and property get procedure did not return an object".
I'm almost positive that there should be a way of doing this, but I just can't seem to find it. Does anyone know how to do this?
Update:
I took Ben's advice and added an accessor class to see if this could solve my problem. However, now I'm running into another issue...
First, here's the COM interface I'm using:
[ComVisible(true),
Guid("94EC4909-5C60-4DF8-99AD-FEBC9208CE76"),
InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface ISystem
{
object get_RefInfo(string PropertyName, int index = 0, int subindex = 0);
void set_RefInfo(string PropertyName, int index = 0, int subindex = 0, object theValue);
RefInfoAccessor RefInfo { get; }
}
Here's the accessor class:
public class RefInfoAccessor
{
readonly ISystem mySys;
public RefInfoAccessor(ISystem sys)
{
this.mySys = sys;
}
public object this[string PropertyName, int index = 0, int subindex = 0]
{
get
{
return mySys.get_RefInfo(PropertyName, index, subindex);
}
set
{
mySys.set_RefInfo(PropertyName, index, subindex, value);
}
}
}
Here's the implementation:
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
[Guid(MySystem.ClassId)]
[ProgId("MyApp.System")]
public class MySystem : ISystem
{
internal const string ClassId = "60A84737-8E96-4DF3-A052-7CEB855EBEC8";
public MySystem()
{
_RefInfo = new RefInfoAccessor(this);
}
public object get_RefInfo(string PropertyName, int index = 0, int subindex = 0)
{
// External code does the actual work
return "Test";
}
public void set_RefInfo(string PropertyName, int index = 0, int subindex = 0, object theValue)
{
// External code does the actual work
}
private RefInfoAccessor _RefInfo;
public RefInfoAccessor RefInfo
{
get
{
return _RefInfo;
}
}
}
Here's what I'm doing to test this in VB6, but I get an error:
Set sys = CreateObject("MyApp.System")
' The following statement gets this error:
' "Wrong number of arguments or invalid property assignment"
s = sys.RefInfo("MyTestProperty", 0, 0)
However, this works:
Set sys = CreateObject("MyApp.System")
Set obj = sys.RefInfo
s = obj("MyTestProperty", 0, 0)
It appears that it's trying to use the parameters on the property itself and getting an error because the property has no parameters. If I reference the RefInfo property in its own object variable, then it applies the indexer properties correctly.
Any ideas on how to arrange this so that it knows to apply the parameters to the accessor's indexer, rather than attempting to apply it to the property?
Also, how do I do a +1? This is my first question on StackOverflow :-)
Update #2:
Just to see how it would work, I also tried the default value approach. Here's how the accessor looks now:
public class RefInfoAccessor
{
readonly ISystem mySys;
private int _index;
private int _subindex;
private string _propertyName;
public RefInfoAccessor(ISystem sys, string propertyName, int index, int subindex)
{
this.mySys = sys;
this._index = index;
this._subindex = subindex;
this._propertyName = propertyName;
}
[DispId(0)]
public object Value
{
get
{
return mySys.get_RefInfo(_propertyName, _index, _subindex);
}
set
{
mySys.set_RefInfo(_propertyName, _index, _subindex, value);
}
}
}
This works great for a "get". However, when I try setting the value, .NET flips out with the following error:
Managed Debugging Assistant 'FatalExecutionEngineError' has detected a problem in 'blahblah.exe'.
Additional information: The runtime has encountered a fatal error. The address of the error was at 0x734a60f4, on thread 0x1694. The error code is 0xc0000005. This error may be a bug in the CLR or in the unsafe or non-verifiable portions of user code. Common sources of this bug include user marshaling errors for COM-interop or PInvoke, which may corrupt the stack.
I'm assuming the problem is that .NET tried setting the value to the method, rather to the default property of the returned object, or something similar. If I add ".Value" to the set line, it works fine.
Update #3: Success!
I finally got this working. There's a few things to look for, however.
First, the default value of the accessor must return a scaler, not an object, like so:
public class RefInfoAccessor
{
readonly ISystem mySys;
private int _index;
private int _subindex;
private string _propertyName;
public RefInfoAccessor(ISystem sys, string propertyName, int index, int subindex)
{
this.mySys = sys;
this._index = index;
this._subindex = subindex;
this._propertyName = propertyName;
}
[DispId(0)]
public string Value // <== Can't be "object"
{
get
{
return mySys.get_RefInfo(_propertyName, _index, _subindex).ToString();
}
set
{
mySys.set_RefInfo(_propertyName, _index, _subindex, value);
}
}
}
Second, when using the accessor, you need to make the return type an object:
public object RefInfo(string PropertyName, int index = 0, int subindex = 0)
{
return new RefInfoAccessor(this,PropertyName,index,subindex);
}
This will make C# happy, since the default value is a COM thing (dispid 0) and not a C# thing, so C# expects a RefInfoAccessor to be returned, not a string. Since RefInfoAccessor can be coerced into an object, no compiler error.
When used in VB6, the following will now all work:
s = sys.RefInfo("MyProperty", 0, 0)
Debug.Print s
sys.RefInfo("MyProperty", 0, 0) = "Test" ' This now works!
s = sys.RefInfo("MyProperty", 0)
Debug.Print s
Many thanks to Ben for his help on this!