Can a C# .dll assembly contain an entry point?

后端 未结 3 1520
野趣味
野趣味 2021-02-01 20:41

My goal is to create an executable that will start a shadow copied application. The trick is, I want this starter program to have no external dependencies and not have to conta

3条回答
  •  青春惊慌失措
    2021-02-01 20:52

    I found the advice not so easy to follow. After some experimenting this is how I came to success:

    I created a console application with a simple main and included the rest of the code from my original DLL. Below is a simplified program which includes a DLL:

    namespace FIT.DLLTest
    {
      public class DLLTest
      {
        [STAThread]
        static void Main(string[] args)
        {
          int a = 1;
        }
    
        public DLLTest()
        {
          int b = 17;
        }
    
        public int Add(int int1, int int2)
        {
          return int1 + int2;
        }
      }
    }
    

    After compilation I renamed the generated .exe to a .DLL.

    In the mother program, which uses the DLL I first added the DLLTest.dll as a Reference, then I added the code to execute the DLL.

    namespace TestDLLTest
    {
      class TestDLLTest
      {
        static void Main(string[] args)
        {
          AppDomain domain = AppDomain.CreateDomain( "DLLTest" );
          domain.ExecuteAssembly( "DllTest.dll" );
    
          DLLTest dt = new DLLTest();
          int res2 = dt.Add(6, 8);
          int a = 1;
        }
      }
    }
    

    Violà I could execute and add a breakpoint in the DLLTest.Main method and see that I could invoke the Main of DLLTest. Thanks for the discussion folks!

提交回复
热议问题