close
新增專案如下, 接著將Class1.cs重新命名為Add.cs
重新命名成Add.cs, Class1會自動更新成Add
Add.cs
命名空間:MyMethods
定義AddClass, 並包含一個靜態函式Add
1: using System;
2: using System.Collections.Generic;
3: using System.Text;
4:
5: namespace MyMethods
6: {
7: public class AddClass
8: {
9: public static long Add(long i, long j)
10: {
11: return (i + j);
12: }
13: }
14: }
加入Mult.cs
選擇<類別>並輸入名稱Mult.cs
Mult.cs
命名空間:MyMethods
定義MultipyClass, 並包含一個靜態函式Multiply
1: using System;
2: using System.Collections.Generic;
3: using System.Text;
4:
5: namespace MyMethods
6: {
7: public class MultiplyClass
8: {
9: public static long Multiply(long x, long y)
10: {
11: return (x * y);
12: }
13: }
14: }
編譯成功後會跳出一個對話框, 不用緊張只是提示: 類別庫是要給其他專案呼叫使用,無法直接執行!
產生動態函式庫 MyLibrary
--------------------------------------------------------------------------------------------------------------------------------------------
撰寫一個Console程式, 加入參考MyLibrary函式庫
選擇<主控台應用程式>
名稱: CallMyLibrary
加入參考
加入參考 C:\CSharp\MyLibrary\MyLibrary\bin\Release\MyLibrary.dll
滑鼠雙擊MyLibrary展開樹狀結構
設定起始專案並編譯
編譯器會自動複製動態函式庫MyLibrary.dll
在cmd命令輸入指令
> CallMyLibrary 2 5
參考資料: Microsoft 建立和使用 C# 的 DLL
範例程式: Download
全站熱搜