You have two issues in your code.
- A
Delegate object is not a method, so you need to use a method on the Delegate object to invoke the method it refers - The first argument to
CreateDelegate should be the delegate type, not the class containing a method you want to invoke.
Full working example:
public delegate void ParamLess(); class SomeClass { public void PrintStuff() { Console.WriteLine("stuff"); } } internal class Program { private static Dictionary<int, int> dict = null; static void Main() { var obj = new SomeClass(); Delegate del = Delegate.CreateDelegate(typeof(ParamLess), obj, "PrintStuff", false); del.DynamicInvoke(); // invokes SomeClass.PrintStuff, which prints "stuff" } }
In your case, the Main method should look like this:
public static void Main() { int i = 1; PropertyRetrievalClass obj = new PropertyRetrievalClass(); Delegate del = Delegate.CreateDelegate( typeof(PropertyRetrievalClass.getProperty), obj, "get_Chart_" + i.ToString()); string output = (string)del.DynamicInvoke("asldkl"); }
Update
Note that CreateDelegate is case sensitive on the method name, unless you tell it not to.
// this call will fail, get_chart should be get_Chart Delegate del = Delegate.CreateDelegate( typeof(PropertyRetrievalClass.getProperty), obj, "get_chart_" + i.ToString()); // this call will succeed Delegate del = Delegate.CreateDelegate( typeof(PropertyRetrievalClass.getProperty), obj, "get_Chart_" + i.ToString()); // this call will succeed, since we tell CreateDelegate to ignore case Delegate del = Delegate.CreateDelegate( typeof(PropertyRetrievalClass.getProperty), obj, "get_chart_" + i.ToString(), true);