C# 通过反射设置属性
每次点击标题集访问器时,我都试图设置UpperTitle填充Title.ToUpperC# 通过反射设置属性,c#,nhibernate,C#,Nhibernate,每次点击标题集访问器时,我都试图设置UpperTitle填充Title.ToUpper public string Title { get { return Title; } set { Title = value; UpperTitle = Title.ToUpper(); } } public string UpperTitle { get; protected set; } 这段代码可以编译,但我不确定它是
public string Title
{
get { return Title; }
set
{
Title = value;
UpperTitle = Title.ToUpper();
}
}
public string UpperTitle { get; protected set; }
这段代码可以编译,但我不确定它是否正常,因为我遇到了映射异常
problem to set property by reflection
在
get
中,您再次调用get
(无限循环)!因此,请按如下方式更改代码:
private string _title;
public string Title
{
get { return _title; }
set
{
_title= value;
UpperTitle = string.IsNullOrEmpty(_title)? string.Empty : _title.ToUpper();
}
}
public string UpperTitle { get; protected set; }
在
get
中,您再次调用get
(无限循环)!因此,请按如下方式更改代码:
private string _title;
public string Title
{
get { return _title; }
set
{
_title= value;
UpperTitle = string.IsNullOrEmpty(_title)? string.Empty : _title.ToUpper();
}
}
public string UpperTitle { get; protected set; }
使用
virtual
keyword标记映射属性不确定为什么有一个属性用于仅获取另一个属性的大写:)@Damith原因我想在该属性上添加db索引,以便将来检查使用virtual
keyword标记映射属性不确定为什么有一个属性用于仅获取另一个属性的大写:)@Damith cause我想在该属性上添加db索引以备将来检查。建议在执行ToUpper之前在其中添加空检查。建议在执行ToUpper之前在其中添加空检查。