Path: redb/redb.PropsEditor/Services/PropsMetadataService.cs
Snapshot: redb, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb
Page: https://redbase.app/src/dev-2026-12-06/redb/redb.PropsEditor/Services/PropsMetadataService.cs
----
using System.Reflection;
using redb.PropsEditor.Attributes;
namespace redb.PropsEditor.Services;
///
/// Provides metadata about Props classes for dynamic UI generation.
/// Uses reflection to analyze property types and attributes.
///
public class PropsMetadataService
{
///
/// Gets metadata for all properties of a Props type.
///
public IEnumerable GetProperties() where TProps : class
{
return GetProperties(typeof(TProps));
}
///
/// Gets metadata for all properties of a Props type.
///
public IEnumerable GetProperties(Type propsType)
{
var properties = propsType.GetProperties(BindingFlags.Public | BindingFlags.Instance)
.Where(p => p.CanRead && p.CanWrite)
.Where(p => p.GetCustomAttribute() == null)
.Select(CreateMetadata)
.OrderBy(m => m.Order)
.ThenBy(m => m.Label);
return properties;
}
private PropertyMetadata CreateMetadata(PropertyInfo prop)
{
var displayAttr = prop.GetCustomAttribute();
var textAreaAttr = prop.GetCustomAttribute();
var groupAttr = prop.GetCustomAttribute();
var readOnlyAttr = prop.GetCustomAttribute();
return new PropertyMetadata
{
PropertyInfo = prop,
Name = prop.Name,
Label = displayAttr?.Label ?? FormatLabel(prop.Name),
Placeholder = displayAttr?.Placeholder,
Description = displayAttr?.Description,
Order = displayAttr?.Order ?? 0,
GroupName = groupAttr?.GroupName,
GroupCollapsed = groupAttr?.Collapsed ?? false,
IsReadOnly = readOnlyAttr != null,
IsTextArea = textAreaAttr != null,
TextAreaRows = textAreaAttr?.Rows ?? 4,
PropertyType = prop.PropertyType,
EditorType = DetermineEditorType(prop)
};
}
private EditorType DetermineEditorType(PropertyInfo prop)
{
var type = prop.PropertyType;
var underlyingType = Nullable.GetUnderlyingType(type) ?? type;
// Check for TextArea attribute first
if (prop.GetCustomAttribute() != null)
return EditorType.TextArea;
// String
if (underlyingType == typeof(string))
return EditorType.Text;
// Boolean
if (underlyingType == typeof(bool))
return EditorType.Checkbox;
// Numeric types
if (underlyingType == typeof(int) || underlyingType == typeof(long) ||
underlyingType == typeof(short) || underlyingType == typeof(byte))
return EditorType.Integer;
if (underlyingType == typeof(decimal) || underlyingType == typeof(double) ||
underlyingType == typeof(float))
return EditorType.Decimal;
// DateTime
if (underlyingType == typeof(DateTime) || underlyingType == typeof(DateTimeOffset))
return EditorType.DateTime;
// Enum
if (underlyingType.IsEnum)
return EditorType.Enum;
// Arrays
if (type.IsArray)
{
var elementType = type.GetElementType()!;
if (elementType == typeof(string))
return EditorType.StringArray;
if (elementType.IsPrimitive || elementType == typeof(decimal))
return EditorType.PrimitiveArray;
return EditorType.ObjectArray;
}
// Complex object
if (underlyingType.IsClass && underlyingType != typeof(string))
return EditorType.Object;
// Fallback to JSON
return EditorType.Json;
}
private static string FormatLabel(string propertyName)
{
// Convert PascalCase to "Pascal Case"
var chars = new List();
foreach (var c in propertyName)
{
if (chars.Count > 0 && char.IsUpper(c))
chars.Add(' ');
chars.Add(c);
}
return new string(chars.ToArray());
}
}
///
/// Metadata about a single property for UI rendering.
///
public class PropertyMetadata
{
public required PropertyInfo PropertyInfo { get; init; }
public required string Name { get; init; }
public required string Label { get; init; }
public string? Placeholder { get; init; }
public string? Description { get; init; }
public int Order { get; init; }
public string? GroupName { get; init; }
public bool GroupCollapsed { get; init; }
public bool IsReadOnly { get; init; }
public bool IsTextArea { get; init; }
public int TextAreaRows { get; init; }
public required Type PropertyType { get; init; }
public EditorType EditorType { get; init; }
///
/// Gets the value of this property from an object.
///
public object? GetValue(object obj) => PropertyInfo.GetValue(obj);
///
/// Sets the value of this property on an object.
///
public void SetValue(object obj, object? value) => PropertyInfo.SetValue(obj, value);
}
///
/// Type of editor to render for a property.
///
public enum EditorType
{
Text,
TextArea,
Checkbox,
Integer,
Decimal,
DateTime,
Enum,
StringArray,
PrimitiveArray,
ObjectArray,
Object,
Json
}