小编Yor*_*oro的帖子

为什么虚张声势的示例值不起作用?

这里我example="this does not work"为 swashbuckle 框架添加了 XML 文档参数。

/// <summary>Gets a user by id.</summary>
/// <param name="id" example="this does not work">The id of user.</param>
/// <returns>got user.</returns>
[HttpGet("Get")]
[ProducesResponseType(typeof(UserDTO), 200)]
[ProducesResponseType(typeof(AppException), StatusCodes.Status500InternalServerError)]
public async Task<IActionResult> Get(string id)
{
    return HandleResult(await _userServices.GetUserAsync(id));
}
Run Code Online (Sandbox Code Playgroud)

但示例值不会出现在 Swagger UI 中。

在此输入图像描述

我应该去哪里寻找问题?

.net-core asp.net-core swashbuckle.aspnetcore

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

如何创建这样的通用映射器?

我有很多类,它们具有类似的映射对象操作。abstract class为了保持代码干燥,我想为所有这些类创建基础。baseabstract class将具有如下通用映射函数:

public TEntity GetEntity(Result<TServiceClientEntity> res) 
{
    entity = MappingProfiles.TryMap(res.Value);
    //some logic with result here
    return entity;
}
Run Code Online (Sandbox Code Playgroud)

结果类:

public class Result<T>
{
    public T Value{ get; set; }
    //some more properties..
}
Run Code Online (Sandbox Code Playgroud)

但问题是我想不出如何映射通用类的方法:

public static class MappingProfiles
{
    public static T2 TryMap<T,T2>(T t) 
    {
        return (T2)Map((Real_T_type)t); //f.e.: the type is ExampleFrom
    }

    public static ExampleTo Map(ExampleFrom from)
    {
        return new ExampleTo
        {
            exapleValue = from.exapleValue
        };
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑:

我还希望TryMap通用方法使用我预定义的Map手动方法进行映射。

c# .net-core

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

创建具有许多不可为空属性的 .NET6 类的最佳方法是什么?

我尝试创建具有一定数量的非空属性(> 7)的类。

如果我这样做:

public class Meeting
{

    public Name Name { get; set; }
    public Person ResponsiblePerson { get; set; }
    public Description Description {get; set; }
    public Category Category { get; set; }
    public Type Type { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }
    public List<Person> Attendees { get; set; }


    public Meeting()
    {
        Attendees = new List<Person>();
    }
}
Run Code Online (Sandbox Code Playgroud)

我收到这样的警告: “退出构造函数时,不可为空的属性 '...' 必须包含非空值。请考虑将该属性声明为可空。”

我想到了一个变体列表:

  • 默认值删除了这个警告,但我认为,它只会添加无用的值,将来无论如何都需要更改。

  • 如果我初始化构造函数中的所有属性,此警告就会消失,但构造函数将具有 >7 个参数,这将是一场噩梦。 …

.net c#

2
推荐指数
1
解决办法
1957
查看次数