rabidus-test/Assets/Plugins/QFSW/Quantum Console/Source/Scripts/Parsing/PolymorphicQcParser.cs

39 lines
1.1 KiB
C#
Raw Normal View History

2023-08-22 15:41:12 +03:00
using System;
namespace QFSW.QC
{
/// <summary>
/// Parser for all types inheriting from a single type.
/// </summary>
/// <typeparam name="T">Base type of the types to parse.</typeparam>
public abstract class PolymorphicQcParser<T> : IQcParser where T : class
{
private Func<string, Type, object> _recursiveParser;
public virtual int Priority => -1000;
public bool CanParse(Type type)
{
return typeof(T).IsAssignableFrom(type);
}
public virtual object Parse(string value, Type type, Func<string, Type, object> recursiveParser)
{
_recursiveParser = recursiveParser;
return Parse(value, type);
}
protected object ParseRecursive(string value, Type type)
{
return _recursiveParser(value, type);
}
protected TElement ParseRecursive<TElement>(string value)
{
return (TElement)_recursiveParser(value, typeof(TElement));
}
public abstract T Parse(string value, Type type);
}
}