Пример #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="NewExpressionRepresentation"/> class.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="constructorInfo">The constructor info.</param>
        /// <param name="arguments">The arguments.</param>
        public NewExpressionRepresentation(
            TypeRepresentation type,
            ConstructorInfoRepresentation constructorInfo,
            IReadOnlyList <ExpressionRepresentationBase> arguments)
            : base(type, ExpressionType.New)
        {
            if (constructorInfo == null)
            {
                throw new ArgumentNullException(nameof(constructorInfo));
            }

            if (arguments == null)
            {
                throw new ArgumentNullException(nameof(arguments));
            }

            if (!arguments.Any())
            {
                throw new ArgumentException(Invariant($"'{nameof(arguments)}' is an empty enumerable"));
            }

            if (arguments.Any(_ => _ == null))
            {
                throw new ArgumentException(Invariant($"'{nameof(arguments)}' contains at least one null element"));
            }

            this.ConstructorInfo = constructorInfo;
            this.Arguments       = arguments;
        }
        /// <summary>
        /// Converts from representation.
        /// </summary>
        /// <param name="constructorInfoRepresentation">The representation.</param>
        /// <returns>
        /// Converted <see cref="ConstructorInfo" />.
        /// </returns>
        public static ConstructorInfo FromRepresentation(
            this ConstructorInfoRepresentation constructorInfoRepresentation)
        {
            if (constructorInfoRepresentation == null)
            {
                throw new ArgumentNullException(nameof(constructorInfoRepresentation));
            }

            var type = constructorInfoRepresentation.Type.ResolveFromLoadedTypes();

            var results = type.GetConstructors()
                          .Where(_ => _.GetSignatureHash().Equals(constructorInfoRepresentation.ConstructorHash, StringComparison.OrdinalIgnoreCase))
                          .ToList();

            if (!results.Any())
            {
                throw new ArgumentException(Invariant($"Could not find a constructor that matched hash '{constructorInfoRepresentation.ConstructorHash}' on type '{type}'."));
            }

            if (results.Count > 1)
            {
                var foundAddIn = string.Join(",", results.Select(_ => _.ToString()));

                throw new ArgumentException(Invariant($"Found too many constructors that matched hash '{constructorInfoRepresentation.ConstructorHash}' on type '{type}'; {foundAddIn}."));
            }

            var result = results.Single();

            return(result);
        }
Пример #3
0
        public NewExpressionRepresentation DeepCloneWithConstructorInfo(ConstructorInfoRepresentation constructorInfo)
        {
            var result = new NewExpressionRepresentation(
                this.Type?.DeepClone(),
                constructorInfo,
                this.Arguments?.DeepClone());

            return(result);
        }
        /// <summary>
        /// Converts to representation.
        /// </summary>
        /// <param name="constructorInfo">The constructor information.</param>
        /// <returns>
        /// Converted <see cref="ConstructorInfoRepresentation" />.
        /// </returns>
        public static ConstructorInfoRepresentation ToRepresentation(
            this ConstructorInfo constructorInfo)
        {
            if (constructorInfo == null)
            {
                throw new ArgumentNullException(nameof(constructorInfo));
            }

            var type = constructorInfo.DeclaringType.ToRepresentation();

            var constructorHash = constructorInfo.GetSignatureHash();

            var result = new ConstructorInfoRepresentation(type, constructorHash);

            return(result);
        }