C#属性绑定-创新互联-古蔺大橙子建站
RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
C#属性绑定-创新互联

C#的反射明显的比AS3的反射要强很多啊。可以从外部访问到类的私有成员。举个例子:

在遂溪等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供成都网站制作、成都网站设计 网站设计制作按需定制开发,公司网站建设,企业网站建设,品牌网站建设,营销型网站建设,外贸营销网站建设,遂溪网站建设费用合理。
                Type _class = this.GetType();
                FieldInfo _field = _class.GetField(@fieldName, BindingFlags.Instance | BindingFlags.NonPublic);

枚举 : BindingFlags.NonPublic->非公共成员将包括在内进行搜索,意思就是private成员也可以搜索到。

在model中注册须发的主题(User)

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

namespace BindDemo.com.model
{
    public sealed class User : BaseDataModel
    {
        private int _hp = 1000;
        public int Hp
        {
            get { return _hp; }
            //绑定hp(血量)
            set { this.ChangeValue("Hp","_hp",value); }
        }

        private bool _isLive = false;

        public bool IsLive
        {
            get { return _isLive; }
            set { this.ChangeValue("IsLive", "_isLive", value); }
        }
    }
}

这的注意的是字段_hp和字段_isLive都是private(私用的): 这个和AS3绑定(见 AS3 属性绑定/上一篇)有很大的不同,这得益于C#可以从外部访问private

view中需要绑定(注册侦听)(UserView)

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

namespace BindDemo.com.model
{
    public sealed class UserView
    {
        public User user;
        private int _hp;
        public int Hp
        {
            set { 
                this._hp = value ;
                Console.WriteLine("血量 {0}", value);
            }
            get { return this._hp; }
        }
        public UserView()
        {
            user = new User();
            BindTools.BindProperty(this, "Hp", user, "Hp", true);
            BindTools.BindSetter(this.UpdateIsLive, user, "IsLive",true);
        }

        private void UpdateIsLive(bool @isLive)
        {
            Console.WriteLine(@isLive ? "活着" : "战死");
        }

    }
}

好了 , 具体参数意思可以参考(《AS3 属性绑定》篇,上篇)

测试:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BindDemo.com.model;

namespace BindDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            UserView _uM = new UserView();
            Console.WriteLine("获得hp : {0}" , _uM.Hp);
            User uR = _uM.user;
            uR.Hp = 89;
            Console.WriteLine("改变获得hp : {0}", _uM.Hp);
            Console.WriteLine("-------------------------------------------------");
            uR.IsLive = true;
            Console.WriteLine("#################################################");
            uR.IsLive = false;
            Console.ReadLine();
        }
    }
}

结果:

C# 属性绑定

附件:http://down.51cto.com/data/2366862

创新互联www.cdcxhl.cn,专业提供香港、美国云服务器,动态BGP最优骨干路由自动选择,持续稳定高效的网络助力业务部署。公司持有工信部办法的idc、isp许可证, 机房独有T级流量清洗系统配攻击溯源,准确进行流量调度,确保服务器高可用性。佳节活动现已开启,新人活动云服务器买多久送多久。


分享题目:C#属性绑定-创新互联
新闻来源:http://scgulin.cn/article/jojoi.html