简体   繁体   English

C#反射方法GetProperties(BindingFlags.Instance)不返回子类对象

[英]C# Reflection method GetProperties(BindingFlags.Instance) not returning child class objects

I am attempting to retrieve the child classes of an object while omitting primitive types.我试图在省略原始类型的同时检索对象的子类。

   public class Dog
    {
    public int Id {get;set;}
    public int Name {get;set;}
    public Breed Breed {get;set;}
    }

var dog = new Dog(); var dog = new Dog(); var children = dog.GetType().GetProperties(BindingFlags.Instance); var children = dog.GetType().GetProperties(BindingFlags.Instance);

Why does the children array not contain the breed property?为什么 children 数组不包含品种属性?

By supplying only BindingFlags.Instance , you can't get any properties at all, because you are not sending any access modifier predicate.通过仅提供BindingFlags.Instance ,您根本无法获得任何属性,因为您没有发送任何访问修饰符谓词。

According to your needs, combine these flags with bitwise OR operator |根据您的需要,将这些标志与按位 OR 运算符|结合使用

You can find the documentation here: https://docs.microsoft.com/en-us/dotnet/api/system.reflection.bindingflags?view=netframework-4.8您可以在此处找到文档: https : //docs.microsoft.com/en-us/dotnet/api/system.reflection.bindingflags?view=netframework-4.8

var children = dog.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);

EDIT:编辑:

Unfortunately, the enumeration does not have any value for filtering the properties according to their value types.不幸的是,枚举没有根据属性值类型过滤属性的任何值。 To make this a complete answer, the filtering to an array containing only the Breed property is as contributed by @const-phi:为了使其成为一个完整的答案,对仅包含Breed属性的数组的过滤由@const-phi 提供:

var result = children.Where(c => c.PropertyType.IsClass).ToArray(); // Const Phi 

暂无
暂无

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

相关问题 System.Reflection.BindingFlags.Instance与C#访问修改器的对应关系 - System.Reflection.BindingFlags.Instance correspondence to C# access modifers 具有适当的BindingFlags的继承/子类的GetProperties不返回预期的属性 - GetProperties for inherited/child class with appropriate BindingFlags does not return expected properties 在.NET Reflection中使用GetProperties()和BindingFlags.DeclaredOnly - Using GetProperties() with BindingFlags.DeclaredOnly in .NET Reflection C#反射帮助? (的GetProperties) - C# Reflection Help? (GetProperties) System.Reflection GetProperties方法不返回值 - System.Reflection GetProperties method not returning values GetConstructors与_baseType.GetMethod(“ GetConstructorImpl”,BindingFlags.NonPublic | BindingFlags.Instance) - GetConstructors vs. _baseType.GetMethod(“GetConstructorImpl”, BindingFlags.NonPublic | BindingFlags.Instance) 为什么我的Type.GetFields(BindingFlags.Instance | BindingFlags.Public)不起作用? - Why is my Type.GetFields(BindingFlags.Instance|BindingFlags.Public) not working? C# 反射:“类方法”只读属性的 GetProperties - C# Reflection: GetProperties for 'method-like' read-only properties 带有 BindingFlags.Public 的方法 GetProperties 不返回任何内容 - Method GetProperties with BindingFlags.Public doesn't return anything 如何在C#中使用子类的实例访问父类的方法 - how to access method of parent class with instance of child class in c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM