Through Reflection Emit, a Type / class with an indexer (signature: string this[int]) is created this way:
DefineProperty - "Item" (System.Reflection.PropertyAttributes.SpecialName)
DefineMethod - getItem - "get_Item" (MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.SpecialName)
DefineMethod - setItem - "set_Item" (MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.SpecialName)
propertyBuilder.SetGetMethod(getItem);
propertyBuilder.SetSetMethod(setItem);
The indexer can be invoked through Reflection, based on "Item" property:
object oIndexer = t.InvokeMember("Item",
BindingFlags.Public | BindingFlags.GetProperty | BindingFlags.DeclaredOnly | BindingFlags.Instance,
null, oInstance, new object[] { 0 });
From the code below, it finds indexer "Item" as well:
//type is the Type that was created through Reflection Emit
foreach (PropertyInfo property in type.GetProperties(
BindingFlags.Instance | BindingFlags.Public))
{
if (property.GetIndexParameters().Length > 0)
{
Console.WriteLine("Found indexer for {0} - {1}",
type, property.Name);
}
}
Weird thing is that in VS2005 intellisense, "get_Item" / "set_Item" methods are visible and can be invoked with correct result; while "Item" properyt is not visible.
Now the problem, when the Assembly generated through Reflection Emit is referenced and used, I cannot use this syntax - "this[int]' to access to indexer. Compiler Error: Cannot apply indexing with [] to an expression of type 'MyType'.
After much research and reading with no luck. I know I must be missing something simple. Any clues are greatly appreciated!
Thread Closed
This thread is kinda stale and has been closed but if you'd like to continue the conversation, please create a new thread in our Forums,
or Contact Us and let us know.