2012-02-23 50 views
5

我想在我的類XML文檔中包含代碼片段,但編譯器抱怨xml元素沒有關閉! 這裏就是我想實現XML文檔中的泛型問題

/// <summary> 
/// Method documentation here... 
/// </summary> 
/// <remarks> 
/// <para> 
/// This class should be used as follow: 
/// <br/> 
/// ************** PROBLEM IN NEXT LINE ******************** 
/// <c> MyClass class = new MyClass<String>(); </c> 
/// </para> 
/// </remarks> 
public class MyClass<T>{ 
.... 
} 

我試圖通過/// <c> MyClass class = new MyClass{String}(); </c>

任何一個已經經歷過這個更換的代碼片段?

感謝您的幫助

+0

這是問及http://stackoverflow.com/questions/532166/c-how-to-reference-generic-回答class-and-methods-in-xml-documentation – kaj 2012-02-23 12:21:30

+0

@KAJ由於OP更新並更正了複製/粘貼錯誤,我同意並投票結束重複。 – Filburt 2012-02-23 12:30:30

回答

7

在XML文檔中,你有花括號來代替三角括號:

/// <summary> 
/// Calls <see cref="DoSomething{T}"/>. 
/// </summary> 
public void CallsDoSomething() 
{ 

} 

public void DoSomething<T>() 
{ 

} 

你最終被迫之所以這樣做,是因爲它真正的ISN」如果你允許在元素標記之外的三角形括號,則很好地形成了xml。

您嘗試的替換是正確的。

+3

或<和> – demoncodemonkey 2012-02-23 12:20:52

+1

是的 - 你也可以這樣做 - 雖然它使得代碼文檔本身的可讀性降低了恕我直言。 – 2012-02-23 12:23:23

2

您的<remarks>從未關閉。

更換已經嘗試過的角撐圈也是需要的。

+0

對不起,這是一個複製/粘貼問題。剛剛添加結束標記的評論。 – GETah 2012-02-23 12:24:13

+0

在這種情況下,KAJ是正確的,使用大括號可以解決您的問題。 – Filburt 2012-02-23 12:29:29

5

您沒有在第4行關閉Remarks元素,它可能是抱怨,只是在錯誤的行號。

另外,通過包含泛型的示例,它將拾取List<string>作爲文本字面量List後跟未封閉的string XML元素。最簡單的解決方法是執行List &amp;lt;string&amp;gr;,解析時產生List<string>而不是XML元素。

C#編譯器團隊的補充{}作爲替代的是,這樣你就可以做List{string},它會被加工成<>的。

+0

對不起,這是一個複製/粘貼問題。剛剛添加結束標記的評論。 – GETah 2012-02-23 12:23:57

3

幾件事情:

  1. &lt;&gt;替換它們讓您遠離<>字符。
  2. </remarks>
  3. 閉上你的XML <remarks>節當你決定引用一個通用的標籤(即<see ... /><seealso ... />等),那麼你會做這樣類似如下:<see cref="SomeMethod{T}(T value)" />。永遠不要在參考中指定具體的類型(也就是不要做<see cref="SomeMethod{String}(String value)" />)。

這是你的XML註釋的一個固定的版本:

/// <summary> 
/// Method documentation here... 
/// </summary> 
/// <remarks> 
/// <note type="implementsinfo"> 
///  <para>This class should be used as follow:</para> 
///  <para><c>MyClass class = new MyClass&lt;string&lt;();</c></para> 
/// </note> 
/// </remarks> 
public class MyClass<T> 
{ 
    .... 
} 
+0

感謝您的快速響應。我會用{}代替<和< – GETah 2012-02-23 12:34:31