简体   繁体   English

不可为空类型的通用 throw 函数

[英]Generic throw function for non-nullable types

I am trying to convert an existing project to using nullable reference types properties using non-nullable properties during initialization .我试图在初始化期间使用不可为空的属性将现有项目转换为使用可空引用类型属性。

I am using this approach for getting properties which should exist:我正在使用这种方法来获取应该存在的属性:

public class Dog
{
    private Person? _person;
    public int Id { get; set; }

    public string Name { get; set; }

    public Person Person
    {
        get => _person ?? throw new InvalidOperationException(
            $"Unset property {nameof(_person)}. You probably forgot to Include it");
        set => _person = value;
    }
}

But it's tedious to write this for almost every property, so I've tried to make a generic ThrowOrGet() function:但是为几乎每个属性都写这个很乏味,所以我试图制作一个通用的ThrowOrGet()函数:

public static class Util
{
    public static T ThrowOrGet<T>([AllowNull] T obj)
    {
        if (obj == null)
        {
            throw new InvalidOperationException(
                $"Unset property {nameof(obj)}. You probably forgot to Include it");
        }
        return obj;
    }
}

Which is used like this:这是这样使用的:

public class Dog
{
    private Person? _person;
    public int Id { get; set; }

    public string Name { get; set; }

    public Person Person
    {
        get =>  Util.ThrowOrGet(_person); ---> "Possible null reference return"
        set => _person = value;
    }
}

But the the function Util.ThrowOrGet(_person);但是函数Util.ThrowOrGet(_person); now says that it's a possible null reference return.现在说这是一个可能的null引用返回。 If I inline the function, it works as intended.如果我内联该函数,它将按预期工作。 Is there a way to make a generic function that does this?有没有办法制作一个通用函数来做到这一点?

If you're only going to use ThrowOrGet on reference types (which you should, as it doesn't make sense for value types), then you should declare it like this:如果你只打算在引用类型上使用ThrowOrGet (你应该这样做,因为它对值类型没有意义),那么你应该像这样声明它:

public static T ThrowOrGet<T>(T? obj) where T : class
{
    if (obj == null)
    {
        throw new InvalidOperationException(
            $"Unset property {nameof(obj)}. You probably forgot to Include it");
    }
    return obj;
}

This says that the function accepts a nullable argument, and always returns a non-nullable reference.这表示该函数接受一个可为空的参数,并始终返回一个不可为空的引用。 It's better to follow this pattern than rely on the attributes, as they're really just there for complex cases, which this is not.遵循此模式比依赖属性更好,因为它们实际上只用于复杂情况,而事实并非如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 泛型类型的函数:返回一个不可为空的值 - Generic typed function: return a non-nullable value 如何创建使用允许空值和非空值类型的通用类型的类 - How to create a class that uses a generic type that allows nullable and non-nullable types 通用返回类型和非空对象 - Generic return type and non-nullable objects 使用JSchemaGenerator创建非空数组类型 - Creating Non-Nullable Array Types With JSchemaGenerator 在 C# 中创建不可为 Null 的类型 - Create Non-Nullable Types in C# 不可为空的引用类型的默认值 VS 不可为空的值类型的默认值 - Non-nullable reference types' default values VS non-nullable value types' default values 使用泛型处理可空与不可空类型 - Handling nullable vs. non-nullable types with generics C# 可为空的引用类型:此函数能否在不覆盖空检查的情况下接受具有可为空值和不可为空值的字典? - C# Nullable Reference Types: can this function accept a dictionary with both nullable and non-nullable value without overriding null checking? 所有实体类型的键属性必须映射到存储函数返回的相同的非空列 - Key properties of all entity types must be mapped to the same non-nullable columns returned by the storage function 不可空类型的通用约束 - Generic Constraint for Non Nullable types
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM