【发布时间】:2011-10-08 19:32:01
【问题描述】:
我有一个包含一些成员和属性的类,当我创建它时,我如何知道创建时实例化的名称是什么?
我有一个 PDInt 类,所以我将它实例化为一个成员,然后将该成员包装在一个属性中。这是类,后面是属性:
public class PDInt : PDBase
{
#region Members
int m_Value;
public int Value
{
get
{
return m_Value;
}
set
{
if ( m_Value != value )
{
}
}
}
#endregion
public PDInt()
{
this.Init();
}
public PDInt( int Value )
{
this.Init();
this.Value = Value;
}
public PDInt( int Value, string ControlName )
{
this.Init();
this.Value = Value;
this.ControlName = ControlName;
}
private void Init()
{
this.Value = 0;
}
public void Map( int Value, string ControlName )
{
this.Value = Value;
this.ControlName = ControlName;
this.Validate = true;
}
public void Map( int Value, string ControlName, bool Validate )
{
this.Value = Value;
this.ControlName = ControlName;
this.Validate = Validate;
}
}
这里是成员,然后是属性用法
PDInt m_PrescriptionID;
public PDInt PrescriptionID
{
get
{
if ( m_PrescriptionID == null )
{
m_PrescriptionID = new PDInt();
}
return m_PrescriptionID;
}
set
{
if ( m_PrescriptionID == null )
{
m_PrescriptionID = new PDInt();
}
}
}
能够以编程方式确定实际实例化的名称对我非常有帮助,这样我就可以将它放在类内部的字符串中以供稍后引用。
我在整个应用程序中都使用反射,但在实例化类时我似乎无法弄清楚如何获取名称。
谢谢。
【问题讨论】:
-
实例化名 == 变量名???
标签: c# reflection dynamic properties