我需要按照在类中声明它们的顺序使用反射来获取所有属性.根据MSDN,使用时无法保证订单GetProperties()
GetProperties方法不以特定顺序返回属性,例如按字母顺序或声明顺序.
但我已经读过,通过订购属性有一个解决方法MetadataToken
.所以我的问题是,这样安全吗?我似乎无法在MSDN上找到有关它的任何信息.或者有没有其他方法来解决这个问题?
我目前的实施情况如下:
var props = typeof(T)
.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
.OrderBy(x => x.MetadataToken);
Run Code Online (Sandbox Code Playgroud) 所以我现在所拥有的是这样的:
PropertyInfo[] info = obj.GetType().GetProperties(BindingFlags.Public);
Run Code Online (Sandbox Code Playgroud)
obj
某个对象在哪里.
问题是我想要的一些属性不在于obj.GetType()
它们在其中一个基类中.如果我停止调试器并查看obj,我必须挖掘一些"基础"条目以查看我想要获得的属性.是否有一些绑定标志我可以设置让它返回那些或我是否必须递归挖掘Type.BaseType
层次结构并对GetProperties
所有这些做?
我在终端中使用'adb shell getprop'.我可以在Android JAVA中使用哪些接口来获取相同的信息?
我尝试过几样的事情:
Properties sysProps = System.getProperties();
Run Code Online (Sandbox Code Playgroud)
但我不认为这些是我想要的相同属性?具体来说,我想找到将返回类似于以下内容的值:
adb shell getprop | grep dolby
Run Code Online (Sandbox Code Playgroud)
shell'grep dolby'命令返回:
[audio.dolby.ds2.enabled]: [true]
[dolby.audio.sink.info]: [headset]
[dolby.ds.dialogenhancer.state]: [on]
[dolby.ds.graphiceq.state]: [off]
[dolby.ds.hpvirtualizer.state]: [off]
[dolby.ds.intelligenteq.preset]: [Off]
[dolby.ds.intelligenteq.state]: [off]
[dolby.ds.platform]: [qcom]
[dolby.ds.profile.name]: [Movie]
[dolby.ds.spkvirtualizer.state]: [off]
[dolby.ds.state]: [off]
[dolby.ds.volumeleveler.state]: [on]
Run Code Online (Sandbox Code Playgroud)
但我想在Android JAVA代码中访问此信息.
有任何想法吗?
我想要一个显示媒体文件的一些文件属性的应用程序,如果可用,比如(不知道windows中使用的确切英文单词)FileName,Length/Duration,FileType(.avi .mp3等)我试过taglib和windowsapishell但我没有得到一个工作结果(参考是好的)
ShellFile so = ShellFile.FromFilePath(file);
so.Properties.System.(everythingIwant)
Run Code Online (Sandbox Code Playgroud)
向我展示了我想要显示的很多文件属性,但我无法让它工作一个错误的例子:
'WindowsFormsApplication2.vshost.exe'(托管(v4.0.30319)):已加载'C:\ Windows\Microsoft.Net\assembly\GAC_MSIL\WindowsBase\v4.0_4.0.0.0__31bf3856ad364e35\WindowsBase.dll',跳过加载符号.模块已经过优化,调试器选项"Just My Code"已启用.程序'[6300] WindowsFormsApplication2.vshost.exe:Program Trace'已退出,代码为0(0x0).程序 '[6300] WindowsFormsApplication2.vshost.exe:托管(v4.0.30319)' 已经与代码0(为0x0)退出.
容易的事情
var thing = so.Properties.System.FileName.Description;
Console.WriteLine(thing);
Run Code Online (Sandbox Code Playgroud)
不会工作
我确实知道一些Java和PHP编程,但我对C#完全不熟悉
特别感谢@ marr75和@errorstacks!
一个跟进问题:我做了这个,它有效
class Program
{
static void Main(string[] args)
{
string file = "E:/Dump/Shutter Island.avi";
FileInfo oFileInfo = new FileInfo(file);
Console.WriteLine("My File's Name: \"" + oFileInfo.Name + "\"");
DateTime dtCreationTime = oFileInfo.CreationTime;
Console.WriteLine("Date and Time File Created: " + dtCreationTime.ToString());
Console.WriteLine("myFile Extension: " + oFileInfo.Extension);
Console.WriteLine("myFile total Size: " + oFileInfo.Length.ToString());
Console.WriteLine("myFile filepath: " …
Run Code Online (Sandbox Code Playgroud) 在使用反射从程序集中成功获取特定类型的列表之后,我现在想要了解每个类型的公共属性.
这些类型中的每一种都源自至少一个基类.
我注意到当我获得一个类型的属性时,我也从基类中获取属性.
我需要一种方法来过滤掉基类属性,只返回我调用的类型属性的属性.
我认为它类似于我只从给定的基本类型获得基类型的子类,不包括基类型.
Assembly.GetAssembly(baseType).GetTypes().Where(type => type.IsSubclassOf(baseType)).ToList()
Run Code Online (Sandbox Code Playgroud) 我试图反映一种类型,并只获取具有公共设置器的属性。这似乎不适用于我。在下面的示例LinqPad脚本中,“ Id”和“ InternalId”与“ Hello”一起返回。我该怎么做才能将它们过滤掉?
void Main()
{
typeof(X).GetProperties(BindingFlags.SetProperty | BindingFlags.Public | BindingFlags.Instance)
.Select (x => x.Name).Dump();
}
public class X
{
public virtual int Id { get; protected set;}
public virtual int InternalId { get; protected internal set;}
public virtual string Hello { get; set;}
}
Run Code Online (Sandbox Code Playgroud) 如果有n个属性,那么.GetProperties O(n)的Big-O还是反射中涉及的过程增加了复杂性?
假设有这个定义的类:
public class Reflector
{
public string name { get; set; }
public int number { get; set; }
public bool flag { get; set; }
public List<string> etc { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
然后进行此调用:
var reflect = new Reflector();
PropertyInfo[] properties = reflect.GetType().GetProperties();
Run Code Online (Sandbox Code Playgroud)
什么是时间复杂度,即Big-O .GetProperties()
?考虑到有4个属性,这只需要4次迭代还是比它更复杂?或者,是否O(1)具有一些标准的复杂性集到达列表 - 这似乎仍然是O(n)只是为了构建属性数组?
我有一个Person类:
public class Person
{
virtual public long Code { get; set; }
virtual public string Title { get; set; }
virtual public Employee Employee { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我需要一个通用的解决方案来获取Person类的所有属性,而不是自定义类类型的属性.意味着选择Code
,Title
属性.
typeof(Person).GetProperties(); //Title , Code , Employee
typeof(Person).GetProperties().Where(x => !x.PropertyType.IsClass); // Code
Run Code Online (Sandbox Code Playgroud)
如何在没有自定义类类型的情况下选择所有属性?(Code
,Title
)
我有一个自定义类型,作为几个字段,我只想获得依赖属性.
以下是返回所有属性的代码:
propertyInfos = myType.GetProperties();
foreach (PropertyInfo propertyInfo in propertyInfos)
{
Console.WriteLine(propertyInfo.Name);
}
Run Code Online (Sandbox Code Playgroud)
我知道我必须在GetProperties的参数中添加一些东西,有些是BindingFlags.XXX,但是我检查了所有可能的XX,并且找不到对我来说听起来不错的东西......
我正在尝试遍历一个类及它的子类来获取与它一起传递的值.
这是我的班级:
public class MainClass
{
bool IncludeAdvanced { get; set; }
public ChildClass1 ChildClass1 { get; set; }
public ChildClass2 ChildClass2 { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
到目前为止,这是我的代码
GetProperties<MainClass>();
private void GetProperties<T>()
{
Type classType = typeof(T);
foreach (PropertyInfo property in classType.GetProperties(BindingFlags.Public | BindingFlags.Instance))
{
WriteToLog(property.Name + ": " + property.PropertyType + ": " + property.MemberType);
GetProperties<property>();
}
}
Run Code Online (Sandbox Code Playgroud)
两个问题:
希望这一切都有意义.如果没有,请不要犹豫,我会尽力澄清.
谢谢
getproperties ×10
c# ×8
reflection ×6
properties ×3
adb ×1
android ×1
assemblies ×1
c#-4.0 ×1
class ×1
java ×1
protected ×1
shell ×1
taglib ×1
typeof ×1