在我的自定義頂級System命名空間(我也定義了 struct Int32)中,我無法訪問 dotnet 的標準System.Int32型別。我自己Systme.Int32正在隱藏 dotnet 的System.Int32. 我曾嘗試使用global::完全限定System.Int32名稱來獲得 dotnet 的標準型別,但它仍然一直參考我自己的型別。我找不到解決方法。在此處閱讀 Eric Lippert 對相關問題的 11 歲回答https://stackoverflow.com/a/3830520/423632表明我需要為此撰寫自己的編譯器。但應該不是不可能做到的。
以下代碼無法編譯,給出了參考Main 方法中第一條陳述句的錯誤:
“錯誤 CS0029:無法將型別 'int' 隱式轉換為 'System.Int32' ”
// DOES NOT COMPILE !
using System;
namespace System
{
struct Int32
{
public override string ToString()
{
return "My own Int32 lol";
}
}
class Program
{
static int Main(string[] args)
{
global::System.Int32 x = 5; // error CS0029
Console.WriteLine(x);
return 0;
}
}
}
uj5u.com熱心網友回復:
我發現的一種方法是使用 extern 別名:
extern alias MSCorLib;
namespace System
{
struct Int32
{
public override string ToString()
{
return "My own Int32 lol";
}
}
class Program
{
static int Main(string[] args)
{
MSCorLib::System.Int32 x = 5;
Console.WriteLine(x);
return 0;
}
}
}
如果您使用以下-reference選項編譯它:
csc Program.cs -reference:MSCorLib=mscorlib.dll
然后它將按預期列印 5。
對于Int32具體情況,你也可以只使用int:
int x = 5;
當然,這也適用于double,float,short,等。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/350332.html
上一篇:SSH到動態遠程服務器并運行命令-Laravel5.8
下一篇:c#方法覆寫。如何正確呼叫
