@using MudBlazor
@using System.Reflection
@if (_isSimpleType)
{
@RenderEditor(ItemType, _workingValue, v => _workingValue = v, "Value")
}
else
{
@foreach (var prop in _properties)
{
var p = prop;
var currentVal = p.GetValue(_workingValue);
@RenderEditor(p.PropertyType, currentVal, v => p.SetValue(_workingValue, v), p.Name)
}
}
Cancel
OK
@code {
[CascadingParameter] private IMudDialogInstance MudDialog { get; set; } = null!;
[Parameter] public Type ItemType { get; set; } = null!;
[Parameter] public object? InitialValue { get; set; }
private object? _workingValue;
private bool _isSimpleType;
private PropertyInfo[] _properties = Array.Empty();
protected override void OnParametersSet()
{
_isSimpleType = ItemType.IsValueType || ItemType == typeof(string) || ItemType.IsEnum;
_workingValue = InitialValue;
if (!_isSimpleType && _workingValue != null)
{
_properties = ItemType.GetProperties(BindingFlags.Public | BindingFlags.Instance)
.Where(p => p.CanRead && p.CanWrite)
.ToArray();
}
}
private RenderFragment RenderEditor(Type type, object? value, Action