How to ignore a property when using reflection

I have a public property in my class, how to mark it (some attribute) to ignore it when specifying the following flags

BindingFlags.DeclaredOnly | 
BindingFlags.Instance | 
BindingFlags.NonPublic | 
BindingFlags.Public

I need to use a method from a dll with parameters: object flags and bindings, so I need to mark my property somehow in order to ignore this method

+3
source share
5 answers

I think you are mixing the roles of BindingFlags and CustomAttributes. BindingFlags finds things depending on whether they are private, staticetc.

, , , .

, , . , . , , , , , .

DTO , , DTO.

+3

.

class Foo
{
  public static int Bar { get; set; }
}

EDIT , . dll ? , , "" .

, :

class Foo
{
  public int X {get;set;}
  public int Y {get;set;}
}

Foo X Y. dll/utility , Foo Bar. , , "X", "Y".

class Bar
{
  public int Y {get;set;}
}

class Foo : Bar
{
  public int X {get;set;}
}

//

utility.Baz(foo.GetType().BaseType); // <- Base here is Bar, which does not have X
0

- Type.FindMembers, , , , -.

0

I think that a special attribute would be appropriate here. Decorate the elements that you want to ignore with the attribute, and wrap the code you use to check the properties in the routine that weeded out the decorated items.

0
source

You can do this with a simple extension method:

public static class ExtensionMethods
{
    public static PropertyInfo[] GetNonPublicProperties(this Type t) {
        return t.GetProperties(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public).Where(prop => prop.GetAccessors(true).Where(mi => mi.IsPublic == false).Count() != 0).ToArray();
    }
}

Then just use Type.GetNonPublicPropertiesand you install.

0
source

All Articles