小编jim*_*own的帖子

将新元组转换为旧元组会产生编译错误

使用下面的代码,

class Program
{
    static void Main(string[] args)
    {
        Tuple<int, int> test = TupleTest();

    }

    static (int, int) TupleTest()
    {
        return (1, 2);
    }
Run Code Online (Sandbox Code Playgroud)

我收到编译时错误.

错误CS0029无法将类型'(int,int)'隐式转换为'System.Tuple <int,int>'

是否意味着新版本的Tuple隐含地与旧版本不兼容?或者我在这里做错了什么?

c# tuples c#-7.0

6
推荐指数
1
解决办法
1595
查看次数

模拟没有接口或虚方法的类

我想测试一个带有以下签名的方法.

int SomeMethod(List<Employee> employees)
Run Code Online (Sandbox Code Playgroud)

这是相关的课程

public class Employee
{
    public int CustomerID { get; set; }
    public string LastName { get; set; }
    public string FirstName { get; set; }
    public Address Address { get; set; }

}

public class Address
{
    public string StreetName { get; set; }
    public string CityName { get; set; }
    public string State { get; set; }
    public string Country { get; set; }
    public string ZipCode { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我如何模拟List<Employee> …

c# unit-testing mocking

3
推荐指数
1
解决办法
655
查看次数

从验证器类记录日志

我在 Web API 中使用了一个简单的类,如下所示。正如您所看到的,它有一个通过属性应用的验证器

[CustomerNameValidator]
public class Customer
{
    public string CustomerName { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

验证器类如下所示

public class CustomerNameValidatorAttribute : ValidationAttribute
{

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        Customer customer = (Customer)validationContext.ObjectInstance;

        if ( string.IsNullOrEmpty(customer.CustomerName))
        {
            return new ValidationResult("Invalid customer Name");
        }


        return ValidationResult.Success;
    }
}
Run Code Online (Sandbox Code Playgroud)

我想在 IsValid 方法中添加一些日志记录。我正在使用 Startup 类设置的其他地方使用日志记录,如下所示。

public Startup(IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    var builder = new ConfigurationBuilder()
        .SetBasePath(env.ContentRootPath)
        .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
        .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
        .AddEnvironmentVariables();
    Configuration = builder.Build();


    loggerFactory.AddConsole(Configuration.GetSection("Logging")); …
Run Code Online (Sandbox Code Playgroud)

c# validation asp.net-web-api .net-core asp.net-core

3
推荐指数
1
解决办法
953
查看次数