public unsafe static TypedReference MakeTypedReference(object target, FieldInfo[] flds)
        {
            if (target == null)
            {
                throw new ArgumentNullException("target");
            }
            if (flds == null)
            {
                throw new ArgumentNullException("flds");
            }
            if (flds.Length == 0)
            {
                throw new ArgumentException(Environment.GetResourceString("Arg_ArrayZeroError"));
            }
            IntPtr[]    array       = new IntPtr[flds.Length];
            RuntimeType runtimeType = (RuntimeType)target.GetType();

            for (int i = 0; i < flds.Length; i++)
            {
                RuntimeFieldInfo runtimeFieldInfo = flds[i] as RuntimeFieldInfo;
                if (runtimeFieldInfo == null)
                {
                    throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeFieldInfo"));
                }
                if (runtimeFieldInfo.IsInitOnly || runtimeFieldInfo.IsStatic)
                {
                    throw new ArgumentException(Environment.GetResourceString("Argument_TypedReferenceInvalidField"));
                }
                if (runtimeType != runtimeFieldInfo.GetDeclaringTypeInternal() && !runtimeType.IsSubclassOf(runtimeFieldInfo.GetDeclaringTypeInternal()))
                {
                    throw new MissingMemberException(Environment.GetResourceString("MissingMemberTypeRef"));
                }
                RuntimeType runtimeType2 = (RuntimeType)runtimeFieldInfo.FieldType;
                if (runtimeType2.IsPrimitive)
                {
                    throw new ArgumentException(Environment.GetResourceString("Arg_TypeRefPrimitve"));
                }
                if (i < flds.Length - 1 && !runtimeType2.IsValueType)
                {
                    throw new MissingMemberException(Environment.GetResourceString("MissingMemberNestErr"));
                }
                array[i]    = runtimeFieldInfo.FieldHandle.Value;
                runtimeType = runtimeType2;
            }
            TypedReference result = default(TypedReference);

            TypedReference.InternalMakeTypedReference((void *)(&result), target, array, runtimeType);
            return(result);
        }
Exemplo n.º 2
0
        public static unsafe TypedReference MakeTypedReference(object target, FieldInfo[] flds)
        {
            if (target == null)
            {
                throw new ArgumentNullException("target");
            }
            if (flds == null)
            {
                throw new ArgumentNullException("flds");
            }
            if (flds.Length == 0)
            {
                throw new ArgumentException(Environment.GetResourceString("Arg_ArrayZeroError"));
            }
            IntPtr[]    flds1         = new IntPtr[flds.Length];
            RuntimeType lastFieldType = (RuntimeType)target.GetType();

            for (int index = 0; index < flds.Length; ++index)
            {
                RuntimeFieldInfo runtimeFieldInfo = flds[index] as RuntimeFieldInfo;
                if ((FieldInfo)runtimeFieldInfo == (FieldInfo)null)
                {
                    throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeFieldInfo"));
                }
                if (runtimeFieldInfo.IsInitOnly || runtimeFieldInfo.IsStatic)
                {
                    throw new ArgumentException(Environment.GetResourceString("Argument_TypedReferenceInvalidField"));
                }
                if (lastFieldType != runtimeFieldInfo.GetDeclaringTypeInternal() && !lastFieldType.IsSubclassOf((System.Type)runtimeFieldInfo.GetDeclaringTypeInternal()))
                {
                    throw new MissingMemberException(Environment.GetResourceString("MissingMemberTypeRef"));
                }
                RuntimeType runtimeType = (RuntimeType)runtimeFieldInfo.FieldType;
                if (runtimeType.IsPrimitive)
                {
                    throw new ArgumentException(Environment.GetResourceString("Arg_TypeRefPrimitve"));
                }
                if (index < flds.Length - 1 && !runtimeType.IsValueType)
                {
                    throw new MissingMemberException(Environment.GetResourceString("MissingMemberNestErr"));
                }
                flds1[index]  = runtimeFieldInfo.FieldHandle.Value;
                lastFieldType = runtimeType;
            }
            TypedReference typedReference = new TypedReference();

            TypedReference.InternalMakeTypedReference((void *)&typedReference, target, flds1, lastFieldType);
            return(typedReference);
        }
Exemplo n.º 3
0
        public static TypedReference MakeTypedReference(object target, FieldInfo[] flds)
        {
            if (target == null)
            {
                throw new ArgumentNullException(nameof(target));
            }
            if (flds == null)
            {
                throw new ArgumentNullException(nameof(flds));
            }
            if (flds.Length == 0)
            {
                throw new ArgumentException(SR.Arg_ArrayZeroError, nameof(flds));
            }

            IntPtr[] fields = new IntPtr[flds.Length];
            // For proper handling of Nullable<T> don't change GetType() to something like 'IsAssignableFrom'
            // Currently we can't make a TypedReference to fields of Nullable<T>, which is fine.
            RuntimeType targetType = (RuntimeType)target.GetType();

            for (int i = 0; i < flds.Length; i++)
            {
                RuntimeFieldInfo field = flds[i] as RuntimeFieldInfo;
                if (field == null)
                {
                    throw new ArgumentException(SR.Argument_MustBeRuntimeFieldInfo);
                }

                if (field.IsStatic)
                {
                    throw new ArgumentException(SR.Format(SR.Argument_TypedReferenceInvalidField, field.Name));
                }

                if (targetType != field.GetDeclaringTypeInternal() && !targetType.IsSubclassOf(field.GetDeclaringTypeInternal()))
                {
                    throw new MissingMemberException(SR.MissingMemberTypeRef);
                }

                RuntimeType fieldType = (RuntimeType)field.FieldType;
                if (fieldType.IsPrimitive)
                {
                    throw new ArgumentException(SR.Format(SR.Arg_TypeRefPrimitve, field.Name));
                }

                if (i < (flds.Length - 1) && !fieldType.IsValueType)
                {
                    throw new MissingMemberException(SR.MissingMemberNestErr);
                }

                fields[i]  = field.FieldHandle.Value;
                targetType = fieldType;
            }

            TypedReference result = new TypedReference();

            // reference to TypedReference is banned, so have to pass result as pointer
            unsafe
            {
                InternalMakeTypedReference(&result, target, fields, targetType);
            }
            return(result);
        }
Exemplo n.º 4
0
        public static TypedReference MakeTypedReference(Object target, FieldInfo[] flds)
        {
            if (target == null)
            {
                throw new ArgumentNullException("target");
            }
            if (flds == null)
            {
                throw new ArgumentNullException("flds");
            }
            Contract.EndContractBlock();
            if (flds.Length == 0)
            {
                throw new ArgumentException(Environment.GetResourceString("Arg_ArrayZeroError"));
            }

            IntPtr[] fields = new IntPtr[flds.Length];
            // For proper handling of Nullable<T> don't change GetType() to something like 'IsAssignableFrom'
            // Currently we can't make a TypedReference to fields of Nullable<T>, which is fine.
            RuntimeType targetType = (RuntimeType)target.GetType();

            for (int i = 0; i < flds.Length; i++)
            {
                RuntimeFieldInfo field = flds[i] as RuntimeFieldInfo;
                if (field == null)
                {
                    throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeFieldInfo"));
                }

                if (field.IsInitOnly || field.IsStatic)
                {
                    throw new ArgumentException(Environment.GetResourceString("Argument_TypedReferenceInvalidField"));
                }

                if (targetType != field.GetDeclaringTypeInternal() && !targetType.IsSubclassOf(field.GetDeclaringTypeInternal()))
                {
                    throw new MissingMemberException(Environment.GetResourceString("MissingMemberTypeRef"));
                }

                RuntimeType fieldType = (RuntimeType)field.FieldType;
                if (fieldType.IsPrimitive)
                {
                    throw new ArgumentException(Environment.GetResourceString("Arg_TypeRefPrimitve"));
                }

                if (i < (flds.Length - 1) && !fieldType.IsValueType)
                {
                    throw new MissingMemberException(Environment.GetResourceString("MissingMemberNestErr"));
                }

                fields[i]  = field.FieldHandle.Value;
                targetType = fieldType;
            }

#if MONO
            return(MakeTypedReferenceInternal(target, flds));
#else
            TypedReference result = new TypedReference();

            // reference to TypedReference is banned, so have to pass result as pointer
            unsafe
            {
                InternalMakeTypedReference(&result, target, fields, targetType);
            }
            return(result);
#endif
        }