protected static Action <object> GetObjectPropertySetter(object target, string property, bool?throwExcepctionOnNotFound = null)
        {
            PropertyInfo targetPropertyInfo = ObjectPropertyHelper.GetPropertyInfoSetter(target, property);

            targetPropertyInfo = targetPropertyInfo ?? ObjectPropertyHelper.GetPropertyInfo(target, property,
                                                                                            BindingFlags.Public | BindingFlags.SetProperty | BindingFlags.NonPublic | BindingFlags.Static);
            if (targetPropertyInfo != null)
            {
                return(x => targetPropertyInfo.SetValue(target, x, null));
            }

            DependencyProperty targetDependencyProperty = ObjectPropertyHelper.GetDependencyProperty(target, property);

            if (targetDependencyProperty != null)
            {
                return(x => ((DependencyObject)target).SetValue(targetDependencyProperty, x));
            }

            if (throwExcepctionOnNotFound.HasValue)
            {
                string message = string.Format(Error_PropertyNotFound, target.GetType().Name, property);
                if (throwExcepctionOnNotFound.Value)
                {
                    throw new ArgumentException(message);
                }
                else
                {
                    System.Diagnostics.Trace.WriteLine(string.Format("MethodBindingBehaviorBase error: {0}", message));
                }
            }

            return(null);
        }
        private string EncodeParameters()
        {
            var comps = new List <string>();

            foreach (var info in GetType().GetProperties())
            {
                string propName = info.Name;
                if (propName != "State")
                {
                    comps.Add(string.Format("{0}={1}", propName,
                                            ObjectPropertyHelper.GetPropertyValue(this, propName)));
                }
            }
            return(string.Join("_", comps.ToArray()));
        }
Exemple #3
0
        /// <summary>
        /// Copies the non-null property values from one object to another.
        /// </summary>
        /// <param name="source">Source object for the property values.</param>
        /// <param name="destination">Destination object for the property values.</param>
        public static void CopyProperties(object source, object destination)
        {
            var sourceType      = source?.GetType();
            var destinationType = destination?.GetType();

            if (sourceType == null || destinationType == null || sourceType != destinationType)
            {
                throw new InvalidOperationException("Cannot copy properties from or to null objects, and when the object types are different.");
            }

            var properties = ObjectPropertyHelper.GetProperties(sourceType);

            foreach (var property in properties.PropertyDelegates)
            {
                var originalValue = property.Getter(source);
                if (originalValue != null)
                {
                    property.Setter(destination, originalValue);
                }
            }
        }
 DependencyProperty GetCommandDependencyProperty(string propName)
 {
     return(ObjectPropertyHelper.GetDependencyProperty(AssociatedObject, propName));
 }
 PropertyInfo GetCommandProperty(string propName)
 {
     return(ObjectPropertyHelper.GetPropertyInfoSetter(AssociatedObject, propName));
 }