question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support for ```required``` properties

See original GitHub issue

Problem Statement

Traditionally, Autofac creates objects by calling one of their constructors. The constructor accepts dependencies and then assigns/initializes values.

Starting in C#11, dependencies can also be expressed using the required keyword. When a property is required, if the constructor that was called does not have the [SetsRequiredMembers] attribute, the property must have a value set immediately following construction.

Currently AutoFac does not support this core enhancement to object construction.

Desired Solution

When Autofac constructs an object, the constructor should be examined for [SetsRequiredMembers]. If it is not found, all writable properties containing the RequiredMemberAttribute attribute should be injected.

    public class Foo1 { 
        //This should be injected
        public required Dependency? Dep { protected get; init; }
    }

    public class Foo2 {
        //This should not
        public required Dependency? Dep { protected get; init; }

        [SetsRequiredMembers]
        public Foo2() {
            this.Dep = null;
        }

    }

Additional Context

Because C#11 introduces this core-level enhancement to object construction, the above rules should always be enabled.

The following code will be helpful in implementing this enhancement:

    public class PS : IPropertySelector {
        public static IPropertySelector Instance { get; }

        private static string RequiredAttributeName { get; }
        private static string SetsRequiredMembersAttributeName { get; }

        static PS() {
            Instance = new PS();

            //The type names are stored as strings so that this can also be used with legacy .NET platforms
            RequiredAttributeName = "System.Runtime.CompilerServices.RequiredMemberAttribute";
            SetsRequiredMembersAttributeName = "System.Diagnostics.CodeAnalysis.SetsRequiredMembersAttribute";
        }

        public bool NeedsPropertyInjection(ConstructorInfo constructorInfo) {
            var SetsRequiredMembers = constructorInfo.GetCustomAttributesData().Any(x => string.Equals(x.AttributeType.FullName, SetsRequiredMembersAttributeName, StringComparison.InvariantCultureIgnoreCase));

            var ret = !SetsRequiredMembers;

            return ret;

        }

        public bool InjectProperty(PropertyInfo propertyInfo, object instance) {
            var ret = propertyInfo.CanWrite
                && propertyInfo.GetCustomAttributesData().Any(x => string.Equals(x.AttributeType.FullName, RequiredAttributeName, StringComparison.InvariantCultureIgnoreCase))
                ;

            return ret;

        }
    }

Issue Analytics

  • State:closed
  • Created 10 months ago
  • Reactions:1
  • Comments:25 (13 by maintainers)

github_iconTop GitHub Comments

1reaction
alistairjevanscommented, Mar 7, 2023
1reaction
tilligcommented, Dec 17, 2022

I don’t think we need to be sold on the value of the feature. At this point it’s more a question of how it should work and how we can remain compatible with M.E.DI. I’m almost more concerned about that compatibility issue because trying to retrofit compatibility has caused us a lot of trouble in the past. Folks, for whatever reason, choose Autofac as the underlying container and then complain when it isn’t identical to the Microsoft container.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Require properties for deserialization
Learn how to mark properties as required for deserialization to succeed.
Read more >
Support for .NET 7 required properties #4151
C#11 / .NET 7 has a new feature that allows properties to be marked as required. ... It shouldn't be possible that Address...
Read more >
Required, localizable, and editable properties - Reference
Reference. Required, localizable, and editable properties. The tables below show the app and version properties required for App Store submission.
Read more >
Required properties when using Official File web service
Analyzing the issue I found that the metadata of the document got from the document library contained default properties added by SharePoint.
Read more >
HTML attribute: required - MDN Web Docs
The Boolean required attribute, if present, indicates that the user must specify a value for the input before the owning form can be ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found