コード例 #1
0
ファイル: ResolverCache.cs プロジェクト: lgh7010/projectcc
        /// <summary>
        /// Builds a mapping of member name to field/property
        /// </summary>
        /// <param name="objectType"></param>
        private IEnumerable <DataName> BuildMap(Type objectType, out IDictionary <string, MemberMap> maps)
        {
            bool hasName = false;
            IEnumerable <DataName> typeNames = this.Strategy.GetName(objectType);

            if (typeNames != null)
            {
                foreach (DataName typeName in typeNames)
                {
                    if (!typeName.IsEmpty)
                    {
                        hasName = true;
                        break;
                    }
                }
            }
            if (!hasName)
            {
                typeNames = new [] { new DataName(objectType) };
            }

            if (objectType.IsEnum)
            {
                // create special maps for enum types
                IDictionary <Enum, string> enumMap;
                maps = this.BuildEnumMap(objectType, out enumMap);
                return(typeNames);
            }

            // do not incurr the cost of member map for dictionaries
            if (typeof(IDictionary <string, object>).IsAssignableFrom(objectType) ||
                typeof(IDictionary).IsAssignableFrom(objectType))
            {
#if SILVERLIGHT && (NET20 || NET30 || NET35)
                lock (this.MemberCache)
                    lock (this.NameCache)
#elif NET20 || NET30
                this.MapLock.AcquireWriterLock(ResolverCache.LockTimeout);
#elif NET35
                this.MapLock.EnterWriteLock();
#endif
#if !NET40
                        try
#endif
                    {
                        // store marker in cache for future lookups
                        maps = (this.MemberCache[objectType] = null);
                        return(this.NameCache[objectType] = typeNames);
                    }
#if !NET40
                finally
#endif
                {
#if SILVERLIGHT && (NET20 || NET30 || NET35)
                    // noop
#elif NET20 || NET30
                    this.MapLock.ReleaseWriterLock();
#elif NET35
                    this.MapLock.ExitWriteLock();
#endif
                }
            }

            // create new mapping
            maps = new Dictionary <string, MemberMap>();

            bool isImmutableType = FactoryMap.IsImmutableType(objectType);

            // load properties into property map
            foreach (PropertyInfo info in objectType.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy))
            {
                // Ignore indexer properties as aren't serialized
                // https://bugzilla.xamarin.com/show_bug.cgi?id=6821#c5
                if (info.GetIndexParameters().Length != 0 ||
                    this.Strategy.IsPropertyIgnored(info, isImmutableType))
                {
                    continue;
                }

                IEnumerable <DataName> names = this.Strategy.GetName(info);
                hasName = false;
                if (names != null)
                {
                    foreach (DataName name in names)
                    {
                        if (!name.IsEmpty)
                        {
                            hasName = true;
                            break;
                        }
                    }
                }
                if (!hasName)
                {
                    names = new[] { new DataName(info.Name) };
                }

                ValueIgnoredDelegate isIgnored = this.Strategy.GetValueIgnoredCallback(info);
                MemberMap            map       = null;

                foreach (DataName name in names)
                {
                    if (name.IsEmpty || maps.ContainsKey(name.LocalName))
                    {
                        continue;
                    }

                    if (map == null)
                    {
                        maps[name.LocalName] = map = new MemberMap(info, name, isIgnored);
                    }
                    else
                    {
                        maps[name.LocalName] = new MemberMap(map, name);
                    }
                }
            }

            // load fields into property map
            foreach (FieldInfo info in objectType.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy))
            {
                if (this.Strategy.IsFieldIgnored(info))
                {
                    continue;
                }

                IEnumerable <DataName> names = this.Strategy.GetName(info);
                hasName = false;
                if (names != null)
                {
                    foreach (DataName name in names)
                    {
                        if (!name.IsEmpty)
                        {
                            hasName = true;
                            break;
                        }
                    }
                }
                if (!hasName)
                {
                    names = new[] { new DataName(info.Name) };
                }

                ValueIgnoredDelegate isIgnored = this.Strategy.GetValueIgnoredCallback(info);
                MemberMap            map       = null;

                foreach (DataName name in names)
                {
                    if (name.IsEmpty || maps.ContainsKey(name.LocalName))
                    {
                        continue;
                    }

                    if (map == null)
                    {
                        maps[name.LocalName] = map = new MemberMap(info, name, isIgnored);
                    }
                    else
                    {
                        maps[name.LocalName] = new MemberMap(map, name);
                    }
                }
            }

#if SILVERLIGHT && (NET20 || NET30 || NET35)
            lock (this.MemberCache)
                lock (this.NameCache)
#elif NET20 || NET30
            this.MapLock.AcquireWriterLock(ResolverCache.LockTimeout);
#elif NET35
            this.MapLock.EnterWriteLock();
#endif
#if !NET40
                    try
#endif
                {
                    // store in cache for future requests
                    this.MemberCache[objectType] = maps;
                    return(this.NameCache[objectType] = typeNames);
                }
#if !NET40
            finally
#endif
            {
#if SILVERLIGHT && (NET20 || NET30 || NET35)
                // noop
#elif NET20 || NET30
                this.MapLock.ReleaseWriterLock();
#elif NET35
                this.MapLock.ExitWriteLock();
#endif
            }
        }