With restrictions you describe
without using your own type and without changing property to method
this is not possible (using C# language features).
The C# statement
byte i = MyProperty[3];
is compiled into following IL:
IL_001f: ldloc.0
IL_0020: callvirt instance uint8[] ConsoleApp1.Cls::get_MyProperty()
IL_0025: ldc.i4.3
IL_0026: ldelem.u1
You see that the call to the property getter get_MyProperty
(at offset IL_0020
) occurs before the item index is even known. Only at offset IL_0025
, the code knows that an array element at index 3 needs to be loaded from the array. At that time point, the getter method already returned, so you have no chance to get that index value anywhere inside of the method.
Your only option is a low-level IL code patching. You will need to "hack" the compiled IL code using third party tools or even manually, but both are strongly discouraged.
You will need to replace the calls to the getter method with direct calls to your MyMethod
:
IL_001f: ldloc.0 // unchanged
// method call at IL_0020 removed
ldc.i4.3 // instead, we first copy the index value from what was previously at IL_0025...
callvirt instance uint8[] ConsoleApp1.Cls::MyMethod(int32) // ...and then call our own method
ldc.i4.3 // the rest is unchanged
ldelem.u1