2012-01-07 81 views
2

我試圖減少下面的一段代碼:通用接口約束

public interface IQuestion<TSite, TStyling, TRelation> : IShallowPost 
    where TSite : INetworkSite<TStyling, TRelation> 
    where TStyling : INetworkSiteStyling 
    where TRelation : INetworkSiteRelation 
{ 

爲了這樣的事情:

public interface IQuestion<TSite> : IShallowPost 
    where TSite : INetworkSite<TStyling, TRelation> 
    where TStyling : INetworkSiteStyling 
    where TRelation : INetworkSiteRelation 
{ 

我怎樣才能使這項工作?

由於具有這樣的事情:

internal sealed class Question : IQuestion<NetworkSite, NetworkSiteStyling, NetworkSiteRelation> 

相比

internal sealed class Question : IQuestion<NetworkSite> 

更新@Jared

當然讓人很沒有意義對我來說,這將是它。

/// <summary> 
/// Represents a question on one of the Stack Exchange sites. 
/// <para>https://api.stackexchange.com/docs/types/question</para> 
/// </summary> 
/// <typeparam name="TSite">The concrete type of <see cref="INetworkSite{TStyling,TRelation}"/>.</typeparam> 
/// /// <typeparam name="TStyling">The concrete type of <see cref="INetworkSiteStyling"/>.</typeparam> 
/// <typeparam name="TRelation">The concrete type of <see cref="INetworkSiteRelation"/>.</typeparam> 
/// <typeparam name="TMigrationInfo">The concrete type of <see cref="IMigrationInfo{TSite,TStyling,TRelation}"/>.</typeparam> 
/// <typeparam name="TShallowUser">The concrete type of <see cref="IShallowUser"/>.</typeparam> 
/// <typeparam name="TComment">The concrete type of <see cref="IComment{TShallowUser}"/>.</typeparam> 
/// <typeparam name="TAnswer">The concrete type of <see cref="IAnswer{TShallowUser,TComment}"/>.</typeparam> 
public interface IQuestion<TShallowUser, TComment, TMigrationInfo, TSite, TStyling, TRelation, TAnswer> : IShallowPost<TShallowUser, TComment> 
    where TShallowUser : IShallowUser 
    where TComment : IComment<TShallowUser> 
    where TSite : INetworkSite<TStyling, TRelation> 
    where TStyling : INetworkSiteStyling 
    where TRelation : INetworkSiteRelation 
    where TMigrationInfo : IMigrationInfo<TSite, TStyling, TRelation> 
    where TAnswer : IAnswer<TShallowUser, TComment> 
{ 
    /// <summary> 
    /// The id of this question. 
    /// </summary> 
    [JsonProperty("question_id")] 
    long? QuestionId { get; set; } 
    /// <summary> 
    /// The date this question was locked. 
    /// </summary> 
    [JsonConverter(typeof(UnixDateTimeConverter))] 
    [JsonProperty("locked_date")] 
    DateTime? LockedDate { get; set; } 
    /// <summary> 
    /// The date this question was marked as community wiki. 
    /// </summary> 
    [JsonConverter(typeof(UnixDateTimeConverter))] 
    [JsonProperty("community_owned_date")] 
    DateTime? CommunityOwnedDate { get; set; } 
    /// <summary> 
    /// The amount of answers posted in response to this question. 
    /// </summary> 
    [JsonProperty("answer_count")] 
    long? AnswerCount { get; set; } 
    /// <summary> 
    /// The id of the accepted answer, if any. 
    /// </summary> 
    [JsonProperty("accepted_answer_id")] 
    long? AcceptedAnswerId { get; set; } 
    /// <summary> 
    /// Migration info telling where this question was migrated to. 
    /// </summary> 
    [JsonProperty("migrated_to")] 
    TMigrationInfo MigratedTo { get; set; } 
    /// <summary> 
    /// Migration info telling where this question was migrated from. 
    /// </summary> 
    [JsonProperty("migrated_from")] 
    TMigrationInfo MigratedFrom { get; set; } 
    /// <summary> 
    /// Date when the active bounty on this question ends. 
    /// </summary> 
    [JsonConverter(typeof(UnixDateTimeConverter))] 
    [JsonProperty("bounty_closes_date")] 
    DateTime? BountyClosesDate { get; set; } 
    /// <summary> 
    /// The amount of reputation spent on the bounty. 
    /// </summary> 
    [JsonProperty("bounty_amount ")] 
    long? BountyAmount { get; set; } 
    /// <summary> 
    /// The date this question was closed. 
    /// </summary> 
    [JsonConverter(typeof(UnixDateTimeConverter))] 
    [JsonProperty("closed_date")] 
    DateTime? ClosedDate { get; set; } 
    /// <summary> 
    /// The date this question was marked as protected. 
    /// </summary> 
    [JsonConverter(typeof(UnixDateTimeConverter))] 
    [JsonProperty("protected_date")] 
    DateTime? ProtectedDate { get; set; } 
    /// <summary> 
    /// The title of this question. 
    /// </summary> 
    [JsonProperty("title")] 
    string Title { get; set; } 
    /// <summary> 
    /// A list of tags applied on this question. 
    /// </summary> 
    [JsonProperty("tags")] 
    IList<string> Tags { get; set; } 
    /// <summary> 
    /// The reason this post was closed. 
    /// </summary> 
    [JsonProperty("closed_reason")] 
    string ClosedReason { get; set; } 
    /// <summary> 
    /// The amount of users who favorited this question. 
    /// </summary> 
    [JsonProperty("favorite_count")] 
    long? FavoriteCount { get; set; } 
    /// <summary> 
    /// The amount of views this question had. 
    /// </summary> 
    [JsonProperty("view_count")] 
    long? ViewCount { get; set; } 
    /// <summary> 
    /// A list of answers posted on this question. 
    /// </summary> 
    [JsonProperty("answers")] 
    IList<TAnswer> Answers { get; set; } 
    /// <summary> 
    /// A link to the question. 
    /// </summary> 
    [JsonProperty("link")] 
    string Link { get; set; } 
    /// <summary> 
    /// A boolean indicating whether this question is answered or considered unanswered. 
    /// </summary> 
    [JsonProperty("is_answered")] 
    bool? IsAnswered { get; set; } 
} 

由於Json反序列化,我需要具體類型而不是接口,但我真的很討厭必須傳遞那些巨大的泛型類型列表。這很好看。

+0

C# - 它的標記。 – 2012-01-07 15:23:12

+0

你可以提供一些關於IQuestion的更多細節(更多的是界面)? – JaredPar 2012-01-07 15:26:45

+0

@JaredPar更新了實際界面的代碼。 @Cody,對不起,我忘了標記'C#'。 – bevacqua 2012-01-07 15:31:08

回答

3

可以作出這樣的限制更短,如果你使用C#4以上,INetworkSite兩種類型參數聲明爲協變,這將讓你的界面看起來像這樣:

public interface IQuestion<TSite> : IShallowPost 
    where TSite : INetworkSite<INetworkSiteStyling, INetworkSiteRelation> 
{ 
    // interface members 
} 

public interface INetworkSite<out TStyling, out TRelation> 
    // these constraints are not actually needed for the example to work, 
    // but they seemed logical, so I left them in 
    where TStyling : INetworkSiteStyling 
    where TRelation : INetworkSiteRelation 
{ 
    // interface members 
} 

但是,如果INetworkSite需要任何形式的給定類型的輸入(方法參數,參數參數,可設置的屬性),這不起作用,因此迫使你離開約束。

當然,可以只定義一個類型參數作爲協變,只允許那個被排除在約束之外。

+0

+1你擊敗了我;-) – 2012-01-07 15:56:30