CultureManager问题

时间:2013-10-29 15:51:43

标签: c# localization .net-4.5

我有一个我不明白的错误。

虽然以下工作正常:

Resources.Classes.AFieldFormula.DirectFieldFormula

这个抛出异常:

new ResourceManager(typeof(Resources.Classes.AFieldFormula)).GetString("DirectFieldFormula");

Could not find any resources appropriate for the specified culture or the neutral culture. Make sure \"Resources.Classes.AFieldFormula.resources\" was correctly embedded or linked into assembly \"MygLogWeb\" at compile time, or that all the satellite assemblies required are loadable and fully signed.

怎么来的?

资源designer.cs文件:

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.18408
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace Resources.Classes {
    using System;


    /// <summary>
    ///   A strongly-typed resource class, for looking up localized strings, etc.
    /// </summary>
    // This class was auto-generated by the StronglyTypedResourceBuilder
    // class via a tool like ResGen or Visual Studio.
    // To add or remove a member, edit your .ResX file then rerun ResGen
    // with the /str option, or rebuild your VS project.
    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")]
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
    [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
    public class AFieldFormula {

        private static global::System.Resources.ResourceManager resourceMan;

        private static global::System.Globalization.CultureInfo resourceCulture;

        [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
        internal AFieldFormula() {
        }

        /// <summary>
        ///   Returns the cached ResourceManager instance used by this class.
        /// </summary>
        [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
        public static global::System.Resources.ResourceManager ResourceManager {
            get {
                if (object.ReferenceEquals(resourceMan, null)) {
                    global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("MygLogWeb.Classes.AFieldFormula", typeof(AFieldFormula).Assembly);
                    resourceMan = temp;
                }
                return resourceMan;
            }
        }

        /// <summary>
        ///   Overrides the current thread's CurrentUICulture property for all
        ///   resource lookups using this strongly typed resource class.
        /// </summary>
        [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
        public static global::System.Globalization.CultureInfo Culture {
            get {
                return resourceCulture;
            }
            set {
                resourceCulture = value;
            }
        }

        /// <summary>
        ///   Looks up a localized string similar to Direct field.
        /// </summary>
        public static string DirectFieldFormula {
            get {
                return ResourceManager.GetString("DirectFieldFormula", resourceCulture);
            }
        }
    }
}

我的资源文件位于我的解决方案的\ Classes文件夹中,默认命名空间是MygLogWeb。
但我使用了resx属性窗口来设置&#34;自定义工具命名空间&#34;作为Resources.Classes。

那是错的吗?我真的必须让我的文件夹反映命名空间吗?

1 个答案:

答案 0 :(得分:0)

Hans Passant解释了为什么它不起作用。

所以我深入研究了.Net如何处理它,它使用Reflection代替ResourceManager。
因此,如果您想从类型中获取资源,那么它可能是唯一可以安全编译的方法。

以下是我为自定义属性完成的操作:

public class FriendlyNameAttribute : Attribute
{
    private delegate string dGetString();

    private dGetString dValue;

    public string ResourceName
    {
        get;
        private set;
    }

    public Type ResourceType
    {
        get;
        private set;
    }

    private void CheckValue()
    {
        this.dValue = () => this.ResourceName;

        if (this.ResourceType == null || !this.ResourceType.IsVisible ||  this.ResourceName == null)
        {
            return;
        }

        var property = this.ResourceType.GetProperty(this.ResourceName);

        if (property == null || property.PropertyType != typeof(string))
        {
            return;
        }

        this.dValue = () => (string)property.GetValue(null, null);
    }

    public FriendlyNameAttribute(string resourceName, Type resourceType = null)
    {
        this.ResourceName = resourceName;
        this.ResourceType = resourceType;
        this.CheckValue();
    }

    public string Value
    {
        get
        {
            return this.dValue();
        }
    }
}