c# - 创建类时如何确定类的实例化名称

标签 c# reflection dynamic properties

我有一个包含一些成员和属性的类,当我创建它时,我如何知道创建时实例化的名称是什么?

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

编辑:

为什么需要它?也许您遇到了可以通过其他方式解决的问题?

关于c# - 创建类时如何确定类的实例化名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6752532/

相关文章:

c# - 使用反射和枚举来进行 MVC 应用程序访问的逻辑控制是否安全?

forms - 如何在提交函数的reactjs中将表单值作为FormData传递

c# - C# : Does it sleep the thread the object was instantiated in, 中的 Thread.Sleep() 或我从中调用该方法的线程?

c# - 图像源不会显示图片

c# - 反射式地在 C# 中实现泛型类型推断

c# - 通过属性值查找对象实例的属性

javascript - .on() 方法中的动态选择器

sql - NTILE 的动态参数

c# - .NET 4.5 和 C# 中使用 HttpClient 的 HTTP HEAD 请求

c# - 如何忽略所有目标成员,映射的成员除外?