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,
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

望天hous

你的鼓励是我最大动力~谢谢啦!

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

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

打赏作者

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

抵扣说明:

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

余额充值