Exemplo n.º 1
0
        public static EnvironmentVariableBaseDto Export(EnvironmentVariableBaseDto environmentVariableBaseDto, EnvironmentVariableTarget target)
        {
            Contract.Requires(null != environmentVariableBaseDto);
            Contract.Requires(Enum.IsDefined(typeof(EnvironmentVariableTarget), target));

            var propertyInfos = environmentVariableBaseDto
                                .GetType()
                                .GetProperties(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.FlattenHierarchy);

            foreach (var propertyInfo in propertyInfos)
            {
                var attribute = (EnvironmentVariableAttribute)Attribute.GetCustomAttribute(propertyInfo, typeof(EnvironmentVariableAttribute));
                if (null == attribute)
                {
                    continue;
                }

                var value = propertyInfo.GetValue(environmentVariableBaseDto, null);
                Environment.SetEnvironmentVariable(attribute.Name, null != value ? value.ToString() : null, target);
            }

            return(environmentVariableBaseDto);
        }
Exemplo n.º 2
0
        public static EnvironmentVariableBaseDto Import(EnvironmentVariableBaseDto environmentVariableBaseDto, EnvironmentVariableTarget target)
        {
            Contract.Requires(null != environmentVariableBaseDto);
            Contract.Requires(Enum.IsDefined(typeof(EnvironmentVariableTarget), target));

            var propertyInfos = environmentVariableBaseDto
                                .GetType()
                                .GetProperties(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.FlattenHierarchy);

            foreach (var propertyInfo in propertyInfos)
            {
                var attribute = (EnvironmentVariableAttribute)Attribute.GetCustomAttribute(propertyInfo, typeof(EnvironmentVariableAttribute));
                if (null == attribute)
                {
                    continue;
                }

                var stringValue = Environment.GetEnvironmentVariable(attribute.Name, target);
                if (null == stringValue)
                {
                    continue;
                }

                try
                {
                    propertyInfo.SetValue(environmentVariableBaseDto, stringValue, null);
                }
                catch (ArgumentException)
                {
                    var changedTypeValue = System.Convert.ChangeType(stringValue, propertyInfo.PropertyType, CultureInfo.InvariantCulture);

                    propertyInfo.SetValue(environmentVariableBaseDto, changedTypeValue, null);
                }
            }

            return(environmentVariableBaseDto);
        }
Exemplo n.º 3
0
 public static EnvironmentVariableBaseDto Export(EnvironmentVariableBaseDto environmentVariableBaseDto)
 {
     return(Export(environmentVariableBaseDto, EnvironmentVariableTarget.Process));
 }
Exemplo n.º 4
0
        public static DictionaryParameters Convert(EnvironmentVariableBaseDto environmentVariableBaseDto, bool includeAllProperties)
        {
            var result = _converter.Convert <EnvironmentVariableAttribute>(environmentVariableBaseDto, includeAllProperties);

            return(result);
        }
Exemplo n.º 5
0
        public static DictionaryParameters Convert(EnvironmentVariableBaseDto environmentVariableBaseDto)
        {
            var result = _converter.Convert <EnvironmentVariableAttribute>(environmentVariableBaseDto, false);

            return(result);
        }