繁体   English   中英

Type.GetProperties()不起作用

[英]Type.GetProperties() not working

因此,我尝试检索从Strings.resx资源生成的属性列表。 一个Strings类是从中自动生成的,而我只是想获取这些属性名称的列表。 下面是一些无效的示例代码。

// 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();

那有什么呢? 试图访问Strings的类在同一程序集中,所以我不认为这是问题。

这是自动生成的Strings类的摘录。

/// <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 ...
    //------------------
}

您缺少非NonPublic标志。 当然,您也需要Static标志,因为该属性是static

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM