C# 通用类 备忘,用于重写操作符的示例

/* ==============================================================================
 * 功能描述:Property  
 * 创 建 者:Dell
 * 创建日期:2021/6/15 15:06:27
 * ==============================================================================*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ClassLibrary1
{
    /// <summary>
    /// Property
    /// </summary>
    [Serializable]
    public class Property<T> where T : struct
    {

        #region Constructor

        public Property(string lab, T val, PropertyType type = PropertyType.Sub)
        {
            this.Label = lab;
            this.Value = val;
            this.Type = type;
        }

        public Property(T val) : this(string.Empty, val)
        {

        }
        public Property()
        {

        }

        #endregion

        #region  Filed

        #endregion

        #region Property

        public string Label { get; init; }

        public PropertyType Type { get; init; }

        public T Value { get; init; }



        #endregion

        #region 隐士转换 Methods

        //无效方法
        //public static implicit operator Property<T>(int obj)
        //{
        //    object ob = obj;
        //    return new Property<T>((T)ob);
        //}

        #endregion

        #region Public Methods

        public static Property<T> operator +(Property<T> a, Property<T> b)
        {
            //这个必须是的有的
            Property<T> prop = new Property<T>();

            #region 比较笨的一种方式,会发生装箱和拆箱操作,以object为中间桥梁

            //这个也可以直接判断 type 类型判断。进行转化

            //if (a.Label == b.Label && a.Type == b.Type)
            //{
            //    object aObj = a.Value;
            //    object bObj = b.Value;
            //    object addObj;
            //    if (aObj is int)
            //    {
            //        addObj = (int)aObj + (int)bObj;
            //    }
            //    else if (aObj is float)
            //    {
            //        addObj = (int)aObj + (int)bObj;

            //    }
            //    else if (aObj is double)
            //    {
            //        addObj = (int)aObj + (int)bObj;
            //    }
            //    else
            //    {
            //        return new Property<T>();
            //    }
            //    return new Property<T>(a.Label, (T)addObj,a.Type);
            //}

            #endregion

            #region 使用动态类型绑定

            //这种方式比较优雅  
            if (a.Label == b.Label && a.Type == b.Type)
            {
                if (typeof(T) != typeof(bool))
                {
                    dynamic aDyna = a.Value;
                    dynamic bDyna = b.Value;
                    return new Property<T>(a.Label, (T)(aDyna + bDyna));
                }
            }

            #endregion
            return prop;

        }

        //等号为啥补鞥呢重载
        //public override static Property<T> operator =(object obj)
        //{
        //    return new Property<T>(obj.ToString(), (T)obj);

        //}


        #endregion

    }

    public enum PropertyType
    {
        Main,
        Sub,
    }
}

上一篇:Mac - npm ERR! Cannot read property 'resolve' of undefined


下一篇:Firewalld防火墙