meowlang/parser/ModelBase.cs

30 lines
935 B
C#
Raw Normal View History

using System.Collections;
2022-02-12 02:29:25 +01:00
namespace meowlang.parser;
public abstract record ModelBase([property: Ignore] Span Span)
{
public Dictionary<Guid, object> Metadata { get; } = new();
public virtual IEnumerable<ModelBase> GetChildren()
{
var properties = GetType().GetProperties();
foreach (var propertyInfo in properties)
{
if (propertyInfo.PropertyType.IsAssignableTo(typeof(ModelBase)))
{
if (propertyInfo.GetValue(this) is ModelBase value) yield return value;
}
else if (propertyInfo.PropertyType.IsAssignableTo(typeof(IEnumerable)))
{
if (propertyInfo.GetValue(this) is IEnumerable<ModelBase> values)
{
foreach (var value in values)
{
yield return value;
}
}
}
}
}
}