Unity热更新方案C#Like(七)-详解支持的C#特性:get/set访问器

C#Like是Unity的热更方案,使用纯C#语言写出可以热更新的代码,就像可以在所有平台使用DLL(动态链接库)文件一样.遵从KISS设计原则,让用户轻松构建或升级成Unity的热更新项目.

简介

本篇主要介绍C#Like支持的C#特性:get/set访问器.

  • C#Like免费版:支持自动实现的get/set 
  • C#Like完整版:多了可以自定义实现get/set,同时也支持表达式主体(在十六章详解)

C#Like免费版:

示范代码如下:

using UnityEngine;  
  
namespace CSharpLike  
{  
    public partial class ExampleCSharp : LikeBehaviour  
    {  
        void TestGetSetAccessor()  
        {  
            Debug.LogError("示范get/set访问器: 你不能在C#Like免费版里使用自定义get/set访问器. (在完整版已支持).强烈推荐升级到完整版: ";  
            // 我们提供以下方案给免费用户:  
            // 自定义get/set访问器: 直接使用函数来代替  
              
            //测试自动实现的get/set访问器  
            Debug.Log("before set value testGetSetAutoImp = " + testGetSetAutoImp);//输出 False  
            testGetSetAutoImp = true;  
            Debug.Log("after set value: testGetSetAutoImp = " + testGetSetAutoImp);//输出 True  
  
        }  
  
        public bool testGetSetAutoImp { get;set; }  
    }  
} 

 C#Like完整版:

示范代码如下:

using UnityEngine;  
  
namespace CSharpLike  
{  
    public partial class ExampleCSharp : LikeBehaviour  
    {  
        void TestGetSetAccessor()  
        {  
            Debug.LogError("Test TestGetSetAccessor:");  
            //测试自动实现的get/set访问器  
            Debug.Log("before set value testGetSetAutoImp = " + testGetSetAutoImp);//输出 False  
            testGetSetAutoImp = true;  
            Debug.Log("after set value: testGetSetAutoImp = " + testGetSetAutoImp);//输出 True  
  
            //测试自定义实现的get/set访问器  
            Debug.Log("before set value testGetSet = " + testGetSetCustom);//输出 101  
            testGetSetCustom = 1;  
            Debug.Log("after set value: testGetSet = " + testGetSetCustom);//输出 1  
  
            //测试静态的自定义实现的get/set访问器  
            Debug.Log("before set value testStaticGetSet = " + testStaticGetSet);//输出 'default string'  
            testStaticGetSet = "I like C#";  
            Debug.Log("after set value: testStaticGetSet = " + testStaticGetSet);//输出 'I like C#'  
        }  
  
        public bool testGetSetAutoImp { get;set; }  
  
        int mTestGetSetCustom = 101;  
        public int testGetSetCustom  
        {  
            get  
            {  
                return mTestGetSetCustom;  
            }  
            private set  
            {  
                mTestGetSetCustom = value;  
            }  
        }  
        static string mTestStaticGetSet = "default string";  
        public static string testStaticGetSet  
        {  
            get  
            {  
                return mTestStaticGetSet;  
            }  
            set  
            {  
                if (!string.IsNullOrEmpty(value))  
                    mTestStaticGetSet = value;  
                else  
                {  
                    Debug.Log("set value null or empty,reset to default string 'default string'");  
                    mTestStaticGetSet = "default string";  
                }  
            }  
        }  
    }  
}

 本系列文章导读:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

C#Like

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

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

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

打赏作者

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

抵扣说明:

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

余额充值