Type.GetProperties()不起作用

时间:2014-09-22 18:30:30

标签: c# reflection getproperties

所以我正在尝试检索从Strings.resx资源生成的属性列表。从那里自动生成字符串类,我只是想获取这些属性名称的列表。下面是一些不起作用的示例代码。

// Well this works, so I know there is a property there.
var clearly_a_property = Strings.home_cancel;

// Yet none of this works
var nothing = typeof(Strings).GetProperties(System.Reflection.BindingFlags.Public | 
    System.Reflection.BindingFlags.Static | 
    System.Reflection.BindingFlags.FlattenHierarchy);
nothing = typeof(Strings).GetProperties(System.Reflection.BindingFlags.Public | 
    System.Reflection.BindingFlags.Instance | 
    System.Reflection.BindingFlags.FlattenHierarchy);
nothing = typeof(Strings).GetProperties(System.Reflection.BindingFlags.Public | 
    System.Reflection.BindingFlags.Static);
nothing = typeof(Strings).GetProperties(System.Reflection.BindingFlags.Public);
nothing = typeof(Strings).GetProperties(System.Reflection.BindingFlags.FlattenHierarchy);
nothing = typeof(Strings).GetProperties(System.Reflection.BindingFlags.Static);
nothing = typeof(Strings).GetProperties(System.Reflection.BindingFlags.Instance);
nothing = typeof(Strings).GetProperties(System.Reflection.BindingFlags.DeclaredOnly);
nothing = typeof(Strings).GetProperties();

那是什么给出的?尝试访问字符串的类位于同一个程序集中,因此我认为这不是问题所在。

以下是自动生成的字符串类的摘录。

/// <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()]
internal class Strings {

    //------------------
    // other stuff ...
    //------------------

    internal static string home_cancel {
        get {
            return ResourceManager.GetString("home_cancel", resourceCulture);
        }
    }

    //------------------
    // other stuff ...
    //------------------
}

1 个答案:

答案 0 :(得分:1)

你缺少NonPublic标志。当然,您还需要Static标记,因为属性为static

var something = typeof(Strings).GetProperties(BindingFlags.NonPublic | BindingFlags.Static);