I’m using JB Evain’s Mono.Cecil to perform some byte-code manipulation on compiled DLL’s. Part of this project is to inject properties into TypeDefinitions, effectively turning compiled getters/setters into the C# Properties pattern using PropertyDefinitions. This part is working well.
However, I would like to add an indexer to the TypeDefinition but I cannot find any documentation or advice on how best to achieve this. The methods which will be used to process the index requests are already in place:
Object getObjectViaIndexer(String str);
void setObjectViaIndexer(String str, object obj);
All i need to do is add the metadata to convert these into indexers….
Any ideas / thoughts would be greatly apprecaited!
Indexers, metadata wise, are simple properties. Only the type is decorated with a special attribute to tell that the property is an indexer. Here’s a sample implementation which adds a new Item property that is recognized as an indexer, and delegates to the existing methods.
Of course you could also just create a property which points to the existing methods, as long as you have the proper attribute.