private static void SetPropertyFromTextValue(ParserContext context, ConsoleControl control, PropertyInfo property, string textValue)
{
bool isObservable = textValue.StartsWith("{") && textValue.EndsWith("}");
if (isObservable)
{
var observablePropertyName = textValue.Substring(1, textValue.Length - 2);
var viewModelObservable = context.CurrentViewModel as ObservableObject;
if (viewModelObservable == null) throw new InvalidOperationException("View model is not observable");
new ViewModelBinding(control, property, viewModelObservable, observablePropertyName);
}
else if(property.HasAttr<MarkupPropertyAttribute>())
{
property.Attr<MarkupPropertyAttribute>().Processor.Process(context);
}
else if (property.PropertyType == typeof(string))
{
property.SetValue(control, textValue);
}
else if (property.PropertyType == typeof(ConsoleString))
{
property.SetValue(control, new ConsoleString(textValue));
}
else if (property.PropertyType.IsEnum)
{
var enumVal = Enum.Parse(property.PropertyType, textValue);
property.SetValue(control, enumVal);
}
else if (property.PropertyType == typeof(Event))
{
Event ev = property.GetValue(control) as Event;
var target = context.CurrentViewModel;
Action handler = () =>
{
var method = target.GetType().GetMethod(textValue, new Type[0]);
if (method != null)
{
method.Invoke(target, new object[0]);
}
else
{
var action = target.GetType().GetProperty(textValue);
if (action == null || action.PropertyType != typeof(Action))
{
throw new InvalidOperationException("Not a method or action");
}
((Action)action.GetValue(target)).Invoke();
}
};
ev.SubscribeForLifetime(handler, control.LifetimeManager);
}
else
{
var parseMethod = property.PropertyType.GetMethod("Parse", new Type[] { typeof(string) });
var parsed = parseMethod.Invoke(null, new object[] { textValue });
property.SetValue(control, parsed);
}
}