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

Class coupling of two in empty constructor (VStudio code analysis)

Visual studio 代码分析报告了此构造函数的两个 class 耦合:

protected AcceptInvitation()
{
}

有人可以解释为什么吗?首先,我认为这是因为其他 classes 依赖于它。但是我用了"Find all usages",结果报了none

那么我如何才能弄清楚为什么 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”报告为 class 耦合。 ArgumentOutOfRangeExceptionArgumentNullException 是显而易见的。另外两个是什么?根据我的阅读,string 被认为是原始的,不计算在内。

整个class:

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

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

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

    protected Class1() { }
}

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


看完整个class,很明显耦合是由于继承自Request.

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

如您所见,构造函数肯定与这两个 classes 耦合。

两个构造函数中都有一个隐含的base()

这会创建与您的基础的耦合 class,并且因为它是通用的,所以您会看到两个额外的耦合;基础 class 及其泛型类型参数。如果它不是通用的,您将只有一个额外的耦合。