Exemple #1
0
        private static object GetPropertyPathValue(object source, string[] path, int index, bool createIfNull)
        {
            if (index >= path.Length)
            {
                return(source);
            }
            PropertyInfo pi            = source.GetType().GetProperty(path[index]);
            object       propertyValue = pi.GetValue(source, null);

            if (propertyValue == null && createIfNull && !UnitTestsUtil.IsPrimitiveType(pi.PropertyType))
            {
                propertyValue = Activator.CreateInstance(pi.PropertyType);
                pi.SetValue(source, propertyValue, null);
            }

            return(GetPropertyPathValue(propertyValue, path, index + 1, createIfNull));
        }
Exemple #2
0
        private static void SetPropertyPathValue(object source, string[] path, int index, object value)
        {
            if (index >= path.Length)
            {
                return;
            }
            PropertyInfo pi = source.GetType().GetProperty(path[index]);

            if (UnitTestsUtil.IsPrimitiveType(pi.PropertyType))
            {
                pi.SetValue(source, value, null);
            }
            else
            {
                object propertyValue = pi.GetValue(source, null);
                if (propertyValue == null)
                {
                    propertyValue = Activator.CreateInstance(pi.PropertyType);
                    pi.SetValue(source, propertyValue, null);
                }
                SetPropertyPathValue(propertyValue, path, index + 1, value);
            }
        }