顯示具有 Reflection 標籤的文章。 顯示所有文章
顯示具有 Reflection 標籤的文章。 顯示所有文章

[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);
}
}