歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> C# 跨程序集調用常量、變量和函數

C# 跨程序集調用常量、變量和函數

日期:2017/3/1 9:31:02   编辑:Linux編程

今天在給人講C#程序集概念的時候突然被問住了,囧啊。所以又認真地看了下C#程序集的功能,本文就是關於C#跨程序集調用其他程序集的常量、變量和函數。

步驟1:建立兩個程序集,分別是C#下的“Windows窗體應用程序”和“類庫”,分別取名為“AAAAA”和“BBBBB”。程序集“BBBBB”中的類被更名為MyClass

步驟2:修改程序集“BBBBB”,文件MyClass.cs中的代碼如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BBBBB
{
public class MyClass
{
/// <summary>
/// 常量
/// </summary>
public const string DateInfo1 = "2015/1/5";

/// <summary>
/// 靜態變量
/// </summary>
public static string DateInfo2 = "2015/1/5";

/// <summary>
/// 方法("打印Hello World!")
/// </summary>
public static void SayHi()
{
Console.WriteLine("Hello World!");
}
}
}

裡面有一個常量DateInfo1、一個靜態變量DateInfo2、一個向控制台打印字符串“Hello World!”的方法

步驟3:在程序集“AAAAA”的引用管理器中添加對程序集“BBBBB”的引用。程序集“AAAAA”的引用管理器可以在解決方案資源管理器中右鍵單擊程序集“AAAAA”下面的“引用”,單擊“添加引用”按鈕進入。進入程序集“AAAAA”的引用管理器後,在左側的樹形菜單中,找到“解決方案”→“項目”,勾選裡面的“BBBBB”,並按下面的“確定”按鈕保存設置。

步驟4:這下載程序集“AAAAA”下面的代碼中就可以using到程序集“BBBBB”的命名空間了

文件Program.cs中的代碼如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using BBBBB;

namespace AAAAA
{
class Program
{
static void Main(string[] args)
{
//打印常量
Console.WriteLine(BBBBB.MyClass.DateInfo1);

//打印靜態變量
Console.WriteLine(BBBBB.MyClass.DateInfo2);

//調用方法
BBBBB.MyClass.SayHi();

Console.ReadLine();
}
}
}

步驟5:生成解決方案,運行程序,運行結果如下:

其他:兩個Debug目錄下的文件構成

1)程序集“AAAAA”的Debug目錄:(包括程序集“BBBBB”編譯出來的全部信息)

2)程序集“BBBBB”下的Debug目錄:

END

Copyright © Linux教程網 All Rights Reserved