예제 #1
0
        public void GetField(Type type, string line)
        {
            var obj = Activator.CreateInstance(type);

            PropertyInfo[] props = type.GetProperties();
            foreach (PropertyInfo prop in props)
            {
                var att =
                    prop.GetCustomAttribute(typeof(StringRangeAttribute)) as StringRangeAttribute;
                if (att == null || (att.StartIndex + att.Length) > line.Length)
                {
                    continue;
                }

                string valueStr = line.Substring(att.StartIndex, att.Length).Trim();
                if (string.IsNullOrEmpty(valueStr))
                {
                    prop.SetValue(type, null);
                    continue;
                }
                if (prop.PropertyType == typeof(decimal) || prop.PropertyType == typeof(float))
                {
                    valueStr = valueStr.Insert(valueStr.Length - 2, ".");
                }

                if (prop.PropertyType.IsEnum)
                {
                    valueStr = GetEnum(prop.PropertyType, valueStr).ToString();
                }

                TypeConverter typeConverter = TypeDescriptor.GetConverter(prop.PropertyType);
                var           value         = typeConverter.ConvertFromString(valueStr);
                prop.SetValue(obj, value);
            }

            FieldEvent?.Invoke(this, new FieldEventArgs(obj));
        }
예제 #2
0
파일: Field.cs 프로젝트: nobnak/FieldLayout
 public void NotifyValidated(Field self)
 {
     EventValidated?.Invoke(self);
     Validated.Invoke(self);
 }