0

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

虽然以下工作正常:

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.

怎么会?

资源设计器.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 属性窗口将“自定义工具命名空间”设置为 Resources.Classes。

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

4

1 回答 1

0

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

所以我更深入地研究了 .Net 是如何处理它的,它使用 Reflection 而不是 ResourceManager。
因此,如果您想从 Type 获取 Resources,它可能是唯一的编译安全方式。

这是我为自定义属性完成的方法:

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();
        }
    }
}
于 2013-10-30T10:55:35.687 回答