2016-11-18 109 views
7

我想引用<see cref="..." />XML documentation標記中的運算符,但似乎無法找到有關如何執行此操作的提示。該標籤上的MSDN article僅顯示引用方法的簡單示例,但不涉及可引用的不同類型的成員。XML文檔中的參考運算符

特別是,我想引用一個隱式轉換運算符,但引用運算符的一般規則也將被讚賞。


比方說,我們有一個簡單的結構,我們將其定義==!=和隱式轉換操作符:

public struct MyStructure 
{ 
    public int Value { get; set; } 

    public static bool operator ==(MyStructure x, MyStructure y) => x.Value == y.Value; 

    public static bool operator !=(MyStructure x, MyStructure y) => x.Value != y.Value; 

    public static implicit operator MyStructure(int i) => new MyStructure { Value = i }; 
} 

足夠簡單可以用<see cref="MyStructure.Value" />引用Value財產,但如何去引用==運算符?我顯然試圖<see cref="MyStructure.==" /><see cref="MyStructure.==(MyStructure, MyStructure)" />,但我不認爲這工作,因爲它應該,因爲這兩種意見:

  1. 經營者不得在展示,而不是其他成員的摘要提示着色的着色時,正確引用
  2. 轉到定義命令不起作用,而它對於其他正確引用成員

我還懷疑工具,如Sandcastle用來生成HTML 頁面基於XML文檔也不會生成有效的超鏈接,但仍有待確認。

編輯

我只是證實沙堡不會產生有效的超鏈接我的任何企圖。此外,當進行檢查,以產生在項目屬性的XML 文檔的選項,顯示的代碼CS1584警告說:「XML註釋中有語法不正確CREF屬性‘MYSTRUCTURE。==’」。


理由

如果有人想知道爲什麼我想引用一個運營商的回答是我寫一個單元測試方法上的操作,並作爲一般規則,我把引用測試進行測試測試方法中XML文檔中的成員。所以我後面是這樣的:

/// <summary> 
/// This method performs tests regarding <see cref="..." /> operator 
/// </summary> 
[TestMethod] 
public void ImplicitConversionOperator() { ... } 
+0

從[這裏](https://msdn.microsoft.com/en-us/library/fsbx0t7x.aspx),它看起來像你可以使用:''。我試了一下,它編譯了W/O警告......並將其寫入XML。 IDE行爲仍然不完整。 – Clay

+0

@Clay你是對的警告消失了,但不幸的是其他問題仍未解決。看起來,把「M:」放在前面(或任何其他字母后跟冒號)可以讓你在沒有警告的情況下放置幾乎任何東西 - 例如''不會引發警告。奇怪的是,兩個字母和冒號不會飛... – Grx70

+0

是的 - 我正在玩它。似乎可能是'M'。 – Clay

回答

3

我在VS 2015企業...不知道'回合其他版本。看起來,如果您記錄您的操作,你會得到完全的正確行爲:

/// <summary>The name sez it all</summary> 
public struct MyStruct 
{ 
    /// <summary>implicit</summary> 
    /// <param name="i">an int</param> 
    public static implicit operator MyStruct(int i) 
    { 
    return new MyStruct(); 
    } 
    /// <summary>Thus and so</summary> 
    public static bool operator ==(MyStruct a, MyStruct b) 
    { 
    return false; 
    } 

    /// <summary>Thus and so</summary> 
    public static bool operator !=(MyStruct a, MyStruct b) 
    { 
    return true; 
    } 

    /// <summary>Thus and so</summary> 
    public override bool Equals(object obj) 
    { 
    return base.Equals(obj); 
    } 

    /// <summary>Thus and so</summary> 
    public override int GetHashCode() 
    { 
    return base.GetHashCode(); 
    } 

    /// <summary>Thus and so</summary> 
    public override string ToString() 
    { 
    return base.ToString(); 
    } 
} 

然後,引用,這個工程並與所有的IDE功能亮起(但它不會在顯示會員下拉):

/// <summary> 
/// See <see cref="MyStruct.operator=="/> 
/// </summary> 
[StructLayout(LayoutKind.Sequential)] 
internal struct BY_HANDLE_FILE_INFORMATION 
{ 
    //... 
} 

轉到功能也適用於此處。

編輯:

隱式操作是:

<see cref="MyStruct.op_Implicit(int)" 
+0

好,趕上,確實工作!對轉換運算符有任何想法嗎? – Grx70

+0

@ Grx70,我編輯了答案......剛纔查了一下XML輸出在會員身上:-) – Clay

+0

根據你的回答,我已經玩了一段時間,並發表了我的發現作爲替代答案。 – Grx70

2

要@ Clay的回答闡述 - 參照運營商的<see (...)/>XML文檔中有兩種方式(即我所知道的)標記:

1.使用生成的方法名稱

請參閱this question以供參考。

等於運算符bool operator ==(MyStructure x, MyStructure y)引用是

<see cref="MyStructure.op_Equality(MyStructure, MyStructure)" /> 

對於隱式類型轉換操作符implicit operator MyStructure(int i)

<see cref="MyStructure.op_Implicit(int)" /> 

然而,有這種方法的缺點(據我可以告訴) 。對於轉換運算符,方法名稱分別爲op_Implicitop_Explicit,分別爲implicitexplicit運算符。這些方法的幾個重載可能只有返回類型纔有可能不同。例如,對於這兩個運營商:

public static implicit operator int(MyStructure s) => s.Value; 
public static implicit operator double(MyStructure s) => s.Value; 

這些方法會產生:

int op_Implicit(MyStructure) 
double op_Implicit(MyStructure) 

那麼這個參考:

<see cref="MyStructure.op_Implicit(MyStructure)" /> 

將是含糊的,它會退回到哪個運營商首先被定義。你也會得到一個警告。

2。使用C#運營商名稱

等於運算符bool operator ==(MyStructure x, MyStructure y)引用是

<see cref="MyStructure.operator ==(MyStructure, MyStructure)" /> 

和隱式轉換操作符implicit operator MyStructure(int i)

<see cref="MyStructure.implicit operator MyStructure(int)" /> 

顯然,在消除歧義前面提到的例子沒有問題:

<see cref="MyStructure.implicit operator int(MyStructure)" /> 
<see cref="MyStructure.implicit operator double(MyStructure)" /> 

其他考慮

我注意到另外一個區別是,第二種方法是通過適當CodeLens認可,而第一個是不是。