public static AssignmentDefinition GetAssignmentDefinition(Type toType, Type fromType) { var cacheKey = toType.FullName + "<" + fromType.FullName; 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; })); }
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, true); var writeMap = GetMembers(toType, false); foreach (var assignmentMember in readMap) { if (writeMap.TryGetValue(assignmentMember.Key, out var writeMember)) { definition.AddMatch(assignmentMember.Key, assignmentMember.Value, writeMember); } } return definition; }));
public static AssignmentDefinition GetAssignmentDefinition(Type toType, Type fromType) { var cacheKey = toType.FullName + "<" + fromType.FullName; lock (AssignmentDefinitionCache) { AssignmentDefinition definition; if (AssignmentDefinitionCache.TryGetValue(cacheKey, out definition)) { return(definition); } definition = new AssignmentDefinition { ToType = toType, FromType = fromType, }; var members = fromType.GetMembers(BindingFlags.Public | BindingFlags.Instance); foreach (var info in members) { var fromPropertyInfo = info as PropertyInfo; if (fromPropertyInfo != null) { var toPropertyInfo = GetPropertyInfo(toType, fromPropertyInfo.Name); if (toPropertyInfo == null) { continue; } if (!fromPropertyInfo.CanRead) { continue; } if (!toPropertyInfo.CanWrite) { continue; } definition.AddMatch(fromPropertyInfo, toPropertyInfo); } var fromFieldInfo = info as FieldInfo; if (fromFieldInfo != null) { var toFieldInfo = GetFieldInfo(toType, fromFieldInfo.Name); if (toFieldInfo == null) { continue; } definition.AddMatch(fromFieldInfo, toFieldInfo); } } AssignmentDefinitionCache[cacheKey] = definition; return(definition); } }