public class Program
{
public string str = "spp";
public string spp = "Hello World!";
public static void Main(string[] args)
{
Program p = new Program();
Console.WriteLine(p.GetType().GetField(p.str).GetValue(p).ToString());
Console.ReadKey();
}
}
利用反射獲取類中的成員變量名稱 並對該成員變量賦值:(注:該成員變量在類中必須支持get 和set方法)
ResultInfo mbResult = new ResultInfo();
for (int i=0;i< rowscount;i++)
{
string key = “key1”
string value = "value1";
foreach (var p in mbResult.GetType().GetProperties())
{
if (p.Name.ToLower().Equals(key.ToLower()))
{
p.SetValue(mbResult, value);
}
}
}