Csharp/C Sharp/Development Class/DLL

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Build with the following command-line switches: csc /t:library DllTestServer.cs

<source lang="csharp"> public class DllTestServer {

   public static void Foo()
   {
       System.Console.WriteLine("DllTestServer.Foo (DllTestServer.DLL)");
   }

}

</source>


Compile cs file to DLL

<source lang="csharp"> // Math.cs

  public class Math
  {
     ///<summary>
     ///   The Add method allows us to add two integers
     ///</summary>
     ///<returns>Result of the addition (int)</returns>
     ///<param name="x">First number to add</param>
     ///<param name="y">Second number to add</param>
     public int Add(int x, int y)
     {
        return x + y;
     }
  }

//csc /t:library /doc:Math.xml Math.cs

</source>


Create DLL

<source lang="csharp"> // C:\> csc /target:module Test.cs

namespace CSharpVBInterface {

     using System;
   
     public class Test {
      public Test() {} 
   
      public int DaysInMonth( string monName )
      {
        if ( monName == "September" || monName == "April" || monName == "June" || monName == "November" )
           return 30;
   
        if ( monName == "February" )
           return 28;
        return 31;
      }
      public string DayName( int dayNo ) {
        switch ( dayNo ) {
           case 0:
            return "Sunday";
           case 1:
            return "Monday";
           case 2:
            return "Tuesday";
           case 3:
            return "Wednesday";
           case 4:
            return "Thursday";
           case 5:
            return "Friday";
           case 6:
            return "Saturday";
        }
        return "Unknown";
     }
  }

}

      </source>


Create DLL library

<source lang="csharp"> // DllTestServer.cs // build with the following command line switches // csc /t:library DllTestServer.cs public class DllTestServer {

public static void Foo()
{
 System.Console.WriteLine("DllTestServer.Foo (DllTestServer.DLL)");
}

} ////////////////////////////////////////////////////////// // DllTestClient.cs // build with the following command line switches // csc DllTestClientApp.cs /r:DllTestServer.dll using System; using System.Diagnostics; using System.Reflection; class DllTestClientApp {

   public static void Main()
   {
       Assembly DLLAssembly = Assembly.GetAssembly(typeof(DllTestServer));
       Console.WriteLine("\nDllTestServer.dll Assembly Information"); 
       Console.WriteLine("\t" + DLLAssembly);
       Process p = Process.GetCurrentProcess();
       string AssemblyName = p.ProcessName + ".exe";
       Assembly ThisAssembly = Assembly.LoadFrom(AssemblyName);
       Console.WriteLine("DllTestClient.exe Assembly Information"); 
       Console.WriteLine("\t" + ThisAssembly + "\n");
 Console.WriteLine("Calling DllTestServer.Foo...");
 DllTestServer.Foo();
   }

}

      </source>


Load system dll library

<source lang="csharp"> using System; using System.Reflection; class Class1 {

  static void Main(string[] args) {
     string windir = Environment.GetEnvironmentVariable("windir");
     Assembly ass = Assembly.LoadFrom(windir + @"\Microsoft.NET\Framework\v1.1.4322\System.Drawing.dll");
     foreach (Type type in ass.GetTypes()) {
        Console.WriteLine(type.ToString());
     }
  }

}

      </source>