How to check if a property exists on an object and get the value of that property using reflection?

hi,

I need to check if a property exists on an object and get the value of that property if the property exists.
I have the attached code.
Thanks for any suggestions.



PropertyInfo info;

		            foreach (Field FO in section.FOCollection)
                    {
                        info = Myobj.GetType().GetProperty(FO.Node);
                        
                    }


if(info!=null)
{
 //get the value
}

Open in new window

ipjyoAsked:
Who is Participating?
 
Wayne Taylor (webtubbs)Connect With a Mentor AstronautCommented:
Like this....

    System.Reflection.PropertyInfo info = Myobj.GetType().GetProperty(FO.Node);
    if (info != null) {
        string value = info.GetValue(Myobj, null);
    }

Wayne
0
 
ipjyoAuthor Commented:
Thank you.
0
All Courses

From novice to tech pro — start learning today.