Example #1
0
        /// <summary>
        /// Evaluates the binding.
        /// </summary>
        public object Evaluate(Controls.DotvvmBindableObject control, DotvvmProperty property)
        {
            if (Delegate != null)
            {
                return(ExecDelegate(control, true));
            }

            if (!OriginalString.Contains("."))
            {
                throw new Exception("Invalid resource name! Use Namespace.ResourceType.ResourceKey!");
            }

            // parse expression
            var expressionText  = OriginalString.Trim();
            var lastDotPosition = expressionText.LastIndexOf(".", StringComparison.Ordinal);
            var resourceType    = expressionText.Substring(0, lastDotPosition);
            var resourceKey     = expressionText.Substring(lastDotPosition + 1);

            // find the resource manager
            var resourceManager = cachedResourceManagers.GetOrAdd(resourceType, GetResourceManager);

            // return the value
            return(resourceManager.GetString(resourceKey));
        }
        /// <summary>
        /// Evaluates the binding.
        /// </summary>
        public object Evaluate(Controls.DotvvmBindableObject control, DotvvmProperty property)
        {
            if (Delegate != null)
            {
                return(ExecDelegate(control, true));
            }

            // parse expression
            string resourceType = "";

            // check value
            var expressionText = OriginalString?.Trim();

            if (string.IsNullOrWhiteSpace(expressionText))
            {
                throw new Exception("Resource binding contains empty string.");
            }

            GetDirectives(control);

            // check directives combination
            if (!string.IsNullOrWhiteSpace(ResourceTypeDirectiveValue) && !string.IsNullOrWhiteSpace(ResourceNamespaceDirectiveValue))
            {
                throw new Exception("@resourceType and @resourceNamespace directives cannot be used in the same time!");
            }

            // check type directive
            string resourceKey = "";

            if (!string.IsNullOrWhiteSpace(ResourceTypeDirectiveValue))
            {
                if (!ResourceTypeDirectiveValue.Contains(","))
                {
                    throw new Exception($"Assembly of resource type '{expressionText}' was not recognized. Specify make sure the directive value is in format: Assembly, Namespace.ResourceType");
                }
                if (!ResourceTypeDirectiveValue.Contains("."))
                {
                    throw new Exception($"Resource {expressionText} was not found. Specify make sure the directive value is in format: Assembly, Namespace.ResourceType");
                }

                if (!expressionText.Contains("."))
                {
                    resourceType = ResourceTypeDirectiveValue;
                    resourceKey  = expressionText;
                }
                else
                {
                    var lastDotPosition = expressionText.LastIndexOf(".", StringComparison.Ordinal);
                    resourceType = expressionText.Substring(0, lastDotPosition);
                    resourceKey  = expressionText.Substring(lastDotPosition + 1);
                }
            }
            else if (!string.IsNullOrWhiteSpace(ResourceNamespaceDirectiveValue))
            {
                if (ResourceNamespaceDirectiveValue.Contains(","))
                {
                    throw new Exception($"@resourceNamespace {ResourceNamespaceDirectiveValue} contains unexpected charachter ','");
                }
                if (expressionText.Contains("."))
                {
                    var lastDotPosition = expressionText.LastIndexOf(".", StringComparison.Ordinal);
                    resourceType = expressionText.Substring(0, lastDotPosition);
                    resourceKey  = expressionText.Substring(lastDotPosition + 1);
                }
                else
                {
                    throw new Exception($"Resource '{expressionText}' does not specify resource class or resource key. Make sure that format of expression is: OptionalNamespace.ResourceType.ResourceKey");
                }
            }
            else
            {
                if (expressionText.Contains("."))
                {
                    var lastDotPosition = expressionText.LastIndexOf(".", StringComparison.Ordinal);
                    resourceType = expressionText.Substring(0, lastDotPosition);
                    resourceKey  = expressionText.Substring(lastDotPosition + 1);
                }
                else
                {
                    throw new Exception($"Resource '{expressionText}' does not specify resource class or resource key. Make sure that format of expression is: Namespace.ResourceType.ResourceKey");
                }
            }
            // find the resource manager
            var resourceManager = cachedResourceManagers.GetOrAdd(resourceType, GetResourceManager);

            if (resourceManager == null)
            {
                resourceManager = cachedResourceManagers.GetOrAdd(ResourceNamespaceDirectiveValue + "." + resourceType, GetResourceManager);
            }
            if (resourceManager == null)
            {
                throw new Exception($"The resource file '{resourceType}' was not found! Make sure that your resource file has Access Modifier set to Public or Internal.");
            }

            // return the value
            var value = resourceManager.GetString(resourceKey);

            if (value == null)
            {
                throw new Exception($"Resource '{expressionText}' was not found.");
            }
            return(value);
        }