空构造函数中的两个类耦合(VStudio代码分析)

时间:2017-01-27 19:51:10

标签: c# visual-studio code-analysis

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; }
}

2 个答案:

答案 0 :(得分:2)

如果没有看到整个类的代码,我的假设是你有两个内联初始化的类字段。例如:

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

    protected Class1() { }
}

上面的类将为受保护的构造函数显示2的类耦合,因为运行构造函数将导致这两个字段初始化。

在看到整个类之后,很明显耦合是由Request&lt; AcceptInvitationReply&gt;的继承引起的。

受保护构造函数的生成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
}

正如您所看到的,构造函数肯定与这两个类耦合。

答案 1 :(得分:1)

两个构造函数都有隐式base()

这会创建与基类的耦合,因为它是通用的,你会看到另外两个耦合;基类及其泛型类型参数。如果它不是通用的,那么你只有一个额外的耦合。