IronPython.Runtime.Binding.SlotOrFunction.GetSlotOrFunction C# (CSharp) Méthode

GetSlotOrFunction() public static méthode

public static GetSlotOrFunction ( PythonContext state, string op ) : SlotOrFunction
state PythonContext
op string
Résultat SlotOrFunction
        public static SlotOrFunction/*!*/ GetSlotOrFunction(PythonContext/*!*/ state, string op, params DynamicMetaObject[] types) {
            PythonTypeSlot slot;
            SlotOrFunction res;
            if (TryGetBinder(state, types, op, null, out res)) {
                if (res != SlotOrFunction.Empty) {
                    return res;
                }
            } else if (MetaUserObject.GetPythonType(types[0]).TryResolveSlot(state.SharedContext, op, out slot)) {
                ParameterExpression tmp = Ast.Variable(typeof(object), "slotVal");

                Expression[] args = new Expression[types.Length - 1];
                for (int i = 1; i < types.Length; i++) {
                    args[i - 1] = types[i].Expression;
                }
                return new SlotOrFunction(
                    new DynamicMetaObject(
                        Ast.Block(
                            new ParameterExpression[] { tmp },
                            MetaPythonObject.MakeTryGetTypeMember(
                                state,
                                slot,
                                tmp,
                                types[0].Expression,
                                Ast.Call(
                                    typeof(DynamicHelpers).GetMethod("GetPythonType"),
                                    types[0].Expression
                                )
                            ),
                            Ast.Dynamic(
                                state.Invoke(
                                    new CallSignature(args.Length)
                                ),
                                typeof(object),
                                ArrayUtils.Insert<Expression>(
                                    AstUtils.Constant(state.SharedContext),
                                    tmp,
                                    args
                                )
                            )
                        ),
                        BindingRestrictions.Combine(types).Merge(BindingRestrictionsHelpers.GetRuntimeTypeRestriction(types[0].Expression, types[0].GetLimitType()))
                    ),
                    slot
                );
            }

            return SlotOrFunction.Empty;
        }

Usage Example

        /// <summary>
        /// Gets a MetaObject which converts the provided object to a bool using __bool__ or __len__
        /// protocol methods.  This code is shared between both our fallback for a site and our MetaObject
        /// for user defined objects.
        /// </summary>
        internal static DynamicMetaObject ConvertToBool(DynamicMetaObjectBinder /*!*/ conversion, DynamicMetaObject /*!*/ self)
        {
            Assert.NotNull(conversion, self);

            SlotOrFunction sf = SlotOrFunction.GetSlotOrFunction(
                PythonContext.GetPythonContext(conversion),
                "__bool__",
                self);

            if (sf.Success)
            {
                if (sf.Target.Expression.Type != typeof(bool))
                {
                    return(new DynamicMetaObject(
                               Ast.Call(
                                   typeof(PythonOps).GetMethod(nameof(PythonOps.ThrowingConvertToBool)),
                                   sf.Target.Expression
                                   ),
                               sf.Target.Restrictions
                               ));
                }

                return(sf.Target);
            }

            sf = SlotOrFunction.GetSlotOrFunction(
                PythonContext.GetPythonContext(conversion),
                "__len__",
                self);

            if (sf.Success)
            {
                return(new DynamicMetaObject(
                           GetConvertByLengthBody(
                               PythonContext.GetPythonContext(conversion),
                               sf.Target.Expression
                               ),
                           sf.Target.Restrictions
                           ));
            }

            return(null);
        }