【求助】通过反射 给属性赋值

猴头 2013-09-04 12:48:06
我定义了一下一个类:

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传送来的字符串值获取与特性描述对应的属性,并给该属性赋值,该怎么做??
...全文
137 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
猴头 2013-09-04
  • 打赏
  • 举报
回复

        /// <summary>
        /// [标识]
        /// </summary>
        public const string BZ = "[标识]";
        /// <summary>
        /// [案件类别]
        /// </summary>
        public const string AJLB = "[案件类别]";
        /// <summary>
        /// [选择对象]
        /// </summary>
        public const string XZDX = "[选择对象]";
        /// <summary>
        /// [选择处所]
        /// </summary>
        public const string XZCS = "[选择处所]";
        /// <summary>
        /// [作案手段]
        /// </summary>
        public const string ZASD = "[作案手段]";
        /// <summary>
        /// [作案时机]
        /// </summary>
        public const string ZASJ = "[作案时机]";
        /// <summary>
        /// [作案进出口]
        /// </summary>
        public const string ZAJCK = "[作案进出口]";
        /// <summary>
        /// [作案特点]
        /// </summary>
        public const string ZATD = "[作案特点]";
        /// <summary>
        /// [侵入方式]
        /// </summary>
        public const string QRFS = "[侵入方式]";
        /// <summary>
        /// [案件性质]
        /// </summary>
        public const string AJXZ = "[案件性质]";
        /// <summary>
        /// [作案动机目的]
        /// </summary>
        public const string ZADJMD = "[作案动机目的]";

        /// <summary>
        /// [自定义选择对象]
        /// </summary>
        public const string ZDY_XZDX = "[自定义选择对象]";
        /// <summary>
        /// [自定义选择处所]
        /// </summary>
        public const string ZDY_XZCS = "[自定义选择处所]";
        /// <summary>
        /// [自定义作案手段]
        /// </summary>
        public const string ZDY_ZASD = "[自定义作案手段]";
        /// <summary>
        /// [自定义作案时机]
        /// </summary>
        public const string ZDY_ZASJ = "[自定义作案时机]";
        /// <summary>
        /// [自定义作案进出口]
        /// </summary>
        public const string ZDY_ZAJCK = "[自定义作案进出口]";
        /// <summary>
        /// [自定义作案特点]
        /// </summary>
        public const string ZDY_ZATD = "[自定义作案特点]";
        /// <summary>
        /// [自定义侵入方式]
        /// </summary>
        public const string ZDY_QRFS = "[自定义侵入方式]";
        /// <summary>
        /// [自定义案件性质]
        /// </summary>
        public const string ZDY_AJXZ = "[自定义案件性质]";
        /// <summary>
        /// [自定义作案动机目的]
        /// </summary>
        public const string ZDY_ZADJMD = "[自定义作案动机目的]";
    }
猴头 2013-09-04
  • 打赏
  • 举报
回复
引用 2 楼 hdhai9451 的回复:
参考: 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"); } } }
我想 采用 反射的方式....
Andy__Huang 2013-09-04
  • 打赏
  • 举报
回复
参考: 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"); } } }

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧