Load an Assembly from Bin in ASP.NET

后端 未结 4 2530
被撕碎了的回忆
被撕碎了的回忆 2021-02-20 10:00

I have a file name, like \"Foo.dll,\" for a library that I know is in the bin directory. I want to create an Assembly object for it. I\'m trying to instantiate this object from

4条回答
  •  终归单人心
    2021-02-20 10:42

    A complete example as I use, if it helps. Resources is a folder under the root of the DLL Library (Assembly)

            public static string ReadAssemblyResourceFile(string resourcefilename)
            {
    using (var stream = Assembly.Load("GM.B2U.DAL").GetManifestResourceStream("GM.B2U.DAL.Resources."
        + resourcefilename))            {
                        if (stream == null) throw new MyExceptionDoNotLog($"GM.B2U.DAL.Resources.{resourcefilename} not found in the Assembly GM.B2U.DAL.dll !");
                        using (var reader = new StreamReader(stream))
                        {
                            return reader.ReadToEnd();
                        }           
                    }
            }
    

    to call the function:

    [TestMethod()]
    public void ReadAssemblyResourceFileTest()
    {
        var res = SetupEngine.ReadAssemblyResourceFile("newdb.sql");
        Assert.IsNotNull(res);
    }
    

    ps. Do not forget to to mark the "Build Action" as "Embedded Resource" (in the properties window) of each resource file.

提交回复
热议问题