Fluent Validation无法在用于本地化的ResourceManager类上查找属性

zya*_*ash 3 c# localization fluentvalidation nancy

我在NancyFX应用程序中使用Fluent验证.使用nuget包安装:Nancy.Validation.FluentValidation

我编写了自己的属性Validator类,并完成了可以通过Strings.invalid_ip属性获取的错误消息的本地化.

所有其他本地化在我的项目中工作正常,Fluent验证例外,它无法在Strings资源管理器类中找到任何属性.

namespace LmsNg.Validators
{
    public class IsIpAddressValidator : PropertyValidator
    {
        public IsIpAddressValidator()
            : base(() => Strings.invalid_ip)

            //also tried another overload, but the same exception happens
            //base("invalid_ip", typeof(Strings))
        {
        }

        protected override bool IsValid(PropertyValidatorContext context)
        {
            var ip = context.PropertyValue as string;
            IPAddress address;

            if (ip != null && IPAddress.TryParse(ip, out address))
            {
                return true;
            }

            return false;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

文件属性

一旦IsIpAddressValidator使用,当验证返回false时,我得到一个例外.

FluentValidation.dll中出现"System.InvalidOperationException"类型的异常,但未在用户代码中处理

System.InvalidOperationExcepction: Could not find a property named 'invalid_ip' on type 'LmsNg.Resources.Strings'.

堆栈跟踪:

   at FluentValidation.Resources.StaticResourceAccessorBuilder.GetResourceAccessor(Type resourceType, String resourceName) in c:\Projects\FluentValidation\src\FluentValidation\Resources\IResourceAccessorBuilder.cs:line 29
   at FluentValidation.Resources.LocalizedStringSource..ctor(Type resourceType, String resourceName, IResourceAccessorBuilder resourceAccessorBuilder) in c:\Projects\FluentValidation\src\FluentValidation\Resources\LocalizedStringSource.cs:line 42
   at FluentValidation.Resources.LocalizedStringSource.CreateFromExpression(Expression`1 expression, IResourceAccessorBuilder resourceProviderSelectionStrategy) in c:\Projects\FluentValidation\src\FluentValidation\Resources\LocalizedStringSource.cs:line 66
   at FluentValidation.Validators.PropertyValidator..ctor(Expression`1 errorMessageResourceSelector) in c:\Projects\FluentValidation\src\FluentValidation\Validators\PropertyValidator.cs:line 54
   at LmsNg.Validators.IsIpAddressValidator..ctor() in d:\WorkSpace\VisualStudio\LmsNg\LmsNg\Validators\IsIpAddressValidator.cs:line 16
   at LmsNg.Modules.Networks.NetworkValidator..ctor() in d:\WorkSpace\VisualStudio\LmsNg\LmsNg\Modules\Networks\NetworkValidator.cs:line 16
   at lambda_method(Closure , Object[] )
   at Nancy.TinyIoc.TinyIoCContainer.ConstructType(Type requestedType, Type implementationType, ConstructorInfo constructor, NamedParameterOverloads parameters, ResolveOptions options)
Run Code Online (Sandbox Code Playgroud)

Build Action的我的.resx文件不正确的?我尝试了几个组合,但仍然会发生同样的错误.

谢谢.

zya*_*ash 5

因此,默认情况下,.resx文件生成.cs包含internal类/字段上的修饰符的文件.

这意味着在这种情况下,不能从不同的组件访问这些FluentValidation.dll.

在属性窗格中更改Custom Tool每个.resx文件

从:ResXFileCodeGenerator 到:PublicResXFileCodeGenerator

帮助解决了这个问题,因为现在生成的代码包含一个public修饰符而不是internal.