我有一个包含一些成员和属性的类,当我创建它时,如何才能知道创建时实例化的名称是什么?
我有一个类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();
}
}
}
能够以编程方式确定实际实例化名称是什么对我来说非常有帮助,所以我可以将它放在类中的字符串中以供稍后引用。
我在我的应用程序中使用反射,我似乎无法弄清楚如何在实例化类时获取名称。
感谢。
答案 0 :(得分:2)
如果我理解正确,你想在类中知道在实例化类时从其他地方使用的实例名是什么?
如果是这样的话 - 你不能。 c#不允许这样做。
编辑:
你为什么需要那个?也许你有一个问题可以通过其他方式解决?