Reputation: 5941
i have a public property in my class, how should I mark it (some attribute) to be ignored when following flags are given
BindingFlags.DeclaredOnly |
BindingFlags.Instance |
BindingFlags.NonPublic |
BindingFlags.Public
I need to use method from dll with parameters: object and binding flags, so i need to mark my property somehow to be ignored by this method
Upvotes: 3
Views: 6477
Reputation: 4561
Your flags will not return static properties.
class Foo
{
public static int Bar { get; set; }
}
EDIT You have stated you don't want to change behavior. Does your dll take an object or a type? If a type, you could use inheritance to 'hide' your properties from this method.
Say you have right now:
class Foo
{
public int X {get;set;}
public int Y {get;set;}
}
And your code expect Foo
to have X
and Y
. If your dll/utility takes types, then we can make Foo
derive Bar
. Let's say we don't want the utility to see 'X', but only 'Y'.
class Bar
{
public int Y {get;set;}
}
class Foo : Bar
{
public int X {get;set;}
}
// in your utility call
utility.Baz(foo.GetType().BaseType); // <- Base here is Bar, which does not have X
Upvotes: 0
Reputation: 16651
You could 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.GetNonPublicProperties
and you're set.
Upvotes: 0
Reputation: 156504
I think you're confusing the roles of BindingFlags and CustomAttributes. BindingFlags find things based on whether they are private
, static
, etc.
If you want to ignore a property in your consuming code, you'll need to change that consuming code to ignore properties with a particular custom attribute associated with them.
If you don't have access to the consuming code, there isn't any way to make it not see a property that is there. There may be some workarounds depending on how the code is written, though. For example, if the code is searching based on a specific type argument, you could create an abstract base class to house all the properties you want it to care about, and then extend that class for your actual implementation, adding some extra properties for your convenience.
Or you could create a DTO to represent just the properties you want to convey to the method you're calling, and copy the appropriate properties from your original object into that DTO.
Upvotes: 3
Reputation: 746
I think a custom attribute would be appropriate here. Decorate the members you wish to ignore with the attribute, and wrap the code you're using to inspect the properties in a routine that would weed out the decorated items.
Upvotes: 0
Reputation: 117220
The only way to ignore it during the call is to use Type.FindMembers
, but that will probably require more work than just post filtering it.
Upvotes: 0