Пример #1
0
        private static Type GetGenericType(InvokeMemberBinder binder)
        {
            var csharpBinder = binder.GetType().GetInterface("Microsoft.CSharp.RuntimeBinder.ICSharpInvokeOrInvokeMemberBinder");
            var typeArgs = (csharpBinder.GetProperty("TypeArguments").GetValue(binder, null) as IList<Type>);

            return typeArgs.FirstOrDefault();
        }
Пример #2
0
 private Type GetMethodReturnType(InvokeMemberBinder binder)
 {
     IList<Type> types = binder.GetType().GetField("m_typeArguments", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(binder) as IList<Type>;
     if ((types != null) && (types.Count > 0))
     {
         return types[0];
     }
     return null;
 }
Пример #3
0
        public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
        {
            int nargout = 1;
            var args1 = args;

            if (binder.CallInfo.ArgumentNames.Count != 0 &&
                binder.CallInfo.ArgumentNames[binder.CallInfo.ArgumentNames.Count - 1].Equals("nargout",
                                                                                              StringComparison.Ordinal))
            {
                try
                {
                    nargout = (int) args[args.Length - 1];
                }
                catch (InvalidCastException)
                {
                    throw new ArgumentException("Type of nargout argument  must be Int32", "nargout");
                }

                args1 = new object[args.Length - 1];
                Array.Copy(args, 0, args1, 0, args1.Length);
            }
            else
            {
                if (_bInfo == null)
                {
                    _bInfo = binder.GetType().GetInterface(
                        "Microsoft.CSharp.RuntimeBinder.ICSharpInvokeOrInvokeMemberBinder")
                        .GetProperty("ResultDiscarded");
                }

                if ((bool) _bInfo.GetValue(binder, null))
                {
                    nargout = 0;
                }
            }

            var status = OctaveCore.Octave.Feval(binder.Name, args1, nargout, out result);

            switch (status)
            {
                case OctaveCore.Octave.FevalStatus.OK:
                    return true;
                case OctaveCore.Octave.FevalStatus.Error:
                    throw new OctaveRuntimeException(OctaveCore.Octave.GetLastError());
                case OctaveCore.Octave.FevalStatus.BadArgument:
                    throw new ArgumentException("Unsupported argument type", "args");
                case OctaveCore.Octave.FevalStatus.BadReturn:
                    throw new ArgumentException("Unsupported return type", "ret");
                case OctaveCore.Octave.FevalStatus.BadAlloc:
                    throw new OutOfMemoryException("Octave failed to allocate the required memory");
                case OctaveCore.Octave.FevalStatus.Interupted:
                    throw new OctaveRuntimeException("Octave interpreter was interrupted");
                default:
                    return false;
            }
        }
        // Called when a method is called
        public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
        {
            for (int i = 0; i < args.Length; i++)
                args[i] = Unwrap(args[i]);
            var csharpBinder = binder.GetType().GetTypeInfo().GetInterface("Microsoft.CSharp.RuntimeBinder.ICSharpInvokeOrInvokeMemberBinder");
            var typeArgs = (csharpBinder.GetTypeInfo().GetProperty("TypeArguments").GetValue(binder, null) as IList<Type>);
            result = InvokeMethodOnType(TargetType, Instance, binder.Name, args, typeArgs);

            // Wrap the sub object if necessary. This allows nested anonymous objects to work.
            result = result.AsDynamic();

            return true;
        }
Пример #5
0
            /// <summary>
            ///     Provides the implementation for operations that invoke a member. Classes derived from the
            ///     <see
            ///         cref="T:System.Dynamic.DynamicObject" />
            ///     class can override this method to specify dynamic behavior for operations such as calling a method.
            /// </summary>
            /// <returns>
            ///     true if the operation is successful; otherwise, false. If this method returns false, the run-time binder of the language determines the behavior. (In most cases, a language-specific run-time exception is thrown.)
            /// </returns>
            /// <param name="binder">
            ///     Provides information about the dynamic operation. The binder.Name property provides the name of the member on which the dynamic operation is performed. For example, for the statement sampleObject.SampleMethod(100), where sampleObject is an instance of the class derived from the
            ///     <see
            ///         cref="T:System.Dynamic.DynamicObject" />
            ///     class, binder.Name returns "SampleMethod". The binder.IgnoreCase property specifies whether the member name is case-sensitive.
            /// </param>
            /// <param name="args">
            ///     The arguments that are passed to the object member during the invoke operation. For example, for the statement sampleObject.SampleMethod(100), where sampleObject is derived from the <see cref="T:System.Dynamic.DynamicObject" /> class, <paramref name="args" /> is equal to 100.
            /// </param>
            /// <param name="result">The result of the member invocation.</param>
            public override bool TryInvokeMember(
                InvokeMemberBinder binder,
                object[] args,
                out object result)
            {
                var method = targetType.GetMethod(binder.Name,
                                                  BindingFlags.FlattenHierarchy |
                                                  BindingFlags.Public |
                                                  BindingFlags.Static);

                if (method == null)
                {
                    result = null;
                    return(false);
                }

                if (binder.CallInfo.ArgumentNames.Any() &&
                    binder.CallInfo.ArgumentNames.First() == "of")
                {
                    var type = args.First() as Type;
                    if (type != null)
                    {
                        method = method.MakeGenericMethod(new[] { type });
                        args   = args.Skip(1).ToArray();
                    }
                }
                else
                {
                    var csharpBinder     = binder.GetType().GetInterface("Microsoft.CSharp.RuntimeBinder.ICSharpInvokeOrInvokeMemberBinder");
                    var genericArguments = (csharpBinder.GetProperty("TypeArguments").GetValue(binder, null) as IEnumerable <Type>);

                    if (genericArguments != null && genericArguments.Any())
                    {
                        method = method.MakeGenericMethod(genericArguments.ToArray());
                    }
                }

                result = method.Invoke(null, args);
                return(true);
            }
Пример #6
0
            /// <summary>
            ///     Provides the implementation for operations that invoke a member. Classes derived from the
            ///     <see
            ///         cref="T:System.Dynamic.DynamicObject" />
            ///     class can override this method to specify dynamic behavior for operations such as calling a method.
            /// </summary>
            /// <returns>
            ///     true if the operation is successful; otherwise, false. If this method returns false, the run-time binder of the language determines the behavior. (In most cases, a language-specific run-time exception is thrown.)
            /// </returns>
            /// <param name="binder">
            ///     Provides information about the dynamic operation. The binder.Name property provides the name of the member on which the dynamic operation is performed. For example, for the statement sampleObject.SampleMethod(100), where sampleObject is an instance of the class derived from the
            ///     <see
            ///         cref="T:System.Dynamic.DynamicObject" />
            ///     class, binder.Name returns "SampleMethod". The binder.IgnoreCase property specifies whether the member name is case-sensitive.
            /// </param>
            /// <param name="args">
            ///     The arguments that are passed to the object member during the invoke operation. For example, for the statement sampleObject.SampleMethod(100), where sampleObject is derived from the <see cref="T:System.Dynamic.DynamicObject" /> class, <paramref name="args" /> is equal to 100.
            /// </param>
            /// <param name="result">The result of the member invocation.</param>
            public override bool TryInvokeMember(
                InvokeMemberBinder binder,
                object[] args,
                out object result)
            {
                var method = targetType.GetMethod(binder.Name,
                                                  BindingFlags.FlattenHierarchy |
                                                  BindingFlags.Public |
                                                  BindingFlags.Static);
                if (method == null)
                {
                    result = null;
                    return false;
                }

                if (binder.CallInfo.ArgumentNames.Any() &&
                    binder.CallInfo.ArgumentNames.First() == "of")
                {
                    var type = args.First() as Type;
                    if (type != null)
                    {
                        method = method.MakeGenericMethod(new[] { type });
                        args = args.Skip(1).ToArray();
                    }
                }
                else
                {
                    var csharpBinder = binder.GetType().GetInterface("Microsoft.CSharp.RuntimeBinder.ICSharpInvokeOrInvokeMemberBinder");
                    var genericArguments = (csharpBinder.GetProperty("TypeArguments").GetValue(binder, null) as IEnumerable<Type>);

                    if (genericArguments != null && genericArguments.Any())
                    {
                        method = method.MakeGenericMethod(genericArguments.ToArray());
                    }
                }

                result = method.Invoke(null, args);
                return true;
            }
Пример #7
0
 static IList<Type> GenericArguments(InvokeMemberBinder binder)
 {
     //TOOD: http://stackoverflow.com/questions/5492373/get-generic-type-of-call-to-method-in-dynamic-object
     var csharpBinder = binder.GetType().GetInterface("Microsoft.CSharp.RuntimeBinder.ICSharpInvokeOrInvokeMemberBinder");
     return csharpBinder.GetProperty("TypeArguments").GetValue(binder, null) as IList<Type> ?? new List<Type>();
 }
Пример #8
0
Файл: Mock.cs Проект: eugman/Oak
        private Type[] TypeArguments(InvokeMemberBinder binder)
        {
            var csharpBinder = binder.GetType().GetInterface("Microsoft.CSharp.RuntimeBinder.ICSharpInvokeOrInvokeMemberBinder");

            return (csharpBinder.GetProperty("TypeArguments").GetValue(binder, null) as IList<Type>).ToArray();
        }