ET框架:Server(3),自动注册序列化类

7 篇文章 0 订阅
6 篇文章 0 订阅

Add完程序集准备好EventSystem后,Main调用了一个空方法MongoHelper.init() 其目的是为了执行MongoHelper的静态构造函数进行初始化,类的静态构造函数会在这个类的任何东西被引用前执行。

MongoHelper的静态构造函数

扫描程序集中所有非泛型Entity子类,将他们注册到mongo驱动中
目的是让所有Entity子类能被序列化

        static MongoHelper()
        {
			// 自动注册IgnoreExtraElements
			// IgnoreExtraElementsConvention:忽略库中有但是类中没有定义的字段。这个一般用于敏感字段处理
			ConventionPack conventionPack = new ConventionPack { new IgnoreExtraElementsConvention(true) };
	        
	        ConventionRegistry.Register("IgnoreExtraElements", conventionPack, type => true);

			// 遍历Game所在程序集的所有 非泛型Entity子类
			Type[] types = typeof(Game).Assembly.GetTypes();

            foreach (Type type in types)
            {
				// 跳过非Entity子类
                if (!type.IsSubclassOf(typeof(Entity)))
                {
                    continue;
                }
				// 跳过泛型
                if (type.IsGenericType)
                {
                    continue;
                }

                try
                {
					// 注册后,这个type就可以序列化了
	                BsonClassMap.LookupClassMap(type);
                }
                catch (Exception e)
                {
	                Log.Error($"11111111111111111: {type.Name} {e}");
                }
                
            }
            // 其他一些注册
#if SERVER
            BsonSerializer.RegisterSerializer(typeof(Vector3), new StructBsonSerialize<Vector3>());
#else
            BsonSerializer.RegisterSerializer(typeof(Vector4), new StructBsonSerialize<Vector4>());
            BsonSerializer.RegisterSerializer(typeof(Vector2Int), new StructBsonSerialize<Vector2Int>());
#endif
        }

完全的自动化注册,使用者也不需要关心类是否注册了。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值