我有下面的類的類:不能被實例化
public class Person
{
public String Name { get; set; }
internal Person()
{
this.Name = "Ian";
}
public static Person Initialize()
{
return new Person();
}
}
因爲構造是內部的,編譯器將拋出一個編譯錯誤「型人沒有定義的構造」,當外部組件試圖實例化Person
。這是預期的行爲,因爲我希望用戶實例化類直通靜態Initialize
方法,像這樣:
Person p = Person.Initialize();
但是,智能感知仍呈現Person
作爲實例化類(它列鍵入new
關鍵字後)。
有沒有辦法在智能感知中「隱藏」構造函數,因爲它具有誤導性?或者我的設計出了問題?
編輯: 問題是如何「隱藏」在IntelliSense構造函數,因爲它是誤導。
打開Visual Studio並鍵入以下內容:
System.IO.TextReader a = new
正如你所看到的,所以存在被聚焦智能感知沒有TextReader
。現在嘗試Person
和Person
「構造函數」將自動突出顯示。
我想要的是TextReader智能感知「行爲」。
嘗試使構造'private',不'internal' – Rob 2011-03-23 10:43:40
快速的猜測是對'private'關鍵字明確地添加到您的'Person'方法。但爲什麼不直接調用構造函數很重要? – Belinda 2011-03-23 10:45:36
內部意味着它將在同一個'namespace'中可見。你確定使用'Person'的類不在同一個命名空間嗎?否則,請參閱@ Rob的評論。 – Kevin 2011-03-23 10:47:45