Csharp/C Sharp/Development Class/DLL

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

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)");
    }
}


Compile cs file to DLL

 
// 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


Create DLL

// 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";
      }
   }
}


Create DLL library

// 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();
    }
}


Load system dll library

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());
      }
   }
}