private ExpandedObject Build(ITemplateContext context) { IDictionary <string, object> dictionary = new ExpandedObject(); var serializer = Container.Get <IJsonSerializer>(); foreach (var property in this.properties) { if (this.jsonKeys.Contains(property.Key)) { string values = property.Value.ToString(); string value = MarkerRegex.Replace( values, m => { string propertyName = m.Groups[1].Value; object retValue = context.GetValue(propertyName); return(retValue == null ? string.Empty : retValue.ToString()); }); var parsedValue = serializer.Deserialize <object>(value); dictionary.Add(property.Key, parsedValue); } else { dictionary.Add(property.Key, property.Value); } } return((ExpandedObject)dictionary); }
public void Copies_existing_properties() { var obj = new { id = 5 }; dynamic expanded = new ExpandedObject(obj); Assert.AreEqual(obj.id, expanded.id); }
public ReflectionType(ExpandedObject expandInstance) { this.info = expandInstance.GetType(); this.expandInstance = expandInstance; this.attributes = new Lazy <IReadOnlyList <Attribute> >(() => new List <Attribute>()); this.properties = new Lazy <IReadOnlyList <IReflectionProperty> >(() => new List <IReflectionProperty>()); }
public void Can_add_new_properties_to_expanded_object() { dynamic expanded = new ExpandedObject(new object()); var data = "some additional data"; expanded.data = data; Assert.AreEqual(data, expanded.data); }
/// <summary> /// Gets the specified instance. /// </summary> /// <param name="instance">The instance.</param> /// <returns>IReflectionType instance.</returns> public static IReflectionType Get(ExpandedObject instance) { if (instance == null) { throw new ArgumentNullException("instance"); } return(new ReflectionType(instance)); }
public void Render(ITemplateContext context) { var templateExpression = Container.TryGet <ITemplateExpression>(this.expression); if (templateExpression != null) { ExpandedObject clone = this.Build(context); templateExpression.Render(this.expression, this.inverted, clone, this.parts, context); } }
/// <summary> /// Gets the specified instance. /// </summary> /// <param name="instance">The instance.</param> /// <returns>IReflectionType instance.</returns> public static IReflectionType Get(object instance) { if (instance == null) { throw new ArgumentNullException("instance"); } Type type = instance as Type; if (type != null) { return(Get(type)); } ExpandedObject expandObject = instance as ExpandedObject; if (expandObject != null) { return(new ReflectionType(expandObject)); } return(Get(instance.GetType())); }