/// <summary>
        /// Facilitates the creation of simple "function objects" that implement one
        /// or more interfaces by delegation to a provided <seealso cref="MethodHandle"/>,
        /// after appropriate type adaptation and partial evaluation of arguments.
        /// Typically used as a <em>bootstrap method</em> for {@code invokedynamic}
        /// call sites, to support the <em>lambda expression</em> and <em>method
        /// reference expression</em> features of the Java Programming Language.
        ///
        /// <para>This is the standard, streamlined metafactory; additional flexibility
        /// is provided by <seealso cref="#altMetafactory(MethodHandles.Lookup, String, MethodType, Object...)"/>.
        /// A general description of the behavior of this method is provided
        /// <seealso cref="LambdaMetafactory above"/>.
        ///
        /// </para>
        /// <para>When the target of the {@code CallSite} returned from this method is
        /// invoked, the resulting function objects are instances of a class which
        /// implements the interface named by the return type of {@code invokedType},
        /// declares a method with the name given by {@code invokedName} and the
        /// signature given by {@code samMethodType}.  It may also override additional
        /// methods from {@code Object}.
        ///
        /// </para>
        /// </summary>
        /// <param name="caller"> Represents a lookup context with the accessibility
        ///               privileges of the caller.  When used with {@code invokedynamic},
        ///               this is stacked automatically by the VM. </param>
        /// <param name="invokedName"> The name of the method to implement.  When used with
        ///                    {@code invokedynamic}, this is provided by the
        ///                    {@code NameAndType} of the {@code InvokeDynamic}
        ///                    structure and is stacked automatically by the VM. </param>
        /// <param name="invokedType"> The expected signature of the {@code CallSite}.  The
        ///                    parameter types represent the types of capture variables;
        ///                    the return type is the interface to implement.   When
        ///                    used with {@code invokedynamic}, this is provided by
        ///                    the {@code NameAndType} of the {@code InvokeDynamic}
        ///                    structure and is stacked automatically by the VM.
        ///                    In the event that the implementation method is an
        ///                    instance method and this signature has any parameters,
        ///                    the first parameter in the invocation signature must
        ///                    correspond to the receiver. </param>
        /// <param name="samMethodType"> Signature and return type of method to be implemented
        ///                      by the function object. </param>
        /// <param name="implMethod"> A direct method handle describing the implementation
        ///                   method which should be called (with suitable adaptation
        ///                   of argument types, return types, and with captured
        ///                   arguments prepended to the invocation arguments) at
        ///                   invocation time. </param>
        /// <param name="instantiatedMethodType"> The signature and return type that should
        ///                               be enforced dynamically at invocation time.
        ///                               This may be the same as {@code samMethodType},
        ///                               or may be a specialization of it. </param>
        /// <returns> a CallSite whose target can be used to perform capture, generating
        ///         instances of the interface named by {@code invokedType} </returns>
        /// <exception cref="LambdaConversionException"> If any of the linkage invariants
        ///                                   described <seealso cref="LambdaMetafactory above"/>
        ///                                   are violated </exception>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public static CallSite metafactory(MethodHandles.Lookup caller, String invokedName, MethodType invokedType, MethodType samMethodType, MethodHandle implMethod, MethodType instantiatedMethodType) throws LambdaConversionException
        public static CallSite Metafactory(MethodHandles.Lookup caller, String invokedName, MethodType invokedType, MethodType samMethodType, MethodHandle implMethod, MethodType instantiatedMethodType)
        {
            AbstractValidatingLambdaMetafactory mf;

            mf = new InnerClassLambdaMetafactory(caller, invokedType, invokedName, samMethodType, implMethod, instantiatedMethodType, false, EMPTY_CLASS_ARRAY, EMPTY_MT_ARRAY);
            mf.ValidateMetafactoryArgs();
            return(mf.BuildCallSite());
        }
        /// <summary>
        /// Facilitates the creation of simple "function objects" that implement one
        /// or more interfaces by delegation to a provided <seealso cref="MethodHandle"/>,
        /// after appropriate type adaptation and partial evaluation of arguments.
        /// Typically used as a <em>bootstrap method</em> for {@code invokedynamic}
        /// call sites, to support the <em>lambda expression</em> and <em>method
        /// reference expression</em> features of the Java Programming Language.
        ///
        /// <para>This is the general, more flexible metafactory; a streamlined version
        /// is provided by {@link #metafactory(java.lang.invoke.MethodHandles.Lookup,
        /// String, MethodType, MethodType, MethodHandle, MethodType)}.
        /// A general description of the behavior of this method is provided
        /// <seealso cref="LambdaMetafactory above"/>.
        ///
        /// </para>
        /// <para>The argument list for this method includes three fixed parameters,
        /// corresponding to the parameters automatically stacked by the VM for the
        /// bootstrap method in an {@code invokedynamic} invocation, and an {@code Object[]}
        /// parameter that contains additional parameters.  The declared argument
        /// list for this method is:
        ///
        /// <pre>{@code
        ///  CallSite altMetafactory(MethodHandles.Lookup caller,
        ///                          String invokedName,
        ///                          MethodType invokedType,
        ///                          Object... args)
        /// }</pre>
        ///
        /// </para>
        /// <para>but it behaves as if the argument list is as follows:
        ///
        /// <pre>{@code
        ///  CallSite altMetafactory(MethodHandles.Lookup caller,
        ///                          String invokedName,
        ///                          MethodType invokedType,
        ///                          MethodType samMethodType,
        ///                          MethodHandle implMethod,
        ///                          MethodType instantiatedMethodType,
        ///                          int flags,
        ///                          int markerInterfaceCount,  // IF flags has MARKERS set
        ///                          Class... markerInterfaces, // IF flags has MARKERS set
        ///                          int bridgeCount,           // IF flags has BRIDGES set
        ///                          MethodType... bridges      // IF flags has BRIDGES set
        ///                          )
        /// }</pre>
        ///
        /// </para>
        /// <para>Arguments that appear in the argument list for
        /// <seealso cref="#metafactory(MethodHandles.Lookup, String, MethodType, MethodType, MethodHandle, MethodType)"/>
        /// have the same specification as in that method.  The additional arguments
        /// are interpreted as follows:
        /// <ul>
        ///     <li>{@code flags} indicates additional options; this is a bitwise
        ///     OR of desired flags.  Defined flags are <seealso cref="#FLAG_BRIDGES"/>,
        ///     <seealso cref="#FLAG_MARKERS"/>, and <seealso cref="#FLAG_SERIALIZABLE"/>.</li>
        ///     <li>{@code markerInterfaceCount} is the number of additional interfaces
        ///     the function object should implement, and is present if and only if the
        ///     {@code FLAG_MARKERS} flag is set.</li>
        ///     <li>{@code markerInterfaces} is a variable-length list of additional
        ///     interfaces to implement, whose length equals {@code markerInterfaceCount},
        ///     and is present if and only if the {@code FLAG_MARKERS} flag is set.</li>
        ///     <li>{@code bridgeCount} is the number of additional method signatures
        ///     the function object should implement, and is present if and only if
        ///     the {@code FLAG_BRIDGES} flag is set.</li>
        ///     <li>{@code bridges} is a variable-length list of additional
        ///     methods signatures to implement, whose length equals {@code bridgeCount},
        ///     and is present if and only if the {@code FLAG_BRIDGES} flag is set.</li>
        /// </ul>
        ///
        /// </para>
        /// <para>Each class named by {@code markerInterfaces} is subject to the same
        /// restrictions as {@code Rd}, the return type of {@code invokedType},
        /// as described <seealso cref="LambdaMetafactory above"/>.  Each {@code MethodType}
        /// named by {@code bridges} is subject to the same restrictions as
        /// {@code samMethodType}, as described <seealso cref="LambdaMetafactory above"/>.
        ///
        /// </para>
        /// <para>When FLAG_SERIALIZABLE is set in {@code flags}, the function objects
        /// will implement {@code Serializable}, and will have a {@code writeReplace}
        /// method that returns an appropriate <seealso cref="SerializedLambda"/>.  The
        /// {@code caller} class must have an appropriate {@code $deserializeLambda$}
        /// method, as described in <seealso cref="SerializedLambda"/>.
        ///
        /// </para>
        /// <para>When the target of the {@code CallSite} returned from this method is
        /// invoked, the resulting function objects are instances of a class with
        /// the following properties:
        /// <ul>
        ///     <li>The class implements the interface named by the return type
        ///     of {@code invokedType} and any interfaces named by {@code markerInterfaces}</li>
        ///     <li>The class declares methods with the name given by {@code invokedName},
        ///     and the signature given by {@code samMethodType} and additional signatures
        ///     given by {@code bridges}</li>
        ///     <li>The class may override methods from {@code Object}, and may
        ///     implement methods related to serialization.</li>
        /// </ul>
        ///
        /// </para>
        /// </summary>
        /// <param name="caller"> Represents a lookup context with the accessibility
        ///               privileges of the caller.  When used with {@code invokedynamic},
        ///               this is stacked automatically by the VM. </param>
        /// <param name="invokedName"> The name of the method to implement.  When used with
        ///                    {@code invokedynamic}, this is provided by the
        ///                    {@code NameAndType} of the {@code InvokeDynamic}
        ///                    structure and is stacked automatically by the VM. </param>
        /// <param name="invokedType"> The expected signature of the {@code CallSite}.  The
        ///                    parameter types represent the types of capture variables;
        ///                    the return type is the interface to implement.   When
        ///                    used with {@code invokedynamic}, this is provided by
        ///                    the {@code NameAndType} of the {@code InvokeDynamic}
        ///                    structure and is stacked automatically by the VM.
        ///                    In the event that the implementation method is an
        ///                    instance method and this signature has any parameters,
        ///                    the first parameter in the invocation signature must
        ///                    correspond to the receiver. </param>
        /// <param name="args">       An {@code Object[]} array containing the required
        ///                    arguments {@code samMethodType}, {@code implMethod},
        ///                    {@code instantiatedMethodType}, {@code flags}, and any
        ///                    optional arguments, as described
        ///                    <seealso cref="#altMetafactory(MethodHandles.Lookup, String, MethodType, Object...)"/> above} </param>
        /// <returns> a CallSite whose target can be used to perform capture, generating
        ///         instances of the interface named by {@code invokedType} </returns>
        /// <exception cref="LambdaConversionException"> If any of the linkage invariants
        ///                                   described <seealso cref="LambdaMetafactory above"/>
        ///                                   are violated </exception>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public static CallSite altMetafactory(MethodHandles.Lookup caller, String invokedName, MethodType invokedType, Object... args) throws LambdaConversionException
        public static CallSite AltMetafactory(MethodHandles.Lookup caller, String invokedName, MethodType invokedType, params Object[] args)
        {
            MethodType   samMethodType          = (MethodType)args[0];
            MethodHandle implMethod             = (MethodHandle)args[1];
            MethodType   instantiatedMethodType = (MethodType)args[2];
            int          flags = (Integer)args[3];

            Class[]      markerInterfaces;
            MethodType[] bridges;
            int          argIndex = 4;

            if ((flags & FLAG_MARKERS) != 0)
            {
                int markerCount = (Integer)args[argIndex++];
                markerInterfaces = new Class[markerCount];
                System.Array.Copy(args, argIndex, markerInterfaces, 0, markerCount);
                argIndex += markerCount;
            }
            else
            {
                markerInterfaces = EMPTY_CLASS_ARRAY;
            }
            if ((flags & FLAG_BRIDGES) != 0)
            {
                int bridgeCount = (Integer)args[argIndex++];
                bridges = new MethodType[bridgeCount];
                System.Array.Copy(args, argIndex, bridges, 0, bridgeCount);
                argIndex += bridgeCount;
            }
            else
            {
                bridges = EMPTY_MT_ARRAY;
            }

            bool isSerializable = ((flags & FLAG_SERIALIZABLE) != 0);

            if (isSerializable)
            {
                bool foundSerializableSupertype = invokedType.ReturnType().IsSubclassOf(typeof(Serializable));
                foreach (Class c in markerInterfaces)
                {
                    foundSerializableSupertype |= c.IsSubclassOf(typeof(Serializable));
                }
                if (!foundSerializableSupertype)
                {
                    markerInterfaces = Arrays.CopyOf(markerInterfaces, markerInterfaces.Length + 1);
                    markerInterfaces[markerInterfaces.Length - 1] = typeof(Serializable);
                }
            }

            AbstractValidatingLambdaMetafactory mf = new InnerClassLambdaMetafactory(caller, invokedType, invokedName, samMethodType, implMethod, instantiatedMethodType, isSerializable, markerInterfaces, bridges);

            mf.ValidateMetafactoryArgs();
            return(mf.BuildCallSite());
        }