예제 #1
0
 /// <summary>
 /// Gets the specified type.
 /// </summary>
 /// <param name="type">The type.</param>
 /// <returns></returns>
 public AccessorList GetProperties(Type type, bool withSubType = false)
 {
     return(AccessorItem.Get(type, withSubType));
 }
        public static bool Map <T>(T self, string arguments, bool respectCase = false)
        {
            var properties = PropertyAccessor.Get(self.GetType(), true);

            var members = arguments.Split(';');

            foreach (var item in members)
            {
                if (string.IsNullOrEmpty(item))
                {
                    continue;
                }

                var ii        = item.Split('=');
                var key       = ii[0].Trim();
                var valueText = string.Join('=', ii.Skip(1));
                if (string.IsNullOrEmpty(valueText))
                {
                    continue;
                }

                AccessorItem property = properties.Get(key, respectCase);

                if (property != null)
                {
                    if ((typeof(string) == property.Type || property.Type.IsValueType) && property.CanWrite)
                    {
                        if (valueText.StartsWith("@"))
                        {
                            var newValueText = Environment.GetEnvironmentVariable(valueText.Substring(1));
                            if (!string.IsNullOrEmpty(newValueText))
                            {
                                valueText = newValueText;
                            }
                            else
                            {
                                Trace.WriteLine($"environment variable {valueText} not found", TraceLevel.Warning.ToString());
                            }
                        }

                        object value = MyConverter.Unserialize(valueText, property.Type);
                        property.SetValue(self, value);
                    }
                    else if (typeof(IEnumerable).IsAssignableFrom(property.Type) && property.Type.IsGenericType)
                    {
                        var p = property.Type.GetGenericArguments();
                        if (p.Length > 1)
                        {
                        }

                        var type = p[0];

                        dynamic value = property.GetValue(self);

                        if (value == null)
                        {
                            if (property.CanWrite)
                            {
                                value = Activator.CreateInstance(property.Type);
                                property.SetValue(self, value);
                            }
                            else
                            {
                                continue;
                            }
                        }

                        var jj = valueText.Split(',');
                        foreach (var item3 in jj)
                        {
                            string subValueText = item3.Trim();

                            if (item3.StartsWith("@"))
                            {
                                var newValueText = Environment.GetEnvironmentVariable(item3.Substring(1));
                                if (!string.IsNullOrEmpty(newValueText))
                                {
                                    subValueText = newValueText;
                                }
                                else
                                {
                                    Trace.WriteLine($"environment variable {item3} not found", TraceLevel.Warning.ToString());
                                }
                            }

                            dynamic value2 = MyConverter.Unserialize(subValueText, type).Trim();
                            value.Add(value2);
                        }
                    }
                }
            }

            bool result = true;

            foreach (var item in properties)
            {
                if (item.Required && item.GetValue(self) == null)
                {
                    Trace.WriteLine($"{item.Name} is required");
                    result = false;
                }
            }

            return(result);
        }