C#泛型的嵌套定义

本文介绍了一个泛型树状结构的实现方法,并通过具体的子类实现了节点授权功能。该结构支持查找指定节点,适用于构建流程或组织结构等场景。文章还展示了如何通过类型判断和转换处理不同类型的节点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    public class yjProcessNode<T> where T : yjProcessNode<T>
    {
        //SortedList不允许有重复的Key,因为SortNumber可能重复,因此不能用
        internal protected List<T> FChildren = new List<T>();
        internal protected bool FIsRoot = true;
        internal protected T FParent = null;
        internal protected csProcess FProcess = null;
        public csProcess Process { get { return FProcess; } }
        public List<T> Children { get { return FChildren; } }
        public bool IsRoot { get { return FIsRoot; } }
        public T Parent { get { return FParent; } }

        public bool Find(T ANode)
        {
            if (ANode == this) return true;
            else
            {
                foreach (T n in FChildren)
                {
                    if (n.Find(ANode))
                        return true;
                }
            }
            return false;
        }
    }

    public class yjProcessNode1 : yjProcessNode<yjProcessNode1>
    {
        internal protected bool FIsAuthorized = true;
        public bool IsAuthorized { get { return FIsAuthorized; } }
    }

    public class yjProcessNode2 : yjProcessNode<yjProcessNode2>
    {
        internal protected Dictionary<string, byte> FAuthoriy;
        public yjProcessNode2()
        {
            FAuthoriy = new Dictionary<string, byte>();
        }

        public byte GetActionAuthority(string AActionID)
        {
            byte vValue=0;
            if (!FAuthoriy.TryGetValue(AActionID, out vValue))
                vValue = 0;
            return vValue;
        }
    }

使用Convert.ChangeType来做强制转换:

private static List<T> _BuildUserProcessAuthorityView<T>(string AUserID, string AProcessID,
            out bool AIsAdministrator) where T : yjProcessNode<T>, new()
.......
    bool vIsProcessNode2 = (typeof(T) == typeof(yjProcessNode2)) || (typeof(T).IsSubclassOf(typeof(yjProcessNode2)));
    yjProcessNode2 n2 = (yjProcessNode2)Convert.ChangeType(n.Value, typeof(yjProcessNode2));



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

火星牛

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值