public T To <T>() where T : new() { T t = new T(); Type type = typeof(T); PropertyInfo[] props = type.GetProperties(); foreach (PropertyInfo prop in props) { object[] attrs = prop.GetCustomAttributes(typeof(WordPropertyAttribute), true); if (attrs.Length != 0) { WordPropertyAttribute attr = (WordPropertyAttribute)attrs[0]; string str = (string)((DCTSimpleProperty)this.PropertyCollection[attr.TagID]).Value; prop.SetValue(t, TryParse(str, prop.PropertyType), null); } } return(t); }
private DCTSimpleProperty ConvertToSimpleProperty(PropertyInfo propInfo, object source) { DCTSimpleProperty simpleProp = null; var value = propInfo.GetValue(source, null); Type type = value.GetType(); PropertyInfo[] propInfos = type.GetProperties(); object[] attrs = propInfo.GetCustomAttributes(typeof(WordPropertyAttribute), true); if (attrs.Length > 0) { simpleProp = new DCTSimpleProperty(); WordPropertyAttribute attr = (WordPropertyAttribute)attrs[0]; simpleProp.TagID = attr.TagID; simpleProp.FormatString = attr.FormatString; simpleProp.IsReadOnly = attr.IsReadOnly; simpleProp.Value = propInfo.GetValue(source, null); } return(simpleProp); }
public List <T> ToList <T>(string tagID) where T : new() { List <T> results = new List <T>(); Type type = typeof(T); PropertyInfo[] props = type.GetProperties(); DCTDataProperty dctDataProp = this.PropertyCollection[tagID]; ExceptionHelper.TrueThrow(dctDataProp == null || dctDataProp is DCTSimpleProperty, string.Format("无法根据{0}找到对应的DCTDataProperty", tagID)); DCTComplexProperty compProp = (DCTComplexProperty)dctDataProp; foreach (DCTWordDataObject wordObj in compProp.DataObjects) { T t = new T(); if (wordObj.PropertyCollection.Count == 0) { continue; } foreach (PropertyInfo prop in props) { object[] attrs = prop.GetCustomAttributes(typeof(WordPropertyAttribute), true); if (attrs.Length != 0) { WordPropertyAttribute attr = (WordPropertyAttribute)attrs[0]; DCTSimpleProperty simpleProp = (DCTSimpleProperty)wordObj.PropertyCollection[attr.TagID]; if (null == simpleProp) { continue; } string str = (string)((DCTSimpleProperty)wordObj.PropertyCollection[attr.TagID]).Value; prop.SetValue(t, TryParse(str, prop.PropertyType), null); } } results.Add(t); } return(results); }