/// <summary>
        /// Initializes a new instance of the <see cref="ElementInitRepresentation"/> class.
        /// </summary>
        /// <param name="type">Type with method.</param>
        /// <param name="addMethod">The add method.</param>
        /// <param name="arguments">The arguments.</param>
        public ElementInitRepresentation(
            TypeRepresentation type,
            MethodInfoRepresentation addMethod,
            IReadOnlyList <ExpressionRepresentationBase> arguments)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

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

            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.Type      = type;
            this.AddMethod = addMethod;
            this.Arguments = arguments;
        }
Exemple #2
0
        public ElementInitRepresentation DeepCloneWithAddMethod(MethodInfoRepresentation addMethod)
        {
            var result = new ElementInitRepresentation(
                this.Type?.DeepClone(),
                addMethod,
                this.Arguments?.DeepClone());

            return(result);
        }
Exemple #3
0
        public MethodCallExpressionRepresentation DeepCloneWithMethod(MethodInfoRepresentation method)
        {
            var result = new MethodCallExpressionRepresentation(
                this.Type?.DeepClone(),
                this.NodeType.DeepClone(),
                this.ParentObject?.DeepClone(),
                method,
                this.Arguments?.DeepClone());

            return(result);
        }
Exemple #4
0
        /// <summary>
        /// Converts to representation.
        /// </summary>
        /// <param name="methodInfo">The method information.</param>
        /// <returns>
        /// Converted <see cref="MethodInfoRepresentation" />.
        /// </returns>
        public static MethodInfoRepresentation ToRepresentation(
            this MethodInfo methodInfo)
        {
            if (methodInfo == null)
            {
                throw new ArgumentNullException(nameof(methodInfo));
            }

            var methodHash = methodInfo.GetSignatureHash();

            var genericArguments = methodInfo.GetGenericArguments().Select(_ => _.ToRepresentation()).ToList();

            var result = new MethodInfoRepresentation(methodInfo.DeclaringType.ToRepresentation(), methodHash, genericArguments);

            return(result);
        }
Exemple #5
0
        /// <summary>
        /// Converts from representation.
        /// </summary>
        /// <param name="methodInfoRepresentation">The representation.</param>
        /// <returns>
        /// Converted <see cref="MemberInfo" />.
        /// </returns>
        public static MethodInfo FromRepresentation(
            this MethodInfoRepresentation methodInfoRepresentation)
        {
            if (methodInfoRepresentation == null)
            {
                throw new ArgumentNullException(nameof(methodInfoRepresentation));
            }

            var methodHash = methodInfoRepresentation.MethodHash;

            var genericArguments = methodInfoRepresentation.GenericArguments.Select(_ => _.ResolveFromLoadedTypes()).ToArray();

            var type = methodInfoRepresentation.Type.ResolveFromLoadedTypes();

            var methodInfos = type.GetAllMethodInfos();

            var methodHashAndInfoTupleSet = methodInfos.Select(methodInfo =>
            {
                var localMethodInfo = methodInfo.IsGenericMethod
                    ? methodInfo.MakeGenericMethod(genericArguments)
                    : methodInfo;
                var localMethodHash = localMethodInfo.GetSignatureHash();

                return(new Tuple <string, MethodInfo>(localMethodHash, localMethodInfo));
            });

            var results = methodHashAndInfoTupleSet.Where(_ => _.Item1.Equals(methodHash, StringComparison.OrdinalIgnoreCase)).ToList();

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

            if (results.Count > 1)
            {
                var foundAddIn = string.Join(",", results.Select(_ => _.Item2.ToString()));
                throw new ArgumentException(Invariant($"Found too many members that matched hash '{methodInfoRepresentation.MethodHash}' on type '{type}'; {foundAddIn}."));
            }

            var result = results.Single().Item2;

            return(result);
        }
Exemple #6
0
        /// <summary>
        /// Initializes a new instance of the <see cref="MethodCallExpressionRepresentation"/> class.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="nodeType">Type of the node.</param>
        /// <param name="parentObject">The object.</param>
        /// <param name="method">The method.</param>
        /// <param name="arguments">The arguments.</param>
        public MethodCallExpressionRepresentation(
            TypeRepresentation type,
            ExpressionType nodeType,
            ExpressionRepresentationBase parentObject,
            MethodInfoRepresentation method,
            IReadOnlyList <ExpressionRepresentationBase> arguments)
            : base(type, nodeType)
        {
            if (parentObject == null)
            {
                throw new ArgumentNullException(nameof(parentObject));
            }

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

            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.ParentObject = parentObject;
            this.Method       = method;
            this.Arguments    = arguments;
        }