I'm trying to declare several indexed properties as part of an interface in C#. The aim being able to write something like: int v=obj.Field1[4]+obj.Field2[4];
The implementation is done in C++/CLI.
I've found info about the use of a 'proxy' to approach the behaviour in Named indexed property in C#?
C#:
public interface IMyProp1
{
int this[int idx] { get; }
}
public interface IMyProp2
{
int this[int idx] { get; }
}
public interface IMyThing: IMyProp1, IMyProp2
{
IMyProp1 Field1;
IMyProp2 Field2;
}
In C++/CLI, I've found some info here: https://msdn.microsoft.com/en-us/library/2f1ec0b1.aspx But it's not specific about interface I've written the following (trial and error with the help of the VS2015 compiler)
public ref class MyThing: IMyThing
{
virtual property int default[int]
{
int get(int idx) { return fld1[idx]; }
}
virtual property IMyProp1 Field1 { IMyProp1 get() { return this; } }
virtual property IMyProp2 Field2 { IMyProp2 get() { return this; } }
private:
array<int>^ fld1;
array<int>^ fld2;
}
But I don't see how I can implement 2 different behaviours, as
virtual property int default[int]
is unique. Even if there are 2 'different' interfaces (I concede it's the same signature), I can't figure out a way to specify 2 different implementations:
virtual property int IMyProp1::default[int] { int get(int idx) { return fld1[idx]; }
virtual property int IMyProp2::default[int] { int get(int idx) { return fld2[idx]; }
I've found this information about explicit interface implementation in C++
interface class MyInterface1 { void f(); };
interface class MyInterface2 { void f(); };
ref class MyClass : MyInterface1, MyInterface2
{
public:
virtual void f1() = MyInterface1::f
{
Console::WriteLine("MyClass::f1 == MyInterface1::f");
}
virtual void f2() = MyInterface2::f
{
Console::WriteLine("MyClass::f2 == MyInterface2::f");
}
};
But can't figure out a way to mix that with the indexed property signature.