private static Document AddField(this Document luceneDocument, LuceneFieldInfo field, string value) { if (value == null) { return(luceneDocument); } luceneDocument.Add(new Field( field.Name, value, field.LuceneFieldAttribute.Store, field.LuceneFieldAttribute.Index, field.LuceneFieldAttribute.TermVector)); return(luceneDocument); }
private static void SerializeEnumerable <T>(Document luceneDocument, T document, LuceneFieldInfo field) { var enumerable = (IEnumerable)(field.PropertyInfo.GetValue(document)); if (enumerable == null) { return; } foreach (var item in enumerable) { // TODO: limited to value types that are convertible to string // should really have a typeconverter attribute on the fieldInfo object as well to allow specific serialization var value = (string)Convert.ChangeType(item, typeof(string)); luceneDocument.AddField(field, value); } }
private static void TryDeserializeEnumerable <T>(T result, Document document, LuceneFieldInfo field) { var luceneFields = document.GetFields(field.Name); if (luceneFields == null) { return; } var fieldType = field.PropertyInfo.PropertyType; if (fieldType.IsArray()) { var argumentType = fieldType.GetElementType(); var collection = new ArrayList(); foreach (var luceneField in luceneFields) { object value; if (TryDeserialize(luceneField.StringValue, argumentType, out value)) { collection.Add(value); } } field.PropertyInfo.SetValue(result, collection); return; } if (fieldType.IsEnumerableOfT()) { var argumentType = fieldType.GetGenericArguments().First(); var listType = typeof(List <>); var concreteType = listType.MakeGenericType(argumentType); var collection = (IList)Activator.CreateInstance(concreteType); foreach (var luceneField in luceneFields) { object value; if (TryDeserialize(luceneField.StringValue, argumentType, out value)) { collection.Add(value); } } field.PropertyInfo.SetValue(result, collection); } }