protected ContentConverter(Type type, KeyConverter keyConverter, KeyForType forType) { idGetter = GetIdGetter(type); dataGetters = keyConverter.GetDataGetters( type: type, forType: forType).ToArray(); }
private IEnumerable <Action <object, object> > GetSetters(Type type, KeyForType keyForType) { var attributeProperties = type?.GetProperties <KeyAttribute>()? .Where(p => p.GetSetMethod() != default).ToArray(); if (attributeProperties?.Any() ?? false) { foreach (var attributeProperty in attributeProperties) { var name = attributeProperty.GetAttribute <KeyAttribute>()?.Name ?? attributeProperty.Name; var key = keys.SingleOrDefault(k => k.AttrName == name && k.For == keyForType); if (key != default) { var attributeType = attributeProperty.PropertyType; if (!setters.ContainsKey(attributeType)) { var textGetter = default(Func <object, string>); switch (keyForType) { case KeyForType.Graph: textGetter = (graph) => (graph as GraphType).GetAttribute(key); break; case KeyForType.Node: textGetter = (node) => (node as NodeType).GetAttribute(key); break; case KeyForType.Edge: textGetter = (edge) => (edge as EdgeType).GetAttribute(key); break; } if (textGetter != default) { void result(object element, object output) => SetAttribute( element: element, attributeProperty: attributeProperty, attributeType: attributeType, textGetter: textGetter, output: output); yield return(result); } } } } } }
public IEnumerable <Func <object, DataType> > GetDataGetters(Type type, KeyForType forType = KeyForType.All) { var properties = type.GetProperties() .Where(p => p.GetCustomAttribute(typeof(KeyAttribute)) != default).ToArray(); foreach (var property in properties) { var name = (property.GetCustomAttribute(typeof(KeyAttribute)) as KeyAttribute)?.Name ?? property.Name; var key = GetKey( name: name, type: property.PropertyType, forType: forType); yield return((input) => GetData( input: input, property: property, key: key)); } }
public IEnumerable <Action <object, object> > Get(Type type, KeyForType keyForType) { var result = default(IEnumerable <Action <object, object> >); if (keys?.Any() ?? false) { if (!setters.ContainsKey(type)) { var currents = GetSetters( type: type, keyForType: keyForType).ToArray(); setters.Add( key: type, value: currents); } result = setters[type]; } return(result); }
private KeyType GetKey(string name, Type type, KeyForType forType) { var keyType = GetKeyType(type); var key = Keys?.SingleOrDefault(k => k.AttrName == name && k.For == forType && k.AttrType == keyType); if (key == default) { key = new KeyType { For = forType, AttrName = name, AttrType = keyType, AttrTypeSpecified = true, Id = $"Key-{keyIndex++}", }; Keys.Add(key); } return(key); }