使用下面的代码,
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隐含地与旧版本不兼容?或者我在这里做错了什么?
我想测试一个带有以下签名的方法.
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> …
我在 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# ×3
.net-core ×1
asp.net-core ×1
c#-7.0 ×1
mocking ×1
tuples ×1
unit-testing ×1
validation ×1