Esempio n. 1
0
        public InstanceProvider?GetInstanceProvider(TypeInfo forType)
        {
            var paired = GetPairedType(forType, DESERIALIZER_KIND);

            if (paired == null)
            {
                return(null);
            }

            var instanceMtd = paired.GetMethodNonNull("__InstanceProvider", PublicStatic);

#pragma warning disable CS0618 // This obsolete to prevent clients from using them, but they are fine for us.
            var forConstructorAttrs = instanceMtd.GetCustomAttributes <ConstructorInstanceProviderAttribute>();
#pragma warning restore CS0618

            if (forConstructorAttrs.Any())
            {
                var consOnTypes = forConstructorAttrs.Select(x => x.ForType).Distinct().ToImmutableArray();
                if (consOnTypes.Length > 1)
                {
                    Throw.ImpossibleException($"Generated type {paired} (for {forType}) claims multiple constructors for an InstanceProvider.");
                }

                var consOnType = consOnTypes.Single();
                var consParams = forConstructorAttrs.OrderBy(x => x.ParameterIndex).Select(i => i.ParameterType).ToArray();

                var cons = consOnType.GetConstructor(AllInstance, null, consParams, null);
                if (cons == null)
                {
                    Throw.ImpossibleException($"Generated type {paired} (for {forType}) claims a constructor for an InstanceProvider that could not be found.");
                }

                return(InstanceProvider.ForConstructorWithParametersInner(cons, paired));
            }

            return(InstanceProvider.ForMethodInner(instanceMtd, paired));
        }