经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C# » 查看文章
C# DLL(程序集)的生成和调用
来源:cnblogs  作者:取名字脑壳疼  时间:2018/11/25 20:09:17  对本文有异议

日期:2018年11月24日

环境:Window 10,VS2015

一、利用VS2015自带的工具生成DLL

  步骤:

  1.利用C#准备一个.cs文件;

  1. 1 using System;
  2. 2
  3. 3 public class MyMath
  4. 4 {
  5. 5 public MyMath()
  6. 6 {
  7. 7 Console.WriteLine("This is DLL!!!");
  8. 8 }
  9. 9 public long Add(long a,long b)
  10. 10 {
  11. 11 return (a + b);
  12. 12 }
  13. 13 }
  1.   2.开始菜单->Visual Studio 2015->VS2015 开发人员命令提示;

  

  3.输入csc /t:library /out:C:\Users\xxxxx\Desktop\study\AcmeCollections\AcmeCollections\MyMath.dll C:\Users\xxxxx\Desktop\study\AcmeCollections\AcmeCollections\MyMath.cs;

  注解:

    1)library:意思是编译成类库,否则必须有Main();

    2)/out:C:\Users\xxxxx\Desktop\study\AcmeCollections\AcmeCollections\MyMath.dll:输出文件的位置和名称;

    3)C:\Users\xxxxx\Desktop\study\AcmeCollections\AcmeCollections\MyMath.cs:源文件的位置和名称;

    

    上图没有出现报错,即操作成功;

二、给你的项目添加引用此DLL,并运行;

   

  1. 1 using System;
  2. 2 using System.Collections.Generic;
  3. 3 using System.Linq;
  4. 4 using System.Text;
  5. 5 using System.Threading.Tasks;
  6. 6 using System.Runtime.InteropServices;
  7. 7
  8. 8 namespace AcmeCollections
  9. 9 {
  10. 10 class Program
  11. 11 {
  12. 12 static void Main(string[] args)
  13. 13 {
  14. 14 long ans;
  15. 15 string str;
  16. 16 MyMath myMath = new MyMath();
  17. 17 ans = myMath.Add(1, 2);
  18. 18 str = Convert.ToString(ans);
  19. 19 Console.WriteLine(str);
  20. 20 Console.ReadLine();
  21. 21 }
  22. 22 }
  23. 23 }
Main Code

  运行结果:

    

三、参考链接

  1.https://www.cnblogs.com/zuoguanglin/archive/2012/02/23/2364613.html

  2.https://docs.microsoft.com/zh-cn/dotnet/csharp/tour-of-csharp/program-structure

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号