zoukankan      html  css  js  c++  java
  • C# DLL(程序集)的生成和调用

    日期:2018年11月24日

    环境:Window 10,VS2015

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

      步骤:

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

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

      

      3.输入csc /t:library /out:C:UsersxxxxxDesktopstudyAcmeCollectionsAcmeCollectionsMyMath.dll C:UsersxxxxxDesktopstudyAcmeCollectionsAcmeCollectionsMyMath.cs;

      注解:

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

        2)/out:C:UsersxxxxxDesktopstudyAcmeCollectionsAcmeCollectionsMyMath.dll:输出文件的位置和名称;

        3)C:UsersxxxxxDesktopstudyAcmeCollectionsAcmeCollectionsMyMath.cs:源文件的位置和名称;

        

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

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

       

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

  • 相关阅读:
    UVA10765图论+点-双连通分量性质应用
    LA4287图论+ 有向图SCC+缩点
    LA5135图论+ 割点性质运用
    LA2572计算几何+离散化+面的覆盖
    LA2402暴力枚举+计算几何+四边形面积
    UVA10566计算几何+相似三角形比例函数+二分范围的辨析
    UVA11300计算几何:正n边形内的最长的线
    UVA11524平面几何+二分法+海伦公式
    LA4986三分法求出凹性函数最小值+计算几何
    胜利大逃亡--hdu --1253(bfs)
  • 原文地址:https://www.cnblogs.com/w54255787/p/10012702.html
Copyright © 2011-2022 走看看