C# 类 根据属性名称取属性值

 
public class Cftea {    
  public string SiteName { get; set; }    
  public string Domain { get; set; }            
  
  public string GetValue(string name)    
  {        
    return Convert.ToString(this.GetType().GetProperty(name).GetValue(this, null));    
  }
}

调用时:

Cftea cftea = new Cftea(); string siteName = cftea.GetValue("SiteName");

有人问,为什么要这么做呢?主要适用于配置型的开发中,比如将要获取的属性的名称字符串写入 XML 文件,再根据 XML 中配置的属性名称字符串来获取值。

原文地址:https://www.cnblogs.com/wuyifu/p/2947174.html