c# - 引用类中的 Self's Type

标签 c# types self

在我的 C# 类中,我想引用类本身的类型,这可能吗?

我的示例代码:

在行

private static List<CRcpParmPropEle<CParam1, string>> ParmPropList;

我不想使用“CParam1”,我希望使用某种通用方式(例如“this”)来引用自身。因为我有很多像 CParam1 这样的类,所以每个人都需要以这种方式引用自身。

class CParam1
{

    private double m_Prop1;

    private static List<CRcpParmPropEle<CParam1, string>> ParmPropList;

    public CParam1()
    {

    }

    ////-> I wish to replace Cparam1 to something like this
    public static List<CRcpParmPropEle<CParam1, string>> getParmPropList()
    {
        if (ParmPropList == null)
        {                
            ParmPropList.Add(new CRcpParmPropEle<CParam1, string>("Prop1", "BA", 0, false));
            //-> I wish to replace Cparam1 to something like this
        }
        return ParmPropList;
    }

    public string Prop1
    {
        get
        {
                return m_Prop1.ToString();
        }
        set
        {
            m_Prop1 = -1;
            double dW1;
            if (double.TryParse(value, out dW1))
            {
                    m_Prop1 = dW1;
            }
        }
    }

public class CRcpParmPropEle<T,TProp>
{
    public Func<T, TProp> getter;
    public Action<T, TProp> setter;

    public string PropName { get; set; }
    public string ColPos { get; set; }
    public int ColNum { get; set; }
    public int RowNum { get; set; }

    public bool ReadOnly { get; set; }

    public CRcpParmPropEle(string strPropName, string strColPos, int nRowNum, bool bReadOnly)
    {
        PropName = strPropName;
        ColPos = strColPos;
        RowNum = nRowNum;

        ReadOnly = bReadOnly;

        var prop = typeof(T).GetProperty(PropName);    //typeof(rcpObj).GetProperty(propName);
        getter = (Func<T,TProp>)Delegate.CreateDelegate(typeof(Func<T,TProp>), prop.GetGetMethod());
        setter = (Action<T,TProp>)Delegate.CreateDelegate(typeof(Action<T,TProp>), prop.GetSetMethod());            
    }
}

最佳答案

一般来说,没有。没有办法做到这一点。因为在通常使用它的上下文中,无论如何,我们都会编写特定于该类型的代码,并且由于我们习惯于无论如何都必须为静态成员指定类型名称,所以这看起来并不是什么困难。

但是,有一些替代方案可以在您的场景中使用,它们都涉及泛型。

第一个是将新对象的构造委托(delegate)给通用帮助器方法:

public static List<CRcpParmPropEle<CParam1, string>> getParmPropList()
{
    if (ParmPropList == null)
    {
        AddToList(ParmPropList, "Prop1", "BA", 0, false);
    }
    return ParmPropList;
}

private static void AddToList<T>(List<CRcpParmPropEle<T, string>> list, string s1, string s2, int i, bool f)
{
    list.Add(new CRcpParmPropEle<T, string>(s1, s2, i, f));
}

这样,实际类型就是从List<T>的类型推断出来的。对象被传入,因此不需要重述。当然,您无法避免在某处指定类型。它只是不会在这个特定的调用站点结束。

另一个选择是使用静态帮助器类来实现 CParam1 的静态功能。类:

static class ParmPropClass<T>
{
    private static List<CRcpParmPropEle<T, string>> ParmPropList;

    public static List<CRcpParmPropEle<T, string>> getParmPropList()
    {
        if (ParmPropList == null)
        {
            ParmPropList.Add(new CRcpParmPropEle<T, string>("Prop1", "BA", 0, false));
        }
        return ParmPropList;
    }
}

那么当你使用静态成员时,你必须将类型名称指定为 ParmPropClass<CParam1> (或者其他什么,取决于您最终命名辅助类的名称)。同样,您不必在某处指定类型名称,但调用站点不需要这样做。

事实上,如果您在多个位置完全执行这种模式,则通用辅助类型可能是更好的方法,因为这样您就不必复制/始终粘贴代码(无法修复复制到的所有位置中的某些错误的秘诀)。

关于c# - 引用类中的 Self's Type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30158849/

相关文章:

php - 静态::对比self::- 有什么缺点吗?

c# - 如何扩展ms-explorer自动处理 "connected files"/sidecar files/xmp属于jpg?

c# - 创建对象时是否可以为 const 变量赋值?

haskell - haskell 和 Arrows 中的类型推断

java 泛型 : runtime type-checking to determine a strategy

asp.net - 无法从模式对话框下载,window.showModalDialog

javascript - Chrome 浏览器忽略 var self = this;

c# - 关于使用 XmlReader 读取子节点的正确方法的困惑

c# - 根据过滤列表计算列表中每条记录的总计

scala - 在 Scala 中交换不同类型的元组