建立动态连接库(.dll)

   摸索了好久,终于把这个问题搞定,现在将自己建立一个dll的详细步骤列举出来.

   1 建立自定一的.dll
      "新建一个C#项目",选择项目类型为"类库", 源代码如下:
           using System;
            using System.Collections.Generic;
            using System.Text;

      namespace Age
   {
       public class person
       {
           private string name;
           private DateTime birthday;
           public person(string n, DateTime t)
           {
               this.name = n;
               this.birthday = t;
           }
           public int GetAge()
           {
               return DateTime.Now.Year - this.birthday.Year;
           }
           public void displayInfo()
           {
               Console.WriteLine("姓名 {0}, 年龄 {1}", this.name, this.GetAge());
           }
       }
   }

  //名字空间Age中有一个person 类,注意,类库是没有Main()函数的,也不能独立运行.

   2  接下来是把 .cs 文件编译成 .dll 文件
      在命令行下将起始路径设置为程序 "csc.exe" 所在目录
  运行如下命令:
  csc /target:library /out:F:\a_dll.dll /recurse:F:\class1.cs
      其中library是指编译为类库类型,out表示输出,这里是指把编译成的dll文件命名为a_dll.dll并把它放在分区F   
     根目录下, recurse表示源代码文件.cs的路径.

  *到此,dll文件建立完毕,接下来是对文件a_dll.dll的引用和使用.

   3  新建一个控制台项目
       在窗体左边"引用"右击,选择"添加引用",并在"浏览"选项卡下,找到F:a_dll.dll,把此文件添加进去.
      此后,在代码的 using 语句中,要知道原 class1.cs 文件中命名空间的名字,这里为"Age",所以语句为
      using Age;

     代码如下:
      using System;
      using System.Collections.Generic;
      using System.Text;
      using Age;

      namespace getAge
      {
          class Program
          {
              static void Main(string[] args)
              {
                  string s = "Zhang";
                  DateTime t = new DateTime(1974, 12, 8);
                  person p = new person(s, t);
                  p.displayInfo();
              }
          }
      }

      在使用了using Age语句后,代码中输入person字样时,会自动变为蓝色,说明来自Age中的person类已经可以使用了,调试该程序.

   4 有关csc命令的格式和用法,详细见<<csc 命令的使用>>.
补充:
csc /target:library /out:F:\a_dll.dll /recurse:F:\class1.cs
可以简写为: csc /t:library /out:F:\a_dll.dll F:\class1.cs
此外,作为类库原.cs代码文件,其中的名字空间中的类必须都加public 修饰符,否则是私有类型,在引用类库的新工程中不能被应用.

   

转载于:https://www.cnblogs.com/YuYing/archive/2007/03/17/678472.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值