应用程序RELEASE编译后,需要应用与应用程序在同一目录下不同文件夹下的C#.NET DLL 如何实现?

解决方案 »

  1.   

    应用程序RELEASE编译后,需要引用与应用程序在同一目录下不同文件夹下的C#.NET DLL 如何实现?
      

  2.   

    编译前引用,不然怎么能认识呢或者动态载入Dll,没用过,应该有动态载的方法。
      

  3.   

    我记得我RELEASE之后都可以正常调用自己写的类库,不太明白你要问什么.
    http://hi.baidu.com/winnyang/blog/item/6f30d7da80a893ddb7fd483a.html
    这有一个介绍DEBUG和RELEASE的文章LZ看下吧.
      

  4.   

    如果我没理解错的的话,lz的dll不放在Exe的路径中,但是想在程序中引用。
    可以处理domain的AssemblyResolve事件,这个事件在net不能load引用的assembly时调用。
    To tell the .NET runtime where to look for an assembly, follow these steps:1. After adding the reference to the assembly within Visual Studio, set the "Copy Local" property of the Reference to False. This prevents the assembly DLL from being copied to the application’s bin directory. 
    2. In the Form’s load event, add an event handler for the AssemblyResolve event for the current AppDomain. (It’s easy and requires two lines of code — have a look at the code below.) 
    3. Create an AssemblyResolve event handler. You may use the implementation as-is from the source code below. private void Form1_Load(object sender, EventArgs e)
            {
                AppDomain currentDomain = AppDomain.CurrentDomain;
                currentDomain.AssemblyResolve += new ResolveEventHandler(currentDomain_AssemblyResolve);        }        Assembly currentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
            {
                //This handler is called only when the common language runtime tries to bind to the assembly and fails.

                //Retrieve the list of referenced assemblies in an array of AssemblyName.
                Assembly MyAssembly, objExecutingAssemblies;
                string strTempAssmbPath = "";

                objExecutingAssemblies = Assembly.GetExecutingAssembly();
                AssemblyName[] arrReferencedAssmbNames = objExecutingAssemblies.GetReferencedAssemblies();

                //Loop through the array of referenced assembly names.
                foreach (AssemblyName strAssmbName in arrReferencedAssmbNames)
                {
                    //Check for the assembly names that have raised the "AssemblyResolve" event.
                    if (strAssmbName.FullName.Substring(0, strAssmbName.FullName.IndexOf(",")) == args.Name.Substring(0, args.Name.IndexOf(",")))
                    {
                        //Build the path of the assembly from where it has to be loaded.
                        //The following line is probably the only line of code in this method you may need to modify:
                        strTempAssmbPath = "Lib";  //assembly在exe执行目录的子目录Lib中
                        if (!strTempAssmbPath.EndsWith("\\")) strTempAssmbPath += "\\";
                        strTempAssmbPath += args.Name.Substring(0, args.Name.IndexOf(",")) + ".dll";
                        break;
                    }

                }
                //Load the assembly from the specified path.
                MyAssembly = Assembly.LoadFrom(strTempAssmbPath);

                //Return the loaded assembly.
                return MyAssembly;
            }        private void button1_Click(object sender, EventArgs e)
            {
                int res = Library.Class1.Add(2, 3);  //调用assembly中的代码
                MessageBox.Show(res.ToString());
            }
    我已经测试过了,没有问题。
      

  5.   

    4L,非常感谢您的答复,我要的就是你的方案,但是我还有一点不明白的:
    private void button1_Click(object sender, EventArgs e)
            {
                int res = Library.Class1.Add(2, 3);  //调用assembly中的代码
                MessageBox.Show(res.ToString());
            }你所指的Library是不是我加载到当前程序集中需要应用的类?
    那这样的类怎么进行实例化?
      

  6.   

    Assembly   asm   =   AppDomain.CurrentDomain.Load("mydll");     
        
      Type   tp   =   asm.GeType("MyType");   
      object   o   =   Activator.CreateInstance(tp);       //创建类实例   
        
      MethodInfo   mi   =   tp.GetMethod(...);       //取的方法描述   
      mi.Invoke(o,   ...);   //调用的对象,以及参数   
      

  7.   


                int res = Library.Class1.Add(2, 3);  //调用assembly中的代码 
                MessageBox.Show(res.ToString()); 我看不明白这个是程序集加载后使用,既然程序集是动态加载的,那么对一个类的实例化应该怎样进行呢?
      

  8.   


    我测试用的assembly是个很简单的dll,Add是static方法因此不需要实例化。将Assembly Load进去后,你可以随意使用Assembly中的类,当然也可以实例化了。如果Add不是static的话,我就需要先Library.Class1 ins = new Library.Class1(); ins.Add(2,3)了。namespace Library
    {
        public class Class1
        {
            public static int Add(int a, int b)
            {
                return a + b;
            }
        }
    }
      

  9.   


    Assembly加载后,你对Assembly中的东西可以任意使用。就好像正常使用reference一样。