C# 文化管理员问题

C# 文化管理员问题,c#,localization,.net-4.5,C#,Localization,.net 4.5,我有一个错误,我不明白 虽然以下方法很好: Resources.Classes.AFieldFormula.DirectFieldFormula 这一个抛出一个异常: new ResourceManager(typeof(Resources.Classes.AFieldFormula)).GetString("DirectFieldFormula"); 找不到任何适合指定区域性或非特定区域性的资源。确保“Resources.Classes.AFieldFormula.Resources”在编

我有一个错误,我不明白

虽然以下方法很好:

Resources.Classes.AFieldFormula.DirectFieldFormula
这一个抛出一个异常:

new ResourceManager(typeof(Resources.Classes.AFieldFormula)).GetString("DirectFieldFormula");
找不到任何适合指定区域性或非特定区域性的资源。确保“Resources.Classes.AFieldFormula.Resources”在编译时已正确嵌入或链接到程序集“MygLogWeb”,或者所需的所有附属程序集均可加载并完全签名。

怎么会这样

Resource 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);
            }
        }
    }
}
//------------------------------------------------------------------------------
// 
//这段代码是由一个工具生成的。
//运行时版本:4.0.30319.18408
//
//对此文件的更改可能会导致不正确的行为,如果
//重新生成代码。
// 
//------------------------------------------------------------------------------
名称空间资源.类{
使用制度;
/// 
///强类型资源类,用于查找本地化字符串等。
/// 
//此类由StronglyTypedResourceBuilder自动生成
//通过诸如ResGen或Visual Studio之类的工具初始化。
//要添加或删除成员,请编辑.ResX文件,然后重新运行ResGen
//使用/str选项,或重建VS项目。
[global::System.CodeDom.Compiler.GeneratedCodeAttribute(“System.Resources.Tools.StronglyTypedResourceBuilder”,“4.0.0.0”)]
[全局::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[全局::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
公共类AFieldFormula{
私有静态全局::System.Resources.ResourceManager resourceMan;
私有静态全局::System.Globalization.CultureInfo resourceCulture;
[global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute(“Microsoft.Performance”,“CA1811:AvoidUncalledPrivateCode”)]
内部AFieldFormula(){
}
/// 
///返回此类使用的缓存ResourceManager实例。
/// 
[全局::System.ComponentModel.EditorRowsableAttribute(全局::System.ComponentModel.EditorRowsableState.Advanced)]
公共静态全局::System.Resources.ResourceManager ResourceManager{
得到{
if(object.ReferenceEquals(resourceMan,null)){
global::System.Resources.ResourceManager temp=new global::System.Resources.ResourceManager(“MygLogWeb.Classes.AFieldFormula”,typeof(AFieldFormula.Assembly);
资源员=临时工;
}
回归智囊团;
}
}
/// 
///为所有线程重写当前线程的CurrentUICulture属性
///使用此强类型资源类进行资源查找。
/// 
[全局::System.ComponentModel.EditorRowsableAttribute(全局::System.ComponentModel.EditorRowsableState.Advanced)]
公共静态全局::System.Globalization.CultureInfo区域性{
得到{
回归资源文化;
}
设置{
资源文化=价值;
}
}
/// 
///查找类似于Direct字段的本地化字符串。
/// 
公共静态字符串DirectFieldFormula{
得到{
返回ResourceManager.GetString(“DirectFieldFormula”,resourceCulture);
}
}
}
}
我的资源文件位于我的解决方案的\Classes文件夹中,默认名称空间为MygLogWeb。
但是我使用resx属性窗口将“自定义工具名称空间”设置为Resources.class


这不对吗?我真的必须让文件夹反映名称空间吗?

汉斯·帕桑解释了为什么它不起作用

所以我更深入地研究了.Net是如何处理它的,它使用反射而不是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();
        }
    }
}

代码使用的“Resources.Classes.AFieldFormula”与“MygLogWeb.Classes.AFieldFormula”之间存在明显的不匹配“由资源设计器使用。资源设计师很有可能做到这一点。否则,依赖资源设计器是一个很好的理由,资源的名称空间名称是一个相当令人头痛的问题。您始终可以知道在程序集上运行ildasm.exe使用的是哪一个,查看清单并找到.mresource指令。@HansPassant我的资源文件位于我的解决方案的\Classes文件夹中,该文件夹的默认命名空间为MygLogWeb。但是我使用resx属性窗口将“自定义工具名称空间”设置为Resources.class。这不对吗?我真的必须像管理命名空间一样管理我的文件夹吗?