我有使用 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) 我有一个 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)