Ejemplo n.º 1
0
        private void Initialize()
        {
            if (_type == null || (current != null && current.GetType() != _type))
            {
                _type = current.GetType();
            }

            if (_typeMemberSelector != null)
            {
                _memberNames = _typeMemberSelector(_type);
            }

            var allMembers = _memberNames == null || _memberNames.Length == 0;

            accessor = TypeAccessor.Create(_type);
            if (accessor.GetMembersSupported)
            {
                var typeMembers = accessor.GetMembers();

                if (allMembers)
                {
                    _memberNames = new string[typeMembers.Count];
                    for (var i = 0; i < _memberNames.Length; i++)
                    {
                        _memberNames[i] = typeMembers[i].Name;
                    }
                }

                allowNull      = new BitArray(_memberNames.Length);
                effectiveTypes = new Type[_memberNames.Length];
                for (var i = 0; i < _memberNames.Length; i++)
                {
                    Type memberType = null;
                    var  iAllowNull = true;
                    var  hunt       = _memberNames[i];
                    foreach (var member in typeMembers)
                    {
                        if (member.Name == hunt)
                        {
                            if (memberType == null)
                            {
                                var tmp = member.Type;
                                memberType = Nullable.GetUnderlyingType(tmp) ?? tmp;

                                iAllowNull = !(TypeHelpers._IsValueType(memberType) && memberType == tmp);

                                // but keep checking, in case of duplicates
                            }
                            else
                            {
                                memberType = null; // duplicate found; say nothing
                                break;
                            }
                        }
                    }
                    allowNull[i]      = iAllowNull;
                    effectiveTypes[i] = memberType ?? typeof(object);
                }
            }
            else if (allMembers)
            {
                throw new InvalidOperationException(
                          "Member information is not available for this type; the required members must be specified explicitly");
            }

            //current = null;
            _isInitialized = true;
        }