Пример #1
0
        internal static AssignmentDefinition GetAssignmentDefinition(Type toType, Type fromType)
        {
            var cacheKey = CreateCacheKey(fromType, toType);

            return(AssignmentDefinitionCache.GetOrAdd(cacheKey, delegate
            {
                var definition = new AssignmentDefinition
                {
                    ToType = toType,
                    FromType = fromType,
                };

                var readMap = GetMembers(fromType, isReadable: true);
                var writeMap = GetMembers(toType, isReadable: false);

                foreach (var assignmentMember in readMap)
                {
                    if (writeMap.TryGetValue(assignmentMember.Key, out var writeMember))
                    {
                        definition.AddMatch(assignmentMember.Key, assignmentMember.Value, writeMember);
                    }
                }

                return definition;
            }));
Пример #2
0
        internal static AssignmentDefinition GetAssignmentDefinition(Type toType, Type fromType)
        {
            var cacheKey = toType.FullName + "<" + fromType.FullName;


#if NET4
            return(AssignmentDefinitionCache.GetOrAdd(cacheKey, delegate

            {
                var definition = new AssignmentDefinition
                {
                    ToType = toType,
                    FromType = fromType,
                };

                var readMap = GetMembers(fromType, isReadable: true);
                var writeMap = GetMembers(toType, isReadable: false);

                foreach (var assignmentMember in readMap)
                {
                    AssignmentMember writeMember;
                    if (writeMap.TryGetValue(assignmentMember.Key, out writeMember))
                    {
                        definition.AddMatch(assignmentMember.Key, assignmentMember.Value, writeMember);
                    }
                }

                return definition;
            }));
#else
            AssignmentDefinition v;

            lock ( _assignmentDefinitionCache_Locker )
            {
                if (AssignmentDefinitionCache.TryGetValue(cacheKey, out v))
                {
                    return(v);
                }
                else
                {
                    var definition = new AssignmentDefinition
                    {
                        ToType   = toType,
                        FromType = fromType,
                    };

                    var readMap  = GetMembers(fromType, isReadable: true);
                    var writeMap = GetMembers(toType, isReadable: false);

                    foreach (var assignmentMember in readMap)
                    {
                        AssignmentMember writeMember;
                        if (writeMap.TryGetValue(assignmentMember.Key, out writeMember))
                        {
                            definition.AddMatch(assignmentMember.Key, assignmentMember.Value, writeMember);
                        }
                    }

                    return(definition);
                }
            }
#endif
        }