小编Azr*_*ran的帖子

Vnext Argument 1:无法从'string'转换为'System.IO.Stream'

我正在尝试使用Vnext项目创建一个通用的序列化程序,当我调用StreamWriter的构造函数时,它会抛出此编译器错误

错误CS1503参数1:无法从'string'转换为'System.IO.Stream'Test.ASP.NET Core 5.0 Helper.cs 14

即使有一个构造函数允许指定文件的路径作为参数.

这是我的班级文件

using System;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace Test
{
    public static class Helper
    {
        public static void SerializeToXml<T>(string path, T value)
        {
            var serializer = new XmlSerializer(typeof(T));
            using (var stream = new StreamWriter(path)) // ERROR OCCURS HERE
            {
                using (var writer = XmlWriter.Create(stream))
                {
                    serializer.Serialize(writer, value);
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的project.json文件

{
    "version": "1.0.0-*",
    "dependencies": {
    },
    "commands": {
        "run": "run"
    },
    "frameworks": …
Run Code Online (Sandbox Code Playgroud)

c# asp.net asp.net-core

7
推荐指数
1
解决办法
1万
查看次数

您会在数据库调用中使用流畅的验证吗

通过流畅的验证,您可以在更新密码之前验证简单的内容,例如 NotNull 、 numberGreaterThan 或更高级的业务规则(例如 userMustExistsOnDb )。

我觉得当我使用流畅验证时,我执行的数据库调用次数是不使用它时的两倍。这是一个例子。

public class DeleteCustomerRequestValidator: AbstractValidator<DeleteCustomerRequest> {
  public DeleteCUstomerRequestValidator() {
    RuleFor(customer => customer.Id).GreaterThan(0);
    RuleFor(customer => customer.Id).Must(ExistsOnDB).WithMessage("The customer does not exists");
  }

  private bool ExistsOnDB(int customerId) {
    // DB call to check if exists on Db
    return Respository.Customers.Any(x => x.Id == customerId)    // CALL NUMBER 1
  }
}
Run Code Online (Sandbox Code Playgroud)

这是我进行第二次调用的删除方法

public void DeleteCustomer(int customerId)
{
     Customer customer = Repository.Customers.First(x => x.Id);  // CALL NUMBER 2
     Repository.Customers.Delete(customer) 
     Repository.Save()
}
Run Code Online (Sandbox Code Playgroud)

但是,如果我不使用 Fluent 验证,我将只进行一次调用来从数据库获取客户。

public …
Run Code Online (Sandbox Code Playgroud)

c# database fluentvalidation

5
推荐指数
1
解决办法
6839
查看次数

标签 统计

c# ×2

asp.net ×1

asp.net-core ×1

database ×1

fluentvalidation ×1