public sealed override Object Invoke(Object thisObject, Object[] arguments)
        {
            switch (_id)
            {
            case StringConstructorId.CharArray:
            {
                CheckArgumentCount(arguments, 1);
                char[] value = (char[])(RuntimeAugments.CheckArgument(arguments[0], typeof(char[]).TypeHandle));
                return(new String(value));
            }

            case StringConstructorId.Char_Int:
            {
                CheckArgumentCount(arguments, 2);
                char c     = (char)(RuntimeAugments.CheckArgument(arguments[0], typeof(char).TypeHandle));
                int  count = (int)(RuntimeAugments.CheckArgument(arguments[1], typeof(int).TypeHandle));
                return(new String(c, count));
            }

            case StringConstructorId.CharArray_Int_Int:
            {
                CheckArgumentCount(arguments, 3);
                char[] value      = (char[])(RuntimeAugments.CheckArgument(arguments[0], typeof(char[]).TypeHandle));
                int    startIndex = (int)(RuntimeAugments.CheckArgument(arguments[1], typeof(int).TypeHandle));
                int    length     = (int)(RuntimeAugments.CheckArgument(arguments[2], typeof(int).TypeHandle));
                return(new String(value, startIndex, length));
            }

            default:
                throw new InvalidOperationException();
            }
        }
Example #2
0
        protected sealed override object?Invoke(object?thisObject, object?[] arguments, BinderBundle binderBundle, bool wrapInTargetInvocationException)
        {
            Debug.Assert(arguments != null);

            // This does not handle optional parameters. None of the methods we use custom invocation for have them.
            if (!(thisObject == null && 0 != (_options & InvokerOptions.AllowNullThis)))
            {
                ValidateThis(thisObject, _thisType.TypeHandle);
            }

            if (arguments.Length != _parameterTypes.Length)
            {
                throw new TargetParameterCountException();
            }

            object[] convertedArguments = new object[arguments.Length];
            for (int i = 0; i < arguments.Length; i++)
            {
                convertedArguments[i] = RuntimeAugments.CheckArgument(arguments[i], _parameterTypes[i].TypeHandle, binderBundle);
            }
            object result;

            try
            {
                result = _action(thisObject, convertedArguments, _thisType);
            }
            catch (Exception e) when(wrapInTargetInvocationException)
            {
                throw new TargetInvocationException(e);
            }
            return(result);
        }
Example #3
0
        protected sealed override void SetFieldBypassCctor(Object obj, Object value)
        {
            value = RuntimeAugments.CheckArgument(value, FieldTypeHandle);
            IntPtr fieldAddress = RuntimeAugments.GetThreadStaticFieldAddress(_declaringTypeHandle, _cookie);

            RuntimeAugments.StoreValueTypeField(fieldAddress, value, FieldTypeHandle);
        }
Example #4
0
        protected sealed override void SetFieldBypassCctor(Object obj, Object value, BinderBundle binderBundle)
        {
            value = RuntimeAugments.CheckArgument(value, FieldTypeHandle, binderBundle);
            IntPtr tlsFieldsStartAddress = RuntimeAugments.GetThreadStaticFieldAddress(_declaringTypeHandle, IntPtr.Zero);
            IntPtr fieldAddress          = tlsFieldsStartAddress + _fieldOffset;

            RuntimeAugments.StoreReferenceTypeField(fieldAddress, value);
        }
        protected sealed override void SetFieldBypassCctor(object value, BinderBundle binderBundle)
        {
            value = RuntimeAugments.CheckArgument(value, FieldTypeHandle, binderBundle);

            if (IsFieldInitOnly)
            {
                throw new FieldAccessException(SR.Acc_InitOnlyStatic);
            }

            UncheckedSetFieldBypassCctor(value);
        }
 public sealed override void SetField(Object obj, Object value, BinderBundle binderBundle)
 {
     if (obj == null)
     {
         throw new TargetException(SR.RFLCT_Targ_StatFldReqTarg);
     }
     if (!RuntimeAugments.IsAssignable(obj, this.DeclaringTypeHandle))
     {
         throw new ArgumentException();
     }
     value = RuntimeAugments.CheckArgument(value, this.FieldTypeHandle, binderBundle);
     UncheckedSetField(obj, value);
 }
Example #7
0
        public override Object Invoke(Object thisObject, Object[] arguments, BinderBundle binderBundle)
        {
            //@todo: This does not handle optional parameters (nor does it need to as today we're only using it for three synthetic array methods.)
            if (!(thisObject == null && 0 != (_options & InvokerOptions.AllowNullThis)))
            {
                MethodInvokerUtils.ValidateThis(thisObject, _thisType);
            }
            if (arguments == null)
            {
                arguments = Array.Empty <Object>();
            }
            if (arguments.Length != _parameterTypes.Length)
            {
                throw new TargetParameterCountException();
            }
            Object[] convertedArguments = new Object[arguments.Length];
            for (int i = 0; i < arguments.Length; i++)
            {
                convertedArguments[i] = RuntimeAugments.CheckArgument(arguments[i], _parameterTypes[i], binderBundle);
            }
            Object result;

            try
            {
                result = _invoker(thisObject, convertedArguments);
            }
            catch (Exception e)
            {
                if (0 != (_options & InvokerOptions.DontWrapException))
                {
                    throw;
                }
                else
                {
                    throw new TargetInvocationException(e);
                }
            }
            return(result);
        }
Example #8
0
        public sealed override Object Invoke(Object thisObject, Object[] arguments)
        {
            Object value    = thisObject;
            bool   hasValue = (thisObject != null);

            switch (_id)
            {
            case NullableMethodId.GetType:
                CheckArgumentCount(arguments, 0);
                return(value.GetType());    // Note: this throws a NullReferenceException if hasValue is false. Well so does the desktop.

            case NullableMethodId.ToString:
                CheckArgumentCount(arguments, 0);
                return(hasValue ? value.ToString() : "");

            case NullableMethodId.Equals:
            {
                CheckArgumentCount(arguments, 1);
                Object other = arguments[0];
                if (!hasValue)
                {
                    return(other == null);
                }
                if (other == null)
                {
                    return(false);
                }
                return(value.Equals(other));
            }

            case NullableMethodId.GetHashCode:
                CheckArgumentCount(arguments, 0);
                return(hasValue ? value.GetHashCode() : 0);

            case NullableMethodId.Ctor:
            {
                // Constructor case is tricky. Our implementation of NewObject() does not accept Nullable<T>'s so this is one of those cases
                // where the constructor is responsible for both the allocation and initialization. Fortunately, we only have to return the boxed
                // version of Nullable<T> which conveniently happens to be equal to the value we were passed in.
                CheckArgumentCount(arguments, 1);
                RuntimeTypeHandle theT     = RuntimeAugments.GetNullableType(_nullableTypeHandle);
                Object            argument = RuntimeAugments.CheckArgument(arguments[0], theT);
                return(argument);
            }

            case NullableMethodId.get_HasValue:
                CheckArgumentCount(arguments, 0);
                return(hasValue);

            case NullableMethodId.get_Value:
                CheckArgumentCount(arguments, 0);
                if (!hasValue)
                {
                    throw new InvalidOperationException(SR.InvalidOperation_NoValue);
                }
                return(value);

            case NullableMethodId.GetValueOrDefault_0:
            {
                CheckArgumentCount(arguments, 0);
                if (hasValue)
                {
                    return(value);
                }
                RuntimeTypeHandle theT = RuntimeAugments.GetNullableType(_nullableTypeHandle);
                return(RuntimeAugments.NewObject(theT));
            }

            case NullableMethodId.GetValueOrDefault_1:
            {
                CheckArgumentCount(arguments, 1);
                RuntimeTypeHandle theT         = RuntimeAugments.GetNullableType(_nullableTypeHandle);
                Object            defaultValue = RuntimeAugments.CheckArgument(arguments[0], theT);
                return(hasValue ? value : defaultValue);
            }

            default:
                throw new InvalidOperationException();
            }
        }
Example #9
0
 protected sealed override void SetFieldBypassCctor(Object obj, Object value, BinderBundle binderBundle)
 {
     value = RuntimeAugments.CheckArgument(value, FieldTypeHandle, binderBundle);
     RuntimeAugments.StoreReferenceTypeField(_fieldAddress, value);
 }
 protected sealed override void SetFieldBypassCctor(object value, BinderBundle binderBundle)
 {
     value = RuntimeAugments.CheckArgument(value, FieldTypeHandle, binderBundle);
     UncheckedSetFieldBypassCctor(value);
 }
Example #11
0
        public sealed override Object Invoke(Object thisObject, Object[] arguments)
        {
            switch (_id)
            {
            case IntPtrConstructorId.Int32:
            {
                CheckArgumentCount(arguments, 1);
                Int32 value = (Int32)(RuntimeAugments.CheckArgument(arguments[0], typeof(Int32).TypeHandle));
                try
                {
                    return(new IntPtr(value));
                }
                catch (Exception inner)
                {
                    throw new TargetInvocationException(inner);
                }
            }

            case IntPtrConstructorId.Int64:
            {
                CheckArgumentCount(arguments, 1);
                Int64 value = (Int64)(RuntimeAugments.CheckArgument(arguments[0], typeof(Int64).TypeHandle));
                try
                {
                    return(new IntPtr(value));
                }
                catch (Exception inner)
                {
                    throw new TargetInvocationException(inner);
                }
            }

            case IntPtrConstructorId.UInt32:
            {
                CheckArgumentCount(arguments, 1);
                UInt32 value = (UInt32)(RuntimeAugments.CheckArgument(arguments[0], typeof(UInt32).TypeHandle));
                try
                {
                    return(new UIntPtr(value));
                }
                catch (Exception inner)
                {
                    throw new TargetInvocationException(inner);
                }
            }

            case IntPtrConstructorId.UInt64:
            {
                CheckArgumentCount(arguments, 1);
                UInt64 value = (UInt64)(RuntimeAugments.CheckArgument(arguments[0], typeof(UInt64).TypeHandle));
                try
                {
                    return(new UIntPtr(value));
                }
                catch (Exception inner)
                {
                    throw new TargetInvocationException(inner);
                }
            }

            default:
                throw new InvalidOperationException();
            }
        }