小编Dun*_*imu的帖子

使用反射确定 C# 中引用类型的可为空性

我有使用 C#-8 并启用可为 null 类型的 .NET Core 项目。

我有以下课程

public class MyClass
{
    public int? NullableInt { get; private set; }

    public string? NullableString { get; private set; }

    public string NonNullableString { get; private set; }

    public MySubClass? MyNullableSubClass { get; private set; }

}
Run Code Online (Sandbox Code Playgroud)

我需要能够循环遍历类的所有属性并确定哪些属性可为空。

所以我的代码看起来像这样

public IEnumerable<string> GetNullableProperties(Type type)
{
    var nullableProperties = new List<string>();
    foreach (var property in type.GetProperties())
    {
       var isNullable = false;
       if (property.PropertyType.IsValueType)
       {
           isNullable = Nullable.GetUnderlyingType(property.PropertyType) != null;
       } else {
           var …
Run Code Online (Sandbox Code Playgroud)

.net c# .net-core c#-8.0 nullable-reference-types

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

Swashbuckle OperationFilter 添加类型对象的参数

我有一个 C# 对象

public class MyObject
{
 public int property1 { get; set; }
 public string property2 { get; set; }
 public string property3 { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我想创建一个具有MyObject.

public class MyFilter : IOperationFilter
{
 public void Apply(OpenApiOperation operation, OperationFilterContext context)
 {
   operation.Parameters.Add(new OpenApiParameter
   {
    Name = "MyParameter",
    Description = "MyDescription"
    In = ParameterLocation.Header
    Required = true,
    Schema = new OpenApiSchema()
    {
     Type = "object",
     Properties = new Dictionary<string, OpenApiSchema>()
     {
       // MyObject
     }
    }
   }
  } …
Run Code Online (Sandbox Code Playgroud)

c# asp.net swashbuckle openapi

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