标签: getproperties

使用反射按声明顺序获取属性

我需要按照在类中声明它们的顺序使用反射来获取所有属性.根据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)

c# reflection properties getproperties

73
推荐指数
5
解决办法
3万
查看次数

如何使用Reflection获取类的所有属性及其基类(在层次结构中)?(C#)

所以我现在所拥有的是这样的:

PropertyInfo[] info = obj.GetType().GetProperties(BindingFlags.Public);
Run Code Online (Sandbox Code Playgroud)

obj某个对象在哪里.

问题是我想要的一些属性不在于obj.GetType()它们在其中一个基类中.如果我停止调试器并查看obj,我必须挖掘一些"基础"条目以查看我想要获得的属性.是否有一些绑定标志我可以设置让它返回那些或我是否必须递归挖掘Type.BaseType层次结构并对GetProperties所有这些做?

c# reflection getproperties

26
推荐指数
3
解决办法
3万
查看次数

如何使用Java读取Android属性

我在终端中使用'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代码中访问此信息.

有任何想法吗?

java shell android getproperties adb

9
推荐指数
4
解决办法
8572
查看次数

如何获取文件属性?

我想要一个显示媒体文件的一些文件属性的应用程序,如果可用,比如(不知道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)

c# windows-shell taglib getproperties windows-api-code-pack

8
推荐指数
2
解决办法
7万
查看次数

当使用反射获取属性时,如何将搜索限制为我感兴趣的子类?

在使用反射从程序集中成功获取特定类型的列表之后,我现在想要了解每个类型的公共属性.

这些类型中的每一种都源自至少一个基类.

我注意到当我获得一个类型的属性时,我也从基类中获取属性.

我需要一种方法来过滤掉基类属性,只返回我调用的类型属性的属性.

我认为它类似于我只从给定的基本类型获得基类型的子类,不包括基类型.

Assembly.GetAssembly(baseType).GetTypes().Where(type => type.IsSubclassOf(baseType)).ToList()
Run Code Online (Sandbox Code Playgroud)

reflection assemblies properties getproperties

5
推荐指数
1
解决办法
1119
查看次数

当type.GetProperties()时过滤掉受保护的setter

我试图反映一种类型,并只获取具有公共设置器的属性。这似乎不适用于我。在下面的示例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)

c# reflection protected getproperties

5
推荐指数
1
解决办法
1591
查看次数

.GetProperties()的大O

如果有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)只是为了构建属性数组?

c# reflection getproperties

5
推荐指数
1
解决办法
272
查看次数

如何获取没有自定义类类型的一个类的属性

我有一个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)

c# reflection properties getproperties c#-4.0

5
推荐指数
1
解决办法
2315
查看次数

使用反射(Type.GetProperties)获取DependencyProperties?

我有一个自定义类型,作为几个字段,我只想获得依赖属性.

以下是返回所有属性的代码:

propertyInfos = myType.GetProperties();

foreach (PropertyInfo propertyInfo in propertyInfos)
{
    Console.WriteLine(propertyInfo.Name);
}
Run Code Online (Sandbox Code Playgroud)

我知道我必须在GetProperties的参数中添加一些东西,有些是BindingFlags.XXX,但是我检查了所有可能的XX,并且找不到对我来说听起来不错的东西......

c# dependency-properties getproperties

4
推荐指数
1
解决办法
2273
查看次数

循环使用类和子类来检索C#中的属性和值

我正在尝试遍历一个类及它的子类来获取与它一起传递的值.

这是我的班级:

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)

两个问题:

  1. 我传递给GetProperties,传递子类,然后循环通过它的属性,如果它是一个类,我该怎么办?
  2. 如果属性项不是类,如何从属性项中获取值?

希望这一切都有意义.如果没有,请不要犹豫,我会尽力澄清.

谢谢

c# class typeof getproperties

2
推荐指数
2
解决办法
6617
查看次数