Repository: dotnet/aspnetcore
File: src/Components/Components/src/PersistentState/PersistentServicesRegistry.cs (C#)

Code before:
            if (instance != null)
            {
                RestoreInstanceState(instance, type, state);
            }
        }
    }

    [RequiresUnreferencedCode("Calls Microsoft.AspNetCore.Components.PersistentComponentState.TryTakeFromJson(String, Type, out Object)")]
    private static void RestoreInstanceState(object instance, Type type, PersistentComponentState state)
    {
        var accessors = _cachedAccessorsByType.GetOrAdd(instance.GetType(), static (runtimeType, declaredType) => new PropertiesAccessor(runtimeType, declaredType), type);
        foreach (var (key, propertyType) in accessors.KeyTypePairs)
        {
            if (state.TryTakeFromJson(key, propertyType, out var result))
            {
                var (setter, getter) = accessors.GetAccessor(key);
                setter.SetValue(instance, result!);
            }
        }
    }

    internal static IPersistentServiceRegistration[] ResolveRegistrations(IEnumerable<IPersistentServiceRegistration> registrations) => [.. registrations.DistinctBy(r => (r.Assembly, r.FullTypeName)).OrderBy(r => r.Assembly).ThenBy(r => r.FullTypeName)];

    private static Type? ResolveType(IPersistentServiceRegistration registration)
    {
        if(registration.GetResolvedTypeOrNull() is Type type)
        {
            return type;
        }
        var assembly = registration.Assembly;
        var fullTypeName = registration.FullTypeName;
        return _persistentServiceTypeCache.GetRootType(assembly, fullTypeName);
    }

    private sealed class PropertiesAccessor
    {
        internal const BindingFlags BindablePropertyFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.IgnoreCase;

        private readonly Dictionary<string, (PropertySetter, PropertyGetter)> _underlyingAccessors;
        private readonly (string, Type)[] _cachedKeysForService;

        public PropertiesAccessor([DynamicallyAccessedMembers(LinkerFlags.Component)] Type targetType, Type keyType)
        {
            _underlyingAccessors = new Dictionary<string, (PropertySetter, PropertyGetter)>(StringComparer.OrdinalIgnoreCase);

            var keys = new List<(string, Type)>();
            foreach (var propertyInfo in GetCandidateBindableProperties(targetType))
            {
                SupplyParameterFromPersistentComponentStateAttribute? parameterAttribute = null;
                foreach (var attribute in propertyInfo.GetCustomAttributes())
                {

Review comment (suggestion):
```suggestion
        if (registration.GetResolvedTypeOrNull() is Type type)
```


Code after:
            if (instance != null)
            {
                RestoreInstanceState(instance, type, state);
            }
        }
    }

    [RequiresUnreferencedCode("Calls Microsoft.AspNetCore.Components.PersistentComponentState.TryTakeFromJson(String, Type, out Object)")]
    private static void RestoreInstanceState(object instance, Type type, PersistentComponentState state)
    {
        var accessors = _cachedAccessorsByType.GetOrAdd(instance.GetType(), static (runtimeType, declaredType) => new PropertiesAccessor(runtimeType, declaredType), type);
        foreach (var (key, propertyType) in accessors.KeyTypePairs)
        {
            if (state.TryTakeFromJson(key, propertyType, out var result))
            {
                var (setter, getter) = accessors.GetAccessor(key);
                setter.SetValue(instance, result!);
            }
        }
    }

    internal static IPersistentServiceRegistration[] ResolveRegistrations(IEnumerable<IPersistentServiceRegistration> registrations) => [.. registrations.DistinctBy(r => (r.Assembly, r.FullTypeName)).OrderBy(r => r.Assembly).ThenBy(r => r.FullTypeName)];

    private static Type? ResolveType(IPersistentServiceRegistration registration)
    {
        if (registration.GetResolvedTypeOrNull() is Type type)
        {
            return type;
        }
        var assembly = registration.Assembly;
        var fullTypeName = registration.FullTypeName;
        return _persistentServiceTypeCache.GetRootType(assembly, fullTypeName);
    }

    private sealed class PropertiesAccessor
    {
        internal const BindingFlags BindablePropertyFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.IgnoreCase;

        private readonly Dictionary<string, (PropertySetter, PropertyGetter)> _underlyingAccessors;
        private readonly (string, Type)[] _cachedKeysForService;

        public PropertiesAccessor([DynamicallyAccessedMembers(LinkerFlags.Component)] Type targetType, Type keyType)
        {
            _underlyingAccessors = new Dictionary<string, (PropertySetter, PropertyGetter)>(StringComparer.OrdinalIgnoreCase);

            var keys = new List<(string, Type)>();
            foreach (var propertyInfo in GetCandidateBindableProperties(targetType))
            {
                SupplyParameterFromPersistentComponentStateAttribute? parameterAttribute = null;
                foreach (var attribute in propertyInfo.GetCustomAttributes())
                {