Git Product home page Git Product logo

Comments (6)

Kimi-Arthur avatar Kimi-Arthur commented on September 16, 2024

Shit. If without this or this, this can't be implemented nicely with SourceGenerator.

from kifanet.

Kimi-Arthur avatar Kimi-Arthur commented on September 16, 2024

So here is a slight better solution without needing the Source Generator magic:

public static class Safe {
    public static T Get<T>(T? value) {
        if (value != null) {
            return value;
        }

        var method = new StackFrame(1).GetMethod();
        if (method == null) {  // This if may be skipped IMO
            throw new NullReferenceException(
                "Unexpectedly failed to find info of getting a null value.");
        }

        throw new NullReferenceException(
            $"Property {method.Name[4..]} of class {method.DeclaringType} is expected to be non-null, but is actually null.");
    }

    public static void Set<T>(ref T field, T? value) {
        if (value == null) {
            var method = new StackFrame(1).GetMethod();
            if (method == null) {  // This if may be skipped IMO
                throw new NullReferenceException(
                    "Unexpectedly failed to find info of setting a null value.");
            }

            throw new NullReferenceException(
                $"Cannot assign null value to non-null property {method.Name[4..]} of class {method.DeclaringType}.");
        }

        field = value;
    }
}

And the usage looks like:

private string? _id;

public string Id {
    get => Safe.Get(_id);
    set => Safe.Set(ref _id, value);
}

It's also possible to implement the Set method to return instead of ref.

from kifanet.

Kimi-Arthur avatar Kimi-Arthur commented on September 16, 2024

For struct types like int to work. You may need to add

    public static T Get<T>(T? value) where T : struct {
        if (value != null) {
            return value.Value;
        }

        var method = new StackFrame(1).GetMethod();
        if (method == null) {
            throw new NullReferenceException(
                "Unexpectedly failed to find info of getting a null value.");
        }

        throw new NullReferenceException(
            $"Property {method.Name[4..]} of class {method.DeclaringType} is expected to be non-null, but is actually null.");
    }

where the where T : struct is the key.

from kifanet.

Kimi-Arthur avatar Kimi-Arthur commented on September 16, 2024

Performance wise, only failure handling is impacted by reflection. So it should be OK. Especially errors like this should be fixed instead of tolerated, so most likely it should only happen once in each run and should be in common in developing time.

from kifanet.

Kimi-Arthur avatar Kimi-Arthur commented on September 16, 2024

As suggested by this answer, [CallerMemberName] will help a lot, but there is no [CallerTypeName] is not available (see here).

Since I don't feel file path is a reasonable solution, we will stick to StackFrame approach for now.

from kifanet.

Kimi-Arthur avatar Kimi-Arthur commented on September 16, 2024

This change conflicts with Json.net. Fixed in 9ade493

In general, I think this is solved.

from kifanet.

Related Issues (12)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.