2016-07-23 65 views
0

我正在使用Visual Studio 2013自動生成的Web API應用程序的XML文檔,並且它運行良好,除了具有從列表<>繼承的業務對象的屬性外;我從他們那裏得到的是「對象集合」。VS爲WebAPI生成的幫助頁面,屬性文檔

作爲一個例子,這裏是一個包含OrderLineCollection財產Order對象:

public class Order 
{ 
    public OrderLineCollection Lines { get; set; } 
} 

public class OrderLine 
{ 
    public string OrderNo { get; set; } 
} 

public class OrderLineCollection : List<OrderLine> 
{ 
    public void ReadFromServer(string orderNo) 
    {} 
} 

的文檔在Lines屬性類型列訂單對象只具有「對象的集合」產生的,與沒有鏈接到OrderLine對象。

如果我不是定義行這樣的屬性,它的工作原理(=我得到的「訂單行集」在類型列,與訂單行超鏈接):

public class Order 
{ 
    public List<OrderLine> Lines { get; set; } 
} 

不過,我想能夠使用上面的OrderLineCollection類,以便我可以在那裏保留我的集合特定的邏輯。我只需要XML文檔爲Lines屬性指定「CollectionLineLine(超鏈接)」。

有沒有簡單的方法來實現這一點?

回答

0

在您的Web API項目,打開該負責生成幫助頁面模型類型名稱類的代碼文件:

{} MyProject的/地區/ HelpPage/ModelDescriptions/ModelDescriptionGenerator.cs

轉到方法:

public ModelDescription GetOrCreateModelDescription(Type modelType) 

然後去下面的代碼

if (modelType.IsEnum) 
{ 
    return GenerateEnumTypeModelDescription(modelType); 
} 

,然後後馬上輸入以下代碼:在方法內部

// Force Web API to generate help page model type names like "Collection of {MyType}" instead of "Collection of Object". 
if (!modelType.IsGenericType        // Model type must not be a generic type. 
    && modelType.BaseType != null       // Model type must have a base type (i.e. the model type is a derived type). 
    && modelType.BaseType.IsGenericType      // Model type base type must be a generic type. 
    && typeof(IList).IsAssignableFrom(modelType.BaseType)) // Model type base type must implement the IList interface (you can replace "IList" with "IEnumerable" to handle more general collection types). 
{ 
    // Set the model type variable to the model type base type and the rest of the method will know what to do. 
    modelType = modelType.BaseType; 

} 

我希望這有助於!