当前位置: 首页 > 编程笔记 >

C#实现动态加载dll的方法

颜修明
2023-03-14
本文向大家介绍C#实现动态加载dll的方法,包括了C#实现动态加载dll的方法的使用技巧和注意事项,需要的朋友参考一下

本文实例讲述了C#实现动态加载dll的方法。分享给大家供大家参考。具体实现方法如下:


using System;

using System.Collections.Generic;

using System.Text;

using System.Reflection;

using System.IO;

namespace Alif.CommonAPI.DynamicLoadAssembly {     public class AssemblyDynamicLoader<T>     {         private AppDomain appDomain;

        private DynamicRemoteLoadAssembly<T> remoteLoader;

        public T InvokeMethod(string assemblyName, string assemblyPath, string assemblyConfigFilePath, string fullClassName, string methodName, params object[] args)         {             AppDomainSetup setup = new AppDomainSetup();             setup.ApplicationName = "ApplicationLoader";             setup.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory + @"bin\";             //setup.PrivateBinPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "private");             setup.CachePath = setup.ApplicationBase;             setup.ShadowCopyFiles = "true";             if (assemblyConfigFilePath != string.Empty)             {                 setup.ConfigurationFile = AppDomain.CurrentDomain.BaseDirectory + assemblyConfigFilePath;             }             setup.ShadowCopyDirectories = setup.ApplicationBase;             setup.LoaderOptimization = LoaderOptimization.SingleDomain;

            this.appDomain = AppDomain.CreateDomain("ApplicationLoaderDomain", null, setup);             String name = Assembly.GetExecutingAssembly().GetName().FullName;

            this.remoteLoader = (DynamicRemoteLoadAssembly<T>)this.appDomain.CreateInstanceAndUnwrap(name, typeof(DynamicRemoteLoadAssembly<T>).FullName);

            assemblyName = AppDomain.CurrentDomain.BaseDirectory + assemblyPath + assemblyName;

            return this.remoteLoader.InvokeMethod(assemblyName, fullClassName, methodName, args);         }

        /// <summary>         ///         /// </summary>         public void Unload()         {             try             {                 AppDomain.Unload(this.appDomain);                 this.appDomain = null;             }             catch (CannotUnloadAppDomainException ex)             {

            }         }     } }


using System;

using System.Collections.Generic;

using System.Text;

using System.Reflection;

using System.Globalization;

namespace Alif.CommonAPI.DynamicLoadAssembly {     public class DynamicRemoteLoadAssembly<T> : MarshalByRefObject     {         private Assembly assembly = null;

        public T InvokeMethod(string assemblyPath, string fullClassName, string methodName, params object[] args)         {             this.assembly = null;             T result = default(T);             try             {                 this.assembly = Assembly.LoadFile(assemblyPath);                 Type pgmType = null;                 if (this.assembly != null)                 {                     pgmType = this.assembly.GetType(fullClassName, true, true);                 }                 else                 {                     pgmType = Type.GetType(fullClassName, true, true);                 }                 BindingFlags defaultBinding = BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.IgnoreCase | BindingFlags.InvokeMethod | BindingFlags.Static;                 CultureInfo cultureInfo = new CultureInfo("es-ES", false);                 try                 {                     MethodInfo methisInfo = assembly.GetType(fullClassName, true, true).GetMethod(methodName);

                    if (methisInfo == null)                     {                         new Exception("EMethod does not exist!");                     }

                    if (methisInfo.IsStatic)                     {                         if (methisInfo.GetParameters().Length == 0)                         {                             if (methisInfo.ReturnType == typeof(void))                             {                                 pgmType.InvokeMember(methodName, defaultBinding, null, null, null, cultureInfo);                             }                             else                             {                                 result = (T)pgmType.InvokeMember(methodName, defaultBinding, null, null, null, cultureInfo);                             }                         }                         else                         {                             if (methisInfo.ReturnType == typeof(void))                             {                                 pgmType.InvokeMember(methodName, defaultBinding, null, null, args, cultureInfo);                             }

                            else                             {                                 result = (T)pgmType.InvokeMember(methodName, defaultBinding, null, null, args, cultureInfo);                             }                         }                     }                     else                     {

                        if (methisInfo.GetParameters().Length == 0)                         {                             object pgmClass = Activator.CreateInstance(pgmType);                             if (methisInfo.ReturnType == typeof(void))                             {                                 pgmType.InvokeMember(methodName, defaultBinding, null, pgmClass, null, cultureInfo);                             }                             else                             {                                 result = (T)pgmType.InvokeMember(methodName, defaultBinding, null, pgmClass, null, cultureInfo);                             }                         }                         else                         {                             object pgmClass = Activator.CreateInstance(pgmType);                             if (methisInfo.ReturnType == typeof(void))                             {                                 pgmType.InvokeMember(methodName, defaultBinding, null, pgmClass, args, cultureInfo);                             }                             else                             {                                 result = (T)pgmType.InvokeMember(methodName, defaultBinding, null, pgmClass, args, cultureInfo);                             }                         }                     }                 }                 catch (Exception e)                 {                     result = (T)pgmType.InvokeMember(methodName, defaultBinding, null, null, null, cultureInfo);                 }                 return result;             }             catch (Exception ee)             {                 return result;             }         }     } }

希望本文所述对大家的C#程序设计有所帮助。

 类似资料:
  • 本文向大家介绍C++调用C#的DLL实现方法,包括了C++调用C#的DLL实现方法的使用技巧和注意事项,需要的朋友参考一下 SwfDotNet是C#编写的,这是个特别好的读写Swf文件的库。本文讲述了在C++项目中,怎么让C++调用C#的DLL动态链接库文件。 具体的实现步骤如下: 一、创建C# DLL,需要指定应用类型为“类库”,代码: 二、C++客户程序,是个控制台应用,代码: 三、这里有几点

  • 我有64位Windows 7、IIS 7,并在IIS上安装了PHP。我启用了php_oci8。用于Oracle连接的dll,但出现以下错误: 我已经检查了php_oci8。dll存在于:“C:\Program Files(x86)\PHP\v5”中。5\ext\php\u oci8。dll' 我的php。见下: 我在http://www.oracle.com/technetwork/topics/

  • 本文向大家介绍C#中GridView动态添加列的实现方法,包括了C#中GridView动态添加列的实现方法的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了C#中GridView动态添加列的实现方法。分享给大家供大家参考。具体如下: 希望本文所述对大家的C#程序设计有所帮助。

  • 本文向大家介绍c++实现加载so动态库中的资源,包括了c++实现加载so动态库中的资源的使用技巧和注意事项,需要的朋友参考一下 实例如下: 以上就是小编为大家带来的c++实现加载so动态库中的资源全部内容了,希望大家多多支持呐喊教程~

  • 本文向大家介绍jQuery动态加载css文件实现方法,包括了jQuery动态加载css文件实现方法的使用技巧和注意事项,需要的朋友参考一下 有时我们可能会需要使用 jQuery 来加载一个外部的 css 文件,如在切换页面布局时。思路是创建一个 link 元素,并将它添加到 标记中即可,下边首先看看怎么使用 jQuery 来实现。 下边是我喜欢的写法: 有些朋友可能会使用下边的写法,只是形式有些小

  • 本文向大家介绍AJAX和jQuery动态加载数据的实现方法,包括了AJAX和jQuery动态加载数据的实现方法的使用技巧和注意事项,需要的朋友参考一下 什么是AJAX? 这里的AJAX不是希腊神话里的英雄,也不是清洁剂品牌,更不是一门语言,而是指异步Javascript和XML(Asynchronous JavaScript And XML),这里的XML(数据格式)也可以是纯文本(Plain T