[英]Could not load file or assembly C# Console Application with Class Library
[英]In C# => Executing Class Library in Console Application
我希望創建一個菜單驅動的程序,該程序將根據某些方面(例如操作員或其他任何事物)進行分類。
我創建了一個控制台應用程序項目,並給了“示例”作為名稱空間。 在示例中,我在Main()中創建了一個菜單
public static void Main()
{
Console.Clear();
Console.WriteLine("\nMenu Driven Program");
Console.WriteLine("\n<--------List of Available Options-------->");
//Creating a list to Select from
Console.WriteLine("\n Select Any of the Following Function :");
Console.WriteLine("\n_________________________________________");
Console.WriteLine("\n1. Arithmatic Operators\n2. Logical Operators");
Console.WriteLine("\n3.Misc1\n4. Misc2");
//Accepting a normal string
string b = Console.ReadLine();
int d;
// Following converts string into an integer
Int32.TryParse(b, out d);
//Following code is used to create menu-driven program
switch (d)
{
case 1:
//Here I wish to call one class library
break;
case 2:
//Here I wish to call another class library
break;
//And so on.
default:
Console.WriteLine("\n\nWhat you wish to do?\na. Continue the Program\n\nb. Exit the Program");
char c = Convert.ToChar(Console.ReadLine());
if(c=='a'||c=='A')
{
Main();
}
else
{
Exit();
}
break;
}
}
現在,我創建了一個類庫,其中將存儲三個類(讓我們考慮一下)增量,除法和乘法。
但是,我無法調用該類庫。 當我在Switch“ Case”中直接輸入類庫的名稱時,它給我CS0118錯誤,指出“ Sample”只是一個名稱空間,但像變量一樣使用。
在我的類庫中,我創建了三個不同的類,並再次創建了Main(),它將顯示可用操作的列表。 例如,如果是算術運算符,則Main in class庫將包括加法,減法,乘法等。
您需要添加對類庫項目的引用。 我假設您在相同的解決方案中創建了它們。 確保兩個總線都相同。 網絡版本說4.6。 要添加參考,請展開項目資源管理器,查找參考,右鍵單擊,然后選擇“添加”,然后找到類庫並選中復選框。 如果您安裝了生產力工具Resharper,則可以更快地修復這些問題。
CS0118錯誤,指出“樣本”只是一個名稱空間,但像變量一樣使用
當您嘗試創建名稱空間的實例或以其他方式將名稱空間用作類的實例時,會發生錯誤CS0118。 有關更多信息,請參見本文檔 。
根據您的描述,我的假設是,您需要根據用戶選擇的內容,在3個類中的每個類中調用主類。 考慮以下代碼...
switch(d)
{
case 1:
Sample.Class1.Main();
break;
case 2:
Sample.Class2.Main();
break;
case 3:
Sample.Class3.Main();
break;
default:
// default processing here
}
假設您的命名空間稱為Sample,並且方法在庫的類中定義為靜態。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.