Bind() static private method

static private Bind ( Assembly integrationAssembly, Type bindIntegrationType ) : void
integrationAssembly System.Reflection.Assembly
bindIntegrationType System.Type
return void
Example #1
0
        private static void LoadIntegration()
        {
            Assembly integrationAssembly = Assembly.Load("ExcelDna.Integration");
            Type     integrationType     = integrationAssembly.GetType("ExcelDna.Integration.Integration");

            // Get the methods that need to be called from the integration assembly
            MethodInfo tryExcelImplMethod       = typeof(XlCallImpl).GetMethod("TryExcelImpl", BindingFlags.Static | BindingFlags.Public);
            Type       tryExcelImplDelegateType = integrationAssembly.GetType("ExcelDna.Integration.TryExcelImplDelegate");
            Delegate   tryExcelImplDelegate     = Delegate.CreateDelegate(tryExcelImplDelegateType, tryExcelImplMethod);

            integrationType.InvokeMember("SetTryExcelImpl", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.InvokeMethod, null, null, new object[] { tryExcelImplDelegate });

            MethodInfo registerMethodsMethod       = typeof(XlAddIn).GetMethod("RegisterMethods", BindingFlags.Static | BindingFlags.Public);
            Type       registerMethodsDelegateType = integrationAssembly.GetType("ExcelDna.Integration.RegisterMethodsDelegate");
            Delegate   registerMethodsDelegate     = Delegate.CreateDelegate(registerMethodsDelegateType, registerMethodsMethod);

            integrationType.InvokeMember("SetRegisterMethods", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.InvokeMethod, null, null, new object[] { registerMethodsDelegate });

            MethodInfo getAssemblyBytesMethod       = typeof(AssemblyManager).GetMethod("GetAssemblyBytes", BindingFlags.Static | BindingFlags.NonPublic);
            Type       getAssemblyBytesDelegateType = integrationAssembly.GetType("ExcelDna.Integration.GetAssemblyBytesDelegate");
            Delegate   getAssemblyBytesDelegate     = Delegate.CreateDelegate(getAssemblyBytesDelegateType, getAssemblyBytesMethod);

            integrationType.InvokeMember("SetGetAssemblyBytesDelegate", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.InvokeMethod, null, null, new object[] { getAssemblyBytesDelegate });

            // set up helpers for future calls
            IntegrationHelpers.Bind(integrationAssembly);
            IntegrationMarshalHelpers.Bind(integrationAssembly);
        }
        private static void LoadIntegration()
        {
            // Get the assembly and ExcelIntegration type - will be loaded from file or from packed resources via AssemblyManager.AssemblyResolve.
            Assembly integrationAssembly = Assembly.Load("ExcelDna.Integration");

            // Check if ExcelDna.Integration was loaded from the Global Assembly Cache
            if (integrationAssembly.GlobalAssemblyCache)
            {
                // Prevent using the version in the GAC to avoid add-ins using the wrong version and breaking
                // https://github.com/Excel-DNA/ExcelDna/pull/250#issuecomment-508642133
                throw new InvalidOperationException("ExcelDna.Integration was loaded from Global Assembly Cache, and that's not allowed.");
            }

            Type integrationType = integrationAssembly.GetType("ExcelDna.Integration.ExcelIntegration");

            // Check the version declared in the ExcelIntegration class
            int integrationVersion = (int)integrationType.InvokeMember("GetExcelIntegrationVersion", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.InvokeMethod, null, null, null);

            if (integrationVersion != ExcelIntegrationVersion)
            {
                // This is not the version we are expecting!
                throw new InvalidOperationException("Invalid ExcelIntegration version detected.");
            }

            // Get the methods that need to be called from the integration assembly
            MethodInfo tryExcelImplMethod       = typeof(XlCallImpl).GetMethod("TryExcelImpl", BindingFlags.Static | BindingFlags.Public);
            Type       tryExcelImplDelegateType = integrationAssembly.GetType("ExcelDna.Integration.TryExcelImplDelegate");
            Delegate   tryExcelImplDelegate     = Delegate.CreateDelegate(tryExcelImplDelegateType, tryExcelImplMethod);

            integrationType.InvokeMember("SetTryExcelImpl", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.InvokeMethod, null, null, new object[] { tryExcelImplDelegate });

            MethodInfo registerMethodsMethod       = typeof(XlRegistration).GetMethod("RegisterMethods", BindingFlags.Static | BindingFlags.Public);
            Type       registerMethodsDelegateType = integrationAssembly.GetType("ExcelDna.Integration.RegisterMethodsDelegate");
            Delegate   registerMethodsDelegate     = Delegate.CreateDelegate(registerMethodsDelegateType, registerMethodsMethod);

            integrationType.InvokeMember("SetRegisterMethods", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.InvokeMethod, null, null, new object[] { registerMethodsDelegate });

            MethodInfo registerWithAttMethod       = typeof(XlRegistration).GetMethod("RegisterMethodsWithAttributes", BindingFlags.Static | BindingFlags.Public);
            Type       registerWithAttDelegateType = integrationAssembly.GetType("ExcelDna.Integration.RegisterMethodsWithAttributesDelegate");
            Delegate   registerWithAttDelegate     = Delegate.CreateDelegate(registerWithAttDelegateType, registerWithAttMethod);

            integrationType.InvokeMember("SetRegisterMethodsWithAttributes", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.InvokeMethod, null, null, new object[] { registerWithAttDelegate });

            MethodInfo registerDelAttMethod       = typeof(XlRegistration).GetMethod("RegisterDelegatesWithAttributes", BindingFlags.Static | BindingFlags.Public);
            Type       registerDelAttDelegateType = integrationAssembly.GetType("ExcelDna.Integration.RegisterDelegatesWithAttributesDelegate");
            Delegate   registerDelAttDelegate     = Delegate.CreateDelegate(registerDelAttDelegateType, registerDelAttMethod);

            integrationType.InvokeMember("SetRegisterDelegatesWithAttributes", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.InvokeMethod, null, null, new object[] { registerDelAttDelegate });

            MethodInfo getResourceBytesMethod       = typeof(AssemblyManager).GetMethod("GetResourceBytes", BindingFlags.Static | BindingFlags.NonPublic);
            Type       getResourceBytesDelegateType = integrationAssembly.GetType("ExcelDna.Integration.GetResourceBytesDelegate");
            Delegate   getResourceBytesDelegate     = Delegate.CreateDelegate(getResourceBytesDelegateType, getResourceBytesMethod);

            integrationType.InvokeMember("SetGetResourceBytesDelegate", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.InvokeMethod, null, null, new object[] { getResourceBytesDelegate });

            // set up helpers for future calls
            IntegrationHelpers.Bind(integrationAssembly, integrationType);
            IntegrationMarshalHelpers.Bind(integrationAssembly);
        }
Example #3
0
        private static void LoadIntegration()
        {
            // Get the assembly and ExcelIntegration type - will be loaded from file or from packed resources via AssemblyManager.AssemblyResolve.
            Assembly integrationAssembly = Assembly.Load("ExcelDna.Integration");
            Type     integrationType     = integrationAssembly.GetType("ExcelDna.Integration.ExcelIntegration");

            // Check the version declared in the ExcelIntegration class
            int integrationVersion = (int)integrationType.InvokeMember("GetExcelIntegrationVersion", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.InvokeMethod, null, null, null);

            if (integrationVersion != ExcelIntegrationVersion)
            {
                // This is not the version we are expecting!
                throw new InvalidOperationException("Invalid ExcelIntegration version detected.");
            }

            // Get the methods that need to be called from the integration assembly
            MethodInfo tryExcelImplMethod       = typeof(XlCallImpl).GetMethod("TryExcelImpl", BindingFlags.Static | BindingFlags.Public);
            Type       tryExcelImplDelegateType = integrationAssembly.GetType("ExcelDna.Integration.TryExcelImplDelegate");
            Delegate   tryExcelImplDelegate     = Delegate.CreateDelegate(tryExcelImplDelegateType, tryExcelImplMethod);

            integrationType.InvokeMember("SetTryExcelImpl", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.InvokeMethod, null, null, new object[] { tryExcelImplDelegate });

            MethodInfo registerMethodsMethod       = typeof(XlRegistration).GetMethod("RegisterMethods", BindingFlags.Static | BindingFlags.Public);
            Type       registerMethodsDelegateType = integrationAssembly.GetType("ExcelDna.Integration.RegisterMethodsDelegate");
            Delegate   registerMethodsDelegate     = Delegate.CreateDelegate(registerMethodsDelegateType, registerMethodsMethod);

            integrationType.InvokeMember("SetRegisterMethods", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.InvokeMethod, null, null, new object[] { registerMethodsDelegate });

            MethodInfo registerWithAttMethod       = typeof(XlRegistration).GetMethod("RegisterMethodsWithAttributes", BindingFlags.Static | BindingFlags.Public);
            Type       registerWithAttDelegateType = integrationAssembly.GetType("ExcelDna.Integration.RegisterMethodsWithAttributesDelegate");
            Delegate   registerWithAttDelegate     = Delegate.CreateDelegate(registerWithAttDelegateType, registerWithAttMethod);

            integrationType.InvokeMember("SetRegisterMethodsWithAttributes", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.InvokeMethod, null, null, new object[] { registerWithAttDelegate });

            MethodInfo registerDelAttMethod       = typeof(XlRegistration).GetMethod("RegisterDelegatesWithAttributes", BindingFlags.Static | BindingFlags.Public);
            Type       registerDelAttDelegateType = integrationAssembly.GetType("ExcelDna.Integration.RegisterDelegatesWithAttributesDelegate");
            Delegate   registerDelAttDelegate     = Delegate.CreateDelegate(registerDelAttDelegateType, registerDelAttMethod);

            integrationType.InvokeMember("SetRegisterDelegatesWithAttributes", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.InvokeMethod, null, null, new object[] { registerDelAttDelegate });

            MethodInfo getFuncRegInfoMethod       = typeof(XlRegistration).GetMethod("GetFunctionRegistrationInfo", BindingFlags.Static | BindingFlags.Public);
            Type       getFuncRegInfoDelegateType = integrationAssembly.GetType("ExcelDna.Integration.GetFunctionRegistrationInfoDelegate");
            Delegate   getFuncRegInfoDelegate     = Delegate.CreateDelegate(getFuncRegInfoDelegateType, getFuncRegInfoMethod);

            integrationType.InvokeMember("SetGetFunctionRegistrationInfo", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.InvokeMethod, null, null, new object[] { getFuncRegInfoDelegate });

            MethodInfo getResourceBytesMethod       = typeof(AssemblyManager).GetMethod("GetResourceBytes", BindingFlags.Static | BindingFlags.NonPublic);
            Type       getResourceBytesDelegateType = integrationAssembly.GetType("ExcelDna.Integration.GetResourceBytesDelegate");
            Delegate   getResourceBytesDelegate     = Delegate.CreateDelegate(getResourceBytesDelegateType, getResourceBytesMethod);

            integrationType.InvokeMember("SetGetResourceBytesDelegate", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.InvokeMethod, null, null, new object[] { getResourceBytesDelegate });

            // set up helpers for future calls
            IntegrationHelpers.Bind(integrationAssembly, integrationType);
            IntegrationMarshalHelpers.Bind(integrationAssembly);
        }