Ejemplo n.º 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="CSharpTypeTableCodeWriter"/> class.
        /// </summary>
        /// <remarks>
        /// Get DispatchTable namespace from the custom assembly attribute.
        /// </remarks>
        /// <param name="sourceTypesAssembly"></param>
        protected CSharpTypeTableCodeWriter(Assembly sourceTypesAssembly)
        {
            // Get the global name of the dispatch table.
            // Used primarily for defining a dispatch base table index.
            //
            DispatchTableNamespaceAttribute dispatchTableCSharpNamespaceAttribute = sourceTypesAssembly.GetCustomAttribute <DispatchTableNamespaceAttribute>();

            DispatchTableCSharpNamespace = dispatchTableCSharpNamespaceAttribute?.Namespace;
        }
Ejemplo n.º 2
0
        /// <summary>Registers settings assembly.</summary>
        /// <remarks>Update deserialize and dispatch tables.</remarks>
        /// <param name="assembly"></param>
        /// <param name="dispatchTableBaseIndex"></param>
        public void RegisterAssembly(Assembly assembly, uint dispatchTableBaseIndex)
        {
            // Ensure the assembly base index is correct.
            //
            if (settingsAssemblies.ContainsKey(assembly.FullName))
            {
                uint existingDispatchTableBaseIndex = settingsAssemblies[assembly.FullName];

                if (existingDispatchTableBaseIndex == dispatchTableBaseIndex)
                {
                    // Assembly is already registered.
                    //
                    return;
                }

                throw new InvalidOperationException($"Trying to register settings assembly {assembly.FullName} {existingDispatchTableBaseIndex} with different dispatch table base index {dispatchTableBaseIndex}.");
            }

            if (dispatchTableBaseIndex != CodegenTypeCount)
            {
                throw new InvalidOperationException($"Register settings assembly {assembly.FullName} failed.");
            }

            // Get the dispatcher table.
            //
            DispatchTableNamespaceAttribute dispatchTableNamespaceAttribute = assembly.GetCustomAttribute <DispatchTableNamespaceAttribute>();

            // Update the assembly dispatch table base index.
            //
            string    typeName = $"{dispatchTableNamespaceAttribute.Namespace}.ObjectDeserializeHandler";
            Type      objectDeserializeHandler = assembly.GetType(typeName);
            FieldInfo fieldInfo = objectDeserializeHandler.GetField("DispatchTableBaseIndex", BindingFlags.Public | BindingFlags.Static);

            fieldInfo.SetValue(null, CodegenTypeCount);

            DispatchEntry[]    dispatchTable        = (DispatchEntry[])objectDeserializeHandler.GetField("DispatchTable", BindingFlags.Static | BindingFlags.Public).GetValue(null);
            DeserializeEntry[] deserializationTable = (DeserializeEntry[])objectDeserializeHandler.GetField("DeserializationCallbackTable", BindingFlags.Static | BindingFlags.Public).GetValue(null);

            // Init module.
            //
            Type callbackHandlersType = assembly.GetType($"{dispatchTableNamespaceAttribute.Namespace}.AssemblyInitializer");

            if (callbackHandlersType != null)
            {
                System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(callbackHandlersType.TypeHandle);
            }

            // Update global dispatch table.
            //
            globalDispatchTable.AddRange(dispatchTable);
            globalDeserializationTable.AddRange(deserializationTable);

            // Add settings assembly.
            //
            settingsAssemblies.Add(assembly.FullName, dispatchTableBaseIndex);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <remarks>
        /// Get DispatchTable namespace from the custom assembly attribute.
        /// </remarks>
        /// <param name="sourceTypesAssembly"></param>
        protected CppTypeTableCodeWriter(Assembly sourceTypesAssembly)
        {
            // Get the global name of the dispatch table.
            // Used primarily for defining a dispatch base table index.
            //
            DispatchTableNamespaceAttribute dispatchTableCppNamespaceAttribute = sourceTypesAssembly.GetCustomAttribute <DispatchTableNamespaceAttribute>();

            DispatchTableCppNamespace = $"::{Constants.ObjectDeserializationHandler}";

            if (dispatchTableCppNamespaceAttribute != null)
            {
                DispatchTableCppNamespace = $"{dispatchTableCppNamespaceAttribute.Namespace.Replace(".", "::")}{DispatchTableCppNamespace}";
            }
        }