2017-01-27 83 views
1

Visual Studio代碼分析報告一兩個這種構造函數的類聯接:類耦合

protected AcceptInvitation() 
{ 
} 

有人可以解釋,爲什麼?首先,我認爲這是因爲其他類的依賴。但我使用「查找所有用法」,但沒有任何報告。

那麼我該如何弄清爲什麼Visual Studio會爲它報告「2」?

後來我有另一個構造函數:

public AcceptInvitation(int accountId, string invitationKey) 
{ 
    if (string.IsNullOrEmpty(invitationKey)) throw new ArgumentNullException("invitationKey"); 
    if (accountId <= 0) throw new ArgumentOutOfRangeException("accountId"); 

    AccountId = accountId; 
    InvitationKey = invitationKey; 
} 

..這報告 「4」 級耦合。 ArgumentOutOfRangeExceptionArgumentNullException是顯而易見的。另外兩個是什麼?從我讀的string被認爲是一個原始的,不算。

全班:

/// <summary> 
///  You must create an account before accepting the invitation 
/// </summary> 
public class AcceptInvitation : Request<AcceptInvitationReply> 
{ 
    /// <summary> 
    ///  Creates a new instance of <see cref="AcceptInvitation" />. 
    /// </summary> 
    /// <param name="userName">username</param> 
    /// <param name="password">clear text password</param> 
    /// <param name="invitationKey">Key from the generated email.</param> 
    public AcceptInvitation(string userName, string password, string invitationKey) 
    { 
     if (userName == null) throw new ArgumentNullException("userName"); 
     if (password == null) throw new ArgumentNullException("password"); 
     if (invitationKey == null) throw new ArgumentNullException("invitationKey"); 

     UserName = userName; 
     Password = password; 
     InvitationKey = invitationKey; 
    } 

    /// <summary> 
    ///  Creates a new instance of <see cref="AcceptInvitation" />. 
    /// </summary> 
    /// <param name="accountId">Existing account</param> 
    /// <param name="invitationKey">Key from the generated email.</param> 
    /// <remarks> 
    ///  <para> 
    ///   Invite to an existing account. 
    ///  </para> 
    /// </remarks> 
    public AcceptInvitation(int accountId, string invitationKey) 
    { 
     if (string.IsNullOrEmpty(invitationKey)) throw new ArgumentNullException("invitationKey"); 
     if (accountId <= 0) throw new ArgumentOutOfRangeException("accountId"); 

     AccountId = accountId; 
     InvitationKey = invitationKey; 
    } 


    /// <summary> 
    ///  Serialization constructor 
    /// </summary> 
    protected AcceptInvitation() 
    { 
    } 


    /// <summary> 
    ///  The email that was used when creating an account. 
    /// </summary> 
    /// <remarks> 
    ///  <para> 
    ///   Do note that this email can be different compared to the one that was used when sending the invitation. Make 
    ///   sure that this one is assigned to the created account. 
    ///  </para> 
    /// </remarks> 
    public string AcceptedEmail { get; set; } 

    /// <summary> 
    ///  Invite to an existing account 
    /// </summary> 
    /// <remarks> 
    ///  <para> 
    ///   Alternative to the <see cref="UserName" />/<see cref="Password" /> combination 
    ///  </para> 
    /// </remarks> 
    public int AccountId { get; set; } 

    /// <summary> 
    ///  Email that the inviation was sent to 
    /// </summary> 
    public string EmailUsedForTheInvitation { get; set; } 


    /// <summary> 
    ///  First name 
    /// </summary> 
    public string FirstName { get; set; } 

    /// <summary> 
    ///  Invitation key from the invitation email. 
    /// </summary> 
    public string InvitationKey { get; private set; } 

    /// <summary> 
    ///  Last name 
    /// </summary> 
    public string LastName { get; set; } 

    /// <summary> 
    ///  password 
    /// </summary> 
    /// <seealso cref="UserName" /> 
    public string Password { get; private set; } 

    /// <summary> 
    ///  Username as entered by the user 
    /// </summary> 
    /// <remarks> 
    ///  <para>Used together with <see cref="Password" /></para> 
    ///  <para>Alternative to <see cref="AccountId" /></para> 
    /// </remarks> 
    public string UserName { get; private set; } 
} 
+0

基類及其泛型類型參數。你在兩個構造函數 – InBetween

回答

2

沒有看到你的整個類的代碼,我的假設是,你有被初始化內嵌二級域。例如:

class Class1 
{ 
    private Person = new Person(); 
    private Automobile = new Automobile(); 

    protected Class1() { } 
} 

上述類將顯示的2類耦合的受保護的構造,因爲在運行的構造將導致這些兩個字段進行初始化。


看到整個班級後,很顯然,該耦合是由於來自請求<AcceptInvitationReply>繼承。

爲受保護的構造所產生的IL看起來是這樣的:

.method family hidebysig specialname rtspecialname instance void .ctor() cil managed 
{ 
    IL_0000: ldarg.0 
    IL_0001: call instance void class YourNamespace.Request`1<class YourNamespace.AcceptInvitationReply>::.ctor() 
    IL_0006: nop 
    IL_0007: nop 
    IL_0008: ret 
} 

正如你可以看到,構造絕對是連接到這兩個類。

+0

中都有一個隱含的'base()',不幸的是,對我而言情況並非如此。我添加了全班 – jgauffin

+0

你的類繼承自Request 。這意味着您的構造函數必須在Request <>類上調用構造函數(即使未定義該構造函數)。爲了進行該調用,構造函數必須知道Request <>類和AcceptInvitationReply類。 – esteuart

1

在這兩個構造函數中都有一個隱含的base()

這會創建一個與基類的耦合,因爲它的泛型,您會看到兩個附加耦合;基類及其泛型類型參數。如果它不是通用的,那麼你只有一個額外的耦合。