private bool TryGetSettingValue(PropertyInfo propInfo, Type returnType, out object settingValue, bool isInBaseType = false) { if (!string.IsNullOrEmpty(ConnString)) { kvMap ??= Init(ConnString); var idxAttribute = propInfo.GetCustomAttribute <IndexingAttribute>(); var key = (idxAttribute == null) ? propInfo.Name : idxAttribute.Id; string value; if (kvMap.TryGetValue(key, out value)) { settingValue = (returnType.Equals(typeof(string))) ? value : CommonExtension.ToObject(value, returnType); return(true); } else if (!isInBaseType) { Func <Type, PropertyInfo> cond = type => (!propInfo.DeclaringType.IsInterface) ? null : type.GetProperty(propInfo.Name); propInfo = propInfo.DeclaringType.FindIncludBaseType <PropertyInfo>(cond); if (propInfo != null) { return(TryGetSettingValue(propInfo, returnType, out settingValue, true)); } } } settingValue = null; return(false); }
public override object Deserialize(Stream stream, Type type) { var list = (IList)Activator.CreateInstance(type); var typeArgument = type.GetGenericArguments()[0]; var indexingAttributes = TakeIndexingAttributesByIdx(typeArgument); bool isAllHeaderMatchPropertyName = false; var head = new List <string>(); using (var reader = new StreamReader(stream, TheEncoding)) { while (!reader.EndOfStream) { var line = reader.ReadLine(); if (string.IsNullOrEmpty(line)) { continue; } if (head.Count == 0) { if (!line.Contains(symbol)) { continue; } line.Split(str => { head.Add(str); return(true); }, symbol); isAllHeaderMatchPropertyName = head.TrueForAll(it => typeArgument.GetProperty(it) != null); if (!isAllHeaderMatchPropertyName && indexingAttributes == null) { throw new FieldAccessException("Deserialize: CSV Columns not match object propertys!"); } continue; } if (LineStrinCheckEvent != null && !LineStrinCheckEvent(line)) { continue; } if (LineStrinReplaceEvent != null) { line = LineStrinReplaceEvent(line); } var item = (CreateInstanceEvent != null) ? CreateInstanceEvent() : Activator.CreateInstance(typeArgument); var values = new List <object>(); line.Split(str => { values.Add(str); return(true); }, symbol); int colNum = (!isAllHeaderMatchPropertyName) ? indexingAttributes.Count : head.Count; for (int i = 0; i < colNum; i++) { var protertyInfo = (!isAllHeaderMatchPropertyName) ? indexingAttributes[i].OriginalPropertyInfo : typeArgument.GetProperty(head[i]); if (protertyInfo != null && i < values.Count) { protertyInfo.SetValue(item, CommonExtension.ToObject(values[i], protertyInfo.PropertyType)); } } list.Add(item); } return(list); } }
internal object DoGetProperty(System.Reflection.MethodInfo methodInfo, string propertyName) { TValue value; if (ValueMap.TryGetValue(propertyName, out value)) { return((methodInfo.ReturnType.Equals(typeof(string))) ?value:CommonExtension.ToObject(value, methodInfo.ReturnType)); } return(value); }
override public void Initializing() { if (string.IsNullOrEmpty(this.Content)) { return; } items.Clear(); Match match = priceRegex.Match(this.Content); if (match.Success) { foreach (Capture item in match.Groups[2].Captures) { items.Add(CommonExtension.ToObject <T>(item.Value)); } } }
static private object DoGetPropertyValue(MethodInfo methodInfo, object retValue) { if (methodInfo == null) { return(retValue); } var propertyType = methodInfo.ReturnType; if (retValue == null || retValue is DBNull) { retValue = (propertyType.IsValueType) ? Activator.CreateInstance(propertyType) : null; } if (retValue == null || propertyType.IsAssignableFrom(retValue.GetType())) { return(retValue); } return(CommonExtension.ToObject(retValue.ToString(), propertyType)); }
protected object DoGetPropertyValue(MethodInfo methodInfo, string propertyName) { var retValue = Row[propertyName]; if (methodInfo == null) { return(retValue); } var propertyType = methodInfo.ReturnType; if (typeof(IConverterField).IsAssignableFrom(propertyType)) { //當Column.Expression!="" ,才會於此進入此code做Field value轉換 if (fieldMap == null) { fieldMap = new Dictionary <string, IConverterField>(); } IConverterField converterField; if (!fieldMap.TryGetValue(propertyName, out converterField)) { converterField = (IConverterField)Activator.CreateInstance(propertyType); converterField.SourceProvider = SourceProvider; converterField.Column = Row.Table.Columns[propertyName]; converterField.Row = Row; converterField.Content = (retValue == null) ? null : retValue.ToString(); converterField.Initializing(); converterField.ContentUpdatedEvent += OnConverterFieldContentUpdatedEvent; fieldMap[propertyName] = converterField; } return(converterField); } if (retValue == null || retValue is DBNull) { retValue = (propertyType.IsValueType) ? Activator.CreateInstance(propertyType) : null; } if (retValue == null || propertyType.IsAssignableFrom(retValue.GetType())) { return(retValue); } return(CommonExtension.ToObject(retValue.ToString(), propertyType)); }
public List <T> ToEntities <T>(IList <IList <object> > values) where T : new() { if (values == null) { return(null); } Type type = typeof(T); Dictionary <string, IndexingAttribute> attributeMap; int maxIdx = BuildAttriMap(type, out attributeMap); List <T> entities = new List <T>(); List <string> colNames = new List <string>(); for (int i = ColumnRowIdx; i < values.Count; i++) { if (i == ColumnRowIdx && HasColumnNames) { values[i].ToList().ForEach(it => colNames.Add(it.ToString())); continue; } var row = values[i]; T entity = new T(); for (int j = 0; j < row.Count; j++) { var propInfo = GetPropertyInfo(j, colNames, attributeMap); if (propInfo == null) { continue; } var value = CommonExtension.ToObject(row[j], propInfo.PropertyType); propInfo.SetValue(entity, value); } entities.Add(entity); } return(entities); }
static public TEntity GenEntityProxy2 <TEntity>(Func <string, object> getPropertyValue, Action <string, object> setPropertyValue) { GetPropertyDelegate getDelegate = new GetPropertyDelegate((methodInfo, propertyName) => CommonExtension.ToObject(getPropertyValue(propertyName), methodInfo.ReturnType)); SetPropertyDelegate setDelegate = new SetPropertyDelegate((methodInfo, propertyName, value) => setPropertyValue(propertyName, value)); return(GenEntityProxy <TEntity>(getDelegate, setDelegate)); }
static public object ToObject(this string it, Type targetType) { return(CommonExtension.ToObject(it, targetType)); }