Indexer reflection

In this chapter you will learn:

  1. How to do Indexer reflection

Reflection on Indexer

The following code uses Type and PropertyInfo to manipulate the indexer.

using System;// ja  v  a  2  s . c o m
using System.Reflection;

public class MyClass
{
    private int [,] myValue = new int[10,10]; 

    public int this [int i,int j]
    {
        get 
        {
            return myValue[i,j];
        }
        set 
        {
            myValue[i,j] = value;
        }
    }
}
public class MyTypeClass
{
    public static void Main()
    {
        try
        {
            Type myType=typeof(MyClass);
            Type[] myTypeArray = new Type[2];
            myTypeArray.SetValue(typeof(int),0);
            myTypeArray.SetValue(typeof(int),1);
            PropertyInfo myPropertyInfo = myType.GetProperty("Item",typeof(int),myTypeArray,null);
            Console.WriteLine(myType.FullName);
            Console.WriteLine(myPropertyInfo.Name);
            Console.WriteLine(myPropertyInfo.PropertyType);
        }catch(Exception ex){
            Console.WriteLine("An exception occurred " + ex.Message);
        }
    }
}

Next chapter...

What you will learn in the next chapter:

  1. How to List Properties by Reflection
  2. Get/set a property using a PropertyInfo
Home » C# Tutorial » Reflection
Reflection
Type
Type properties
Field reflection
Field Type
Field Attributes
FieldHandle
Field value
Set Field value
delegate reflection
Event reflection
Indexer reflection
Properties Reflection
Method
Parameter
Invoke
Type Instantiating
interface reflection
Generic type reflection
Reflection on nested Types
Subtype
Array reflection
Assembly