/// <summary> /// Create a <see cref="VxFormElementLoader{TValue}"/> that will create a <see cref="FormElement"/> /// based on the dynamic <see cref="ExpandoObject"/>. This allows for dynamic usage of the form-generator. /// </summary> /// <typeparam name="TValue"></typeparam> /// <param name="model"></param> /// <param name="key"></param> /// <param name="builder"></param> private void CreateFormElementReferenceExpando <TValue>(ExpandoObject model, string key, RenderTreeBuilder builder) { // cast the model to a dictionary so it's accessable var accessor = ((IDictionary <string, object>)model); object value = default(TValue); accessor.TryGetValue(key, out value); var valueChanged = Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck( EventCallback.Factory.Create <TValue>( this, EventCallback.Factory. CreateInferred(this, __value => accessor[key] = __value, (TValue)accessor[key]))); var formElementReference = new FormElementReference <TValue>() { Value = (TValue)value, ValueChanged = valueChanged, Key = key }; var elementType = typeof(VxFormElementLoader <TValue>); builder.OpenComponent(0, elementType); builder.AddAttribute(1, nameof(VxFormElementLoader <TValue> .ValueReference), formElementReference); builder.CloseComponent(); }
private void CreateFormElementReferencePoco <TValue>(object model, PropertyInfo propertyInfo, RenderTreeBuilder builder) { var valueChanged = Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck( EventCallback.Factory.Create <TValue>( this, EventCallback.Factory. CreateInferred(this, __value => propertyInfo.SetValue(model, __value), (TValue)propertyInfo.GetValue(model)))); // Create an expression to set the ValueExpression-attribute. var constant = Expression.Constant(model, model.GetType()); var exp = Expression.Property(constant, propertyInfo.Name); var lamb = Expression.Lambda <Func <TValue> >(exp); var formElementReference = new FormElementReference <TValue>() { Value = (TValue)propertyInfo.GetValue(model), ValueChanged = valueChanged, ValueExpression = lamb, Key = propertyInfo.Name }; var elementType = typeof(VxFormElementLoader <TValue>); builder.OpenComponent(0, elementType); builder.AddAttribute(1, nameof(VxFormElementLoader <TValue> .ValueReference), formElementReference); builder.CloseComponent(); }