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?
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

0
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);
0
Najam UddinCommented:
Get value need object and index as parameter, check this
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

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

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

Open in new window

0

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!
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.