如何在创建类时确定类的实例化名称?

本文关键字:实例化 创建 | 更新日期: 2023-09-27 18:02:29

我有一个类与几个成员和属性,当我创建它,我怎么能知道实例化的名称是在创建的时候?

我有一个类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#不允许。

编辑:

你为什么需要那个?也许你有一个问题可以用其他方式解决?