コード例 #1
0
 GetEntityReferenceProperty(this ITrackable entity, PropertyInfo property)
 {
     return(entity.GetEntityReferenceProperty <ITrackable>(property));
 }
コード例 #2
0
        private static void SetEntityProperties(this ITrackable targetItem, ITrackable sourceItem,
                                                ITrackingCollection changeTracker)
        {
            // List of 'prop.SetValue' actions
            var actions = new List <Action>();

            // Iterate simple properties
#if SILVERLIGHT || NET40
            foreach (var prop in targetItem.GetType().GetProperties().Where(p => p.CanWrite)
#else
            foreach (var prop in targetItem.GetType().GetTypeInfo().DeclaredProperties
                     .Where(p => p.CanWrite && !p.GetMethod.IsPrivate)
#endif
                     .Except(targetItem.GetNavigationProperties(false).Select(np => np.Property)))
            {
                // Skip tracking properties
                if (prop.Name == Constants.TrackingProperties.TrackingState ||
                    prop.Name == Constants.TrackingProperties.ModifiedProperties)
                {
                    continue;
                }

                // Get source item prop value
                object sourceValue = prop.GetValue(sourceItem, null);
                object targetValue = prop.GetValue(targetItem, null);

                // Continue if source is null or source and target equal
                if (sourceValue == null || sourceValue.Equals(targetValue))
                {
                    continue;
                }

                // Deferred 'SetValue'
                actions.Add(() => prop.SetValue(targetItem, sourceValue, null));
            }

            // Iterate entity reference properties (skip collections)
            foreach (var refProp in targetItem
                     .GetNavigationProperties(false)
                     .OfReferenceType()
                     .Where(np => np.Property.CanWrite))
            {
                ITrackable targetValue = refProp.EntityReference;

                // Skip non-null trackable
                if (targetValue != null)
                {
                    continue;
                }

                ITrackable sourceValue = sourceItem.GetEntityReferenceProperty(refProp.Property).EntityReference;

                // Continue if source is null
                if (sourceValue == null)
                {
                    continue;
                }

                // Deferred 'SetValue'
                actions.Add(() => refProp.Property.SetValue(targetItem, sourceValue, null));
            }

            // Nothing to do?
            if (!actions.Any())
            {
                return;
            }

            // Turn off change-tracking
            bool isTracking = changeTracker.Tracking;
            changeTracker.Tracking = false;

            // Set target item prop value
            foreach (var action in actions)
            {
                action();
            }

            // Reset change-tracking
            changeTracker.Tracking = isTracking;
        }