[英]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.