Use Reflection to get Property and Value from object

Hello, I'm trying to get the property and value for each property in an object using the following code. I'm having issues getting the value and when I attempt to add the key value pair to a collection I get an error that the add method is unknown. How can I get the Property type name and value and put into a list?

 public static List<KeyValuePair<string, string>> GetReportProperties(string templateName)
        {
           ReportTemplate template = new ReportTemplate();
           template = LoadReportTemplateList().Where(t => t.FileName == templateName).Single();
           Type type = template.GetType();
           var props = new List<KeyValuePair<string, string>>();
          foreach (PropertyInfo info in type.GetProperties())
          {
            
              string n = info.Name;
              var srcValue = info.GetValue(info.Name, null);
              string v = srcValue.ToString();

             props.Add(n,v);

          }
                   
            return props;
        }

Open in new window

Scott BaldridgeAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Najam UddinCommented:
try after chainging
props.Add(n,v);

Open in new window

to
props.Add(new KeyValuePair<string, string>(n,v));

Open in new window

Scott BaldridgeAuthor Commented:
Thank you for helping! The add method should now work, but I'm getting this error:

Object does not match target type.

On this line:
 var srcValue = info.GetValue(info.Name, null);
Najam UddinCommented:
Get value need object and index as parameter, check this
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Scott BaldridgeAuthor Commented:
Okay, so I need to find the index of the property in the object? Do you know how to do that?

I tried to do it like this but I still get the same error:

 int i = 0;

          foreach (PropertyInfo info in type.GetProperties())
          {
              object[] indexArgs = { i };
              string n = info.Name;
              object srcValue = info.GetValue(info.Name, indexArgs);
              string v = srcValue.ToString();

              i++;


              props.Add(new KeyValuePair<string, string>(n, v));

          }

Open in new window

Najam UddinCommented:
foreach(var prop in type .GetProperties()) {
    Console.WriteLine("{0}={1}", prop.Name, prop.GetValue(template, null));
}

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Scott BaldridgeAuthor Commented:
LOL I found the exact code on Stackoverflow. Excellent!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.