Beispiel #1
0
        private object GetContextVariable(string variableName, object target)
        {
            object returnValue;

            variableName = variableName.TrimStart('@');
            var member = target.GetType().GetMember(variableName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);

            if (member.Length > 0)
            {
                if (member[0] is PropertyInfo propertyInfo)
                {
                    returnValue = propertyInfo.GetValue(target, null);
                }
                else if (member[0] is FieldInfo fieldInfo)
                {
                    returnValue = fieldInfo.GetValue(target);
                }
                else
                {
                    throw new HandlebarsRuntimeException("Context variable references a member that is not a field or property");
                }
            }
            else if (ParentContext != null)
            {
                returnValue = ParentContext.GetContextVariable(variableName);
            }
            else
            {
                returnValue = null;
            }
            return(returnValue);
        }