示例#1
0
        static object DeserializeObject(ref Utf8JsonReader p_reader, string p_alias, Type p_tgtType)
        {
            Type type = SerializeTypeAlias.GetType(p_alias);

            if (p_tgtType != null && p_tgtType != type)
            {
                // 以子类型为准
                if (p_tgtType.IsSubclassOf(type))
                {
                    type = p_tgtType;
                }
                else if (!type.IsSubclassOf(p_tgtType))
                {
                    throw new Exception($"{p_tgtType.Name} 与 {type.Name} 无继承关系!");
                }
            }

            // 自定义序列化
            if (type.GetInterface("IRpcJson") != null)
            {
                // 无参数构造方法可能为private,如实体类型
                object tgt = Activator.CreateInstance(type, true);
                ((IRpcJson)tgt).ReadRpcJson(ref p_reader);
                return(tgt);
            }

            // 标准序列化
            p_reader.Read();
            object obj = JsonSerializer.Deserialize(ref p_reader, type);

            return(obj);
        }
示例#2
0
 static void SerializeObject(object p_value, Utf8JsonWriter p_writer)
 {
     p_writer.WriteStartArray();
     p_writer.WriteStringValue("#" + SerializeTypeAlias.GetAlias(p_value.GetType()));
     JsonSerializer.Serialize(p_writer, p_value);
     p_writer.WriteEndArray();
 }
示例#3
0
        static void SerializeArray(IEnumerable p_value, Utf8JsonWriter p_writer)
        {
            p_writer.WriteStartArray();
            p_writer.WriteStringValue("&" + SerializeTypeAlias.GetAlias(p_value.GetType()));
            if (p_value is List <object> lo)
            {
                // 记录item类型
                foreach (object item in lo)
                {
                    p_writer.WriteStartArray();

                    if (item != null)
                    {
                        var tp = item.GetType();
                        if (tp.FullName == "System." + tp.Name)
                        {
                            // 简单类型
                            p_writer.WriteStringValue(tp.Name);
                        }
                        else if (tp.IsGenericType && tp.GetGenericTypeDefinition() == typeof(Nullable <>))
                        {
                            // 可空值类型
                            p_writer.WriteStringValue(tp.GetGenericArguments()[0].Name + "?");
                        }
                        else
                        {
                            // 复杂类型空即可
                            p_writer.WriteStringValue("");
                        }
                        Serialize(item, p_writer);
                    }
                    else
                    {
                        // null按string
                        p_writer.WriteStringValue("String");
                        p_writer.WriteNullValue();
                    }

                    p_writer.WriteEndArray();
                }
            }
            else
            {
                foreach (object item in p_value)
                {
                    Serialize(item, p_writer);
                }
            }
            p_writer.WriteEndArray();
        }
示例#4
0
        /// <summary>
        /// 系统初始化
        /// </summary>
        /// <param name="p_stub">系统存根</param>
        /// <param name="p_callback"></param>
        internal static void Startup(IStub p_stub, ICallback p_callback)
        {
            Stub     = p_stub;
            Callback = p_callback;
            if (Stub.SerializeTypes != null)
            {
                SerializeTypeAlias.Merge(Stub.SerializeTypes);
            }

            var app = Application.Current;

            app.Suspending += OnSuspending;
            app.Resuming   += OnResuming;

            // 异常处理
#if UWP
            app.UnhandledException += OnUwpUnhandledException;
#elif ANDROID
            Android.Runtime.AndroidEnvironment.UnhandledExceptionRaiser += OnAndroidUnhandledException;
#elif IOS
            // 在iOS项目的Main函数处理
#elif WASM
            //TaskScheduler.UnobservedTaskException += (s, e) => OnUnhandledException(e.Exception);
            AppDomain.CurrentDomain.UnhandledException += (s, e) => OnUnhandledException(e.ExceptionObject as Exception);
#endif

            // 创建本地文件存放目录
            if (!Directory.Exists(CachePath))
            {
                Directory.CreateDirectory(CachePath);
            }
            if (!Directory.Exists(DataPath))
            {
                Directory.CreateDirectory(DataPath);
            }

#if WASM
            // .net5.0 只能引用 SQLite3Provider_sqlite3,DllImport("sqlite3")
            // 默认为 SQLite3Provider_e_sqlite3 引用时出错!
            SQLitePCL.raw.SetProvider(new SQLitePCL.SQLite3Provider_sqlite3());
#else
            // 初始化不同平台的包绑定!V2支持类型和属性的绑定
            // 内部调用 SQLitePCL.raw.SetProvider(new SQLitePCL.SQLite3Provider_e_sqlite3());
            SQLitePCL.Batteries_V2.Init();
#endif

            // 打开状态库
            AtState.OpenDb();
        }
示例#5
0
        static object DeserializeArray(ref Utf8JsonReader p_reader, string p_alias)
        {
            // 只支持List<T>的情况
            Type type     = SerializeTypeAlias.GetType(p_alias);
            Type itemType = type.GetGenericArguments()[0];

            if (itemType == typeof(object))
            {
                return(DeserializeObjsArray(ref p_reader));
            }

            IList target = Activator.CreateInstance(type) as IList;

            while (p_reader.Read() && p_reader.TokenType != JsonTokenType.EndArray)
            {
                target.Add(Deserialize(ref p_reader, itemType));
            }
            return(target);
        }
示例#6
0
        /// <summary>
        /// 注入服务,提取程序集中的Api列表、事件处理类型、服务列表、可序列化类型列表,注册服务,添加拦截
        /// </summary>
        /// <param name="p_services"></param>
        /// <returns></returns>
        public static IServiceProvider ConfigureServices(IServiceCollection p_services)
        {
            var builder = new ContainerBuilder();

            builder.Populate(p_services);

            //// 提取有用类型,程序集包括Dt.Core、微服务、插件(以.Addin.dll结尾)
            //List<Assembly> asms = new List<Assembly> { Kit.Stub.GetType().Assembly, typeof(Silo).Assembly };
            //asms.AddRange(Directory
            //    .EnumerateFiles(Path.GetDirectoryName(typeof(Silo).Assembly.Location), "*.Addin.dll", SearchOption.TopDirectoryOnly)
            //    .Select(AssemblyLoadContext.Default.LoadFromAssemblyPath));

            // 提取微服务和Dt.Core程序集
            var types = Kit.Stub.GetType().Assembly.GetTypes()
                        .Concat(typeof(Silo).Assembly.GetTypes())
                        .Where(type => type != null && type.IsClass && type.IsPublic && !type.IsAbstract);

            foreach (Type type in types)
            {
                // 提取Api
                ApiAttribute rpcAttr = type.GetCustomAttribute <ApiAttribute>(false);
                if (rpcAttr != null)
                {
                    ExtractApi(type, rpcAttr, builder);
                    continue;
                }

                // 注册事件处理
                if (IsEventHandler(type, builder))
                {
                    continue;
                }

                // 注册服务,支持继承的SvcAttribute
                SvcAttribute svcAttr = type.GetCustomAttribute <SvcAttribute>(true);
                if (svcAttr != null)
                {
                    var itps = type.GetInterfaces();
                    if (itps.Length > 0)
                    {
                        // 注册接口类型
                        builder
                        .RegisterType(type)
                        .As(type)
                        .As(itps)
                        .ConfigureLifecycle(svcAttr.Lifetime, null);
                    }
                    else
                    {
                        builder
                        .RegisterType(type)
                        .ConfigureLifecycle(svcAttr.Lifetime, null);
                    }
                    continue;
                }

                // 实体类型字典
                if (type.IsSubclassOf(typeof(Entity)))
                {
                    var tbl = type.GetCustomAttribute <TblAttribute>(false);
                    if (tbl != null && !string.IsNullOrEmpty(tbl.Name))
                    {
                        _entityDict[tbl.Name.ToLower()] = type;
                    }
                }

                // 自定义json序列化对象
                JsonObjAttribute serAttr = type.GetCustomAttribute <JsonObjAttribute>(false);
                if (serAttr != null)
                {
                    SerializeTypeAlias.Add(serAttr.Alias, type);
                }
            }

            // 内部服务管理Api
            ExtractApi(typeof(Admin), null, builder);
            Log.Information("注入服务成功");

            return(new AutofacServiceProvider(builder.Build()));
        }