dimanche 21 juin 2015

How to add to collection using reflection - C#

I am new to C# and reflection in specific and I am trying to solve a very specific problem. I want to implement the following code using reflection (since in some machines the System.Windows.Forms.DataVisualization.dlls might not be present, in those case i will skip generating charts).

Chart chart1 = new Chart();
string chartTitle = "Chart Title";
chart1.Titles.Add(chartTitle);

I figured out how to load a dll, get its class type, get/set its static/non-static properties, use constructors to create objects etc via Reflections. But I am quite lost about how to invoke "add" method on a collection. Say, i have object Chart1 and chartTitle via Reflection, how do i implement the 3rd line of code using Reflection.

Appreciate your help. Thanks in advance.

Green Apple





Aucun commentaire:

Enregistrer un commentaire