【求助】通过反射 给属性赋值
我定义了一下一个类:
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
namespace HisignScenePad.CSIPAD
{
/// <summary>
/// 案件类别关联分析意见实体类
/// </summary>
public class LawCaseEntity
{
static LawCaseEntity()
{
}
public LawCaseEntity()
{
this.LawCaseIdentify = string.Empty;
this.LawCaseType = string.Empty;
this.IntentionDesc = string.Empty;
this.IntentionDesc1 = string.Empty;
this.locationDesc = string.Empty;
this.locationDesc1 = string.Empty;
this.CommissionMeansDesc = string.Empty;
this.CommissionMeansDesc1 = string.Empty;
this.CommissionPeriodDesc = string.Empty;
this.CommissionPeriodDesc1 = string.Empty;
this.EntranceExitDesc = string.Empty;
this.EntranceExitDesc1 = string.Empty;
this.CommissionPointsDesc = string.Empty;
this.CommissionPointsDesc1 = string.Empty;
this.IntrudingWayDesc = string.Empty;
this.IntrudingWayDesc1 = string.Empty;
this.CasePropertyDesc = string.Empty;
this.CasePropertyDesc1 = string.Empty;
this.MotivationDesc = string.Empty;
this.MotivationDesc1 = string.Empty;
}
/// <summary>
/// 案件标识
/// </summary>
[Description(BZ)]
public string LawCaseIdentify
{
get;
set;
}
/// <summary>
/// 案件类别
/// </summary>
[Description(LawCaseEntity.AJLB)]
public string LawCaseType
{
get;
set;
}
/// <summary>
/// 选择对象
/// </summary>
[Description(LawCaseEntity.XZDX)]
public string IntentionDesc
{
get;
set;
}
/// <summary>
/// 自定义选择对象
/// </summary>
[Description(LawCaseEntity.ZDY_XZDX)]
public string IntentionDesc1
{
get;
set;
}
/// <summary>
/// 选择处所
/// </summary>
[Description(LawCaseEntity.XZCS)]
public string locationDesc
{
get;
set;
}
/// <summary>
/// 自定义选择处所
/// </summary>
[Description(LawCaseEntity.ZDY_XZCS)]
public string locationDesc1
{
get;
set;
}
/// <summary>
/// 作案手段
/// </summary>
[Description(LawCaseEntity.ZASD)]
public string CommissionMeansDesc
{
get;
set;
}
/// <summary>
/// 自定义作案手段
/// </summary>
[Description(LawCaseEntity.ZDY_ZASD)]
public string CommissionMeansDesc1
{
get;
set;
}
/// <summary>
/// 作案时机
/// </summary>
[Description(LawCaseEntity.ZASJ)]
public string CommissionPeriodDesc
{
get;
set;
}
/// <summary>
/// 自定义作案时机
/// </summary>
[Description(LawCaseEntity.ZDY_ZASJ)]
public string CommissionPeriodDesc1
{
get;
set;
}
/// <summary>
/// 作案进出口
/// </summary>
[Description(LawCaseEntity.ZAJCK)]
public string EntranceExitDesc
{
get;
set;
}
/// <summary>
/// 自定义作案进出口
/// </summary>
[Description(LawCaseEntity.ZDY_ZAJCK)]
public string EntranceExitDesc1
{
get;
set;
}
/// <summary>
/// 作案特点
/// </summary>
[Description(LawCaseEntity.ZATD)]
public string CommissionPointsDesc
{
get;
set;
}
/// <summary>
/// 自定义作案特点
/// </summary>
[Description(LawCaseEntity.ZDY_ZATD)]
public string CommissionPointsDesc1
{
get;
set;
}
/// <summary>
/// 侵入方式
/// </summary>
[Description(LawCaseEntity.QRFS)]
public string IntrudingWayDesc
{
get;
set;
}
/// <summary>
/// 自定义侵入方式
/// </summary>
[Description(LawCaseEntity.ZDY_QRFS)]
public string IntrudingWayDesc1
{
get;
set;
}
/// <summary>
/// 案件性质
/// </summary>
[Description(LawCaseEntity.AJXZ)]
public string CasePropertyDesc
{
get;
set;
}
/// <summary>
/// 自定义案件性质
/// </summary>
[Description(LawCaseEntity.ZDY_AJXZ)]
public string CasePropertyDesc1
{
get;
set;
}
/// <summary>
/// 作案动机
/// </summary>
[Description(LawCaseEntity.ZADJMD)]
public string MotivationDesc
{
get;
set;
}
/// <summary>
/// 自定义作案动机
/// </summary>
[Description(LawCaseEntity.ZDY_ZADJMD)]
public string MotivationDesc1
{
get;
set;
}
public void InitProperty(string value)
{
//Do Something
}
/// <summary>
///
/// </summary>
/// <param name="mode"></param>
/// <param name="equalsAll"></param>
/// <returns></returns>
public bool Equals(LawCaseEntity mode, bool equalsAll = true)
{
if (equalsAll)
{
if (this.LawCaseIdentify == mode.LawCaseIdentify
&& this.LawCaseType == mode.LawCaseType
&& this.IntentionDesc == mode.IntentionDesc
&& this.locationDesc == mode.locationDesc
&& this.CommissionMeansDesc == mode.CommissionMeansDesc
&& this.CommissionPeriodDesc == mode.CommissionPeriodDesc
&& this.EntranceExitDesc == mode.EntranceExitDesc
&& this.CommissionPointsDesc == mode.CommissionPointsDesc
&& this.IntrudingWayDesc == mode.IntrudingWayDesc
&& this.CasePropertyDesc == mode.CasePropertyDesc
&& this.MotivationDesc == mode.MotivationDesc
&& this.IntentionDesc1 == mode.IntentionDesc1
&& this.locationDesc1 == mode.locationDesc1
&& this.CommissionMeansDesc1 == mode.CommissionMeansDesc1
&& this.CommissionPeriodDesc1 == mode.CommissionPeriodDesc1
&& this.EntranceExitDesc1 == mode.EntranceExitDesc1
&& this.CommissionPointsDesc1 == mode.CommissionPointsDesc1
&& this.IntrudingWayDesc1 == mode.IntrudingWayDesc1
&& this.CasePropertyDesc1 == mode.CasePropertyDesc1
&& this.MotivationDesc1 == mode.MotivationDesc1)
{
return true;
}
else
{
return false;
}
}
else
{
if (this.LawCaseIdentify == mode.LawCaseIdentify
&& this.LawCaseType == mode.LawCaseType)
{
return true;
}
else
{
return false;
}
}
}
/// <summary>
/// 空判断
/// </summary>
/// <param name="mode"></param>
/// <returns></returns>
public static bool IsEmpty(LawCaseEntity mode)
{
if (string.IsNullOrEmpty(mode.LawCaseIdentify)
&& string.IsNullOrEmpty(mode.LawCaseType)
&& string.IsNullOrEmpty(mode.IntentionDesc)
&& string.IsNullOrEmpty(mode.locationDesc)
&& string.IsNullOrEmpty(mode.CommissionMeansDesc)
&& string.IsNullOrEmpty(mode.CommissionPeriodDesc)
&& string.IsNullOrEmpty(mode.EntranceExitDesc)
&& string.IsNullOrEmpty(mode.CommissionPointsDesc)
&& string.IsNullOrEmpty(mode.IntrudingWayDesc)
&& string.IsNullOrEmpty(mode.CasePropertyDesc)
&& string.IsNullOrEmpty(mode.MotivationDesc)
&& string.IsNullOrEmpty(mode.IntentionDesc1)
&& string.IsNullOrEmpty(mode.locationDesc1)
&& string.IsNullOrEmpty(mode.CommissionMeansDesc1)
&& string.IsNullOrEmpty(mode.CommissionPeriodDesc1)
&& string.IsNullOrEmpty(mode.EntranceExitDesc1)
&& string.IsNullOrEmpty(mode.CommissionPointsDesc1)
&& string.IsNullOrEmpty(mode.IntrudingWayDesc1)
&& string.IsNullOrEmpty(mode.CasePropertyDesc1)
&& string.IsNullOrEmpty(mode.MotivationDesc1))
{
return true;
}
else
{
return false;
}
}
}
每个属性都有对应的特性描述,我想 在方法 public void InitProperty(string value)
中根据value传送来的字符串值获取与特性描述对应的属性,并给该属性赋值,该怎么做??
[解决办法]
参考:
public class Employee
{
private string m_Tel="111";
private string m_Fax = "2222";
public virtual string Tel
{
get
{
return this.m_Tel;
}
set
{
this.m_Tel = value;
this.OnPropertyChanged("Tel");
}
}
public virtual string Fax
{
get
{
return this.m_Fax;
}
set
{
this.m_Fax = value;
this.OnPropertyChanged("Fax");
}
}
}