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);
        }
示例#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);
        }