强制应用程序与可选的DLL运行可选、应用程序、DLL

2023-09-06 11:18:01 作者:没人替你坚强

我已经做桌面应用程序。我有做类库,然后进行自组装大学的DLL。现在,我想使库DLL可选。总之我想要运行的应用程序的天气与否库DLL被吹罚。

I have made desktop application. I have make class library and then make its DLL from University assembly. Now i want to make library DLL optional. In short i want to run the application weather or not library DLL is refereed.

现在,如果我删除库DLL的引用则给出了库方法错误没有定义他们。我想这个应用程序与oujt给人库方法错误运行。

Right now if i remove reference of library DLL then it gives error on library methods that they are not defined. I want this application to run with oujt giving error of library method.

我有搜索谷歌,但我无法找到任何可靠的答案。

I have search on google but i am unable to find out any reliable answer.

推荐答案

检查装配存在于磁盘上,如果这是真的使用动态组件加载:

Check if assembly exists on disk, and if it's true use dynamic assembly loading:

http://msdn.microsoft.com/en-us/library/ 25y1ya39.aspx

调用的类/库中的方法可以通过存根(抽象新的水平),在其中您可以检查是否成功加载的程序集,如果是从中调用替换。

Called classes/methods in your library can be replaced by stubs(new level of abstraction), in which you can check if assembly is successfully loaded, and invoke from it if yes.

确定..很简单的例子:

Ok.. Very simple example:

真实大会code(第一个项目,编译为类库RealAssembly.dll):

"Real Assembly" code(First project, compiled as class library "RealAssembly.dll"):

namespace RealAssembly
{
    using System;
    public class RealClass
    {
        Random rand = new Random();

        public int SomeProperty { get { return rand.Next(); } }

        public string SomeMethod()
        {
            return "We used real library! Meow!";
        }
    }
}

我们的项目code与假(存根)类(第二个项目,编译为控制台应用程序了 - ClientApp.exe):

"Our project" code with Fake(stub) class(Second project, compiled as Console applicaiton - "ClientApp.exe"):

using System;
using System.IO;
using System.Reflection;

namespace ClientApp
{
    class FakeClass
    {
        public int SomeProperty { get { return 0; } }

        public string SomeMethod()
        {
            return "Library not exists, so we used stub! :)";
        }
    }

    class Program
    {
        // dynamic instance of Real or Fake class
        private static dynamic RealOfFakeObject;

        static void Main(string[] args)
        {
            TryLoadAssembly();
            Console.WriteLine(RealOfFakeObject.SomeMethod());
            Console.WriteLine(RealOfFakeObject.SomeProperty);
            Console.WriteLine("Press any key...");
            Console.ReadKey();
        }

        private static void TryLoadAssembly()
        {
            string assemblyFullName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "RealAssembly.dll");

            if (File.Exists(assemblyFullName))
            {
                var RealAssembly = Assembly.LoadFrom(assemblyFullName);
                var RealClassType = RealAssembly.GetType("RealAssembly.RealClass");
                RealOfFakeObject = Activator.CreateInstance(RealClassType);
            }
            else
            {
                RealOfFakeObject = new FakeClass();
            }
        }
    }
}

这两个项目没有直接引用。 系统是在这两个项目中使用的唯一的参考。

This two projects are not referenced directly. "System" is the only reference used in this two projects.

所以,现在,如果编译RealAssembly.dll在同一个目录下存在,我们将有我们用真正的图书馆!喵!串并在控制台输出随机整数。否则,如果RealAssembly.dll不存在于同一个目录 - !图书馆不存在的,所以我们用存根:)0将显示

So now, if compiled "RealAssembly.dll" exists in same directory we will have "We used real library! Meow!" string and random integer at console output. Otherwise if "RealAssembly.dll" not exists in same directory - "Library not exists, so we used stub! :)" and 0 will be shown.