private object InvokeMethod(string name, Type[] typeArgs, object[] args, object[] bindArgs)
        {
            var bindResult = BindMethod(name, typeArgs, args, bindArgs);

            if ((bindResult is MethodBindFailure) && target.Flags.HasFlag(HostTargetFlags.AllowExtensionMethods))
            {
                var targetArg     = new[] { target.Target };
                var extensionArgs = targetArg.Concat(args).ToArray();

                var targetBindArg     = new object[] { target };
                var extensionBindArgs = targetBindArg.Concat(bindArgs).ToArray();

                foreach (var type in ExtensionMethodSummary.Types)
                {
                    var extensionHostItem   = (HostItem)Wrap(engine, HostType.Wrap(type));
                    var extensionBindResult = extensionHostItem.BindMethod(name, typeArgs, extensionArgs, extensionBindArgs);
                    if (extensionBindResult is MethodBindSuccess)
                    {
                        var result = extensionBindResult.Invoke(extensionHostItem);
                        for (var index = 1; index < extensionArgs.Length; index++)
                        {
                            args[index - 1] = extensionArgs[index];
                        }

                        return(result);
                    }
                }
            }

            return(bindResult.Invoke(this));
        }
Esempio n. 2
0
        private static void AddTypeToNamespaceNode(PropertyBag node, Type type)
        {
            object value;
            var    name = type.GetRootName();

            if (!node.TryGetValue(name, out value))
            {
                node.SetPropertyNoCheck(name, HostType.Wrap(type));
                return;
            }

            var hostType = value as HostType;

            if (hostType != null)
            {
                var types = type.ToEnumerable().Concat(hostType.Types).ToArray();

                var groups = types.GroupBy(testType => testType.GetGenericParamCount()).ToIList();
                if (groups.Any(group => group.Count() > 1))
                {
                    types = groups.Select(ResolveTypeConflict).ToArray();
                }

                node.SetPropertyNoCheck(name, HostType.Wrap(types));
                return;
            }

            if (value is PropertyBag)
            {
                throw new OperationCanceledException(MiscHelpers.FormatInvariant("Type conflicts with namespace at '{0}'", type.GetLocator()));
            }

            throw new OperationCanceledException(MiscHelpers.FormatInvariant("Type conflicts with '{0}' at '{1}'", value.GetFriendlyName(), type.GetLocator()));
        }
Esempio n. 3
0
 /// <summary>
 /// Exposes a host type to script code with the specified options.
 /// </summary>
 /// <param name="itemName">A name for the new global script item that will represent the type.</param>
 /// <param name="flags">A value that selects options for the operation.</param>
 /// <param name="type">The type to expose.</param>
 /// <remarks>
 /// Host types are exposed to script code in the form of objects whose properties and
 /// methods are bound to the type's static members and nested types. If the type has
 /// generic parameters, the corresponding object will be invocable with type arguments to
 /// yield a specific type.
 /// <para>
 /// For more information about the mapping between host members and script-callable
 /// properties and methods, see <see cref="AddHostObject(string, HostItemFlags, object)"/>.
 /// </para>
 /// </remarks>
 public void AddHostType(string itemName, HostItemFlags flags, Type type)
 {
     MiscHelpers.VerifyNonNullArgument(type, "type");
     AddHostItem(itemName, flags, HostType.Wrap(type));
 }
Esempio n. 4
0
 /// <summary>
 /// Adds a type to a host type collection.
 /// </summary>
 /// <param name="type">The type to add.</param>
 public void AddType(Type type)
 {
     MiscHelpers.VerifyNonNullArgument(type, "type");
     AddType(HostType.Wrap(type));
 }
Esempio n. 5
0
 /// <summary>
 /// Converts a type to a host type for use with script code running in the specified
 /// script engine.
 /// </summary>
 /// <param name="type">The type to convert to a host type.</param>
 /// <param name="engine">The script engine in which the host type will be used.</param>
 /// <returns>A host type for use with script code.</returns>
 public static object ToHostType(this Type type, ScriptEngine engine)
 {
     MiscHelpers.VerifyNonNullArgument(type, nameof(type));
     MiscHelpers.VerifyNonNullArgument(engine, nameof(engine));
     return(HostItem.Wrap(engine, HostType.Wrap(type)));
 }