[Reflection] Get and Set values of Properties


object sampleObj = new object();
foreach (PropertyInfo info in sampleObj.GetType().GetProperties())
{
// To Get value
if (info.CanRead)
{
object o = info.GetValue(sampleObj, null);
}


// To Set value
object myValue = "Something";
if (info.CanWrite)
{
info.SetValue(sampleObj, myValue, null);
}
}

沒有留言:

張貼留言