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”报告为类耦合。 ArgumentOutOfRangeException
和 ArgumentNullException
是显而易见的。另外两个是什么?从我读过的内容来看,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; }
}
最佳答案
在没有看到整个类的代码的情况下,我假设您有两个内联初始化的类字段。例如:
class Class1
{
private Person = new Person();
private Automobile = new Automobile();
protected Class1() { }
}
上面的类将显示 protected 构造函数的类耦合为 2,因为运行构造函数将导致这两个字段初始化。
看到整个类,很明显耦合是由于继承自Request
为 protected 构造函数生成的 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
}
如您所见,构造函数肯定与这两个类耦合。
关于c# - 空构造函数中两个类的耦合(VStudio代码分析),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41901949/