FieldInfo object visibility

  • Thread starter Thread starter Abdessamad Belangour
  • Start date Start date
A

Abdessamad Belangour

Hi,
I want to get the visiblity kind of a FieldInfo object.
The "IsPublic" ans "IsPrivate" properties give the required information for
public and private visibilities. My questions are:
1. What about "protected" visibility?
2. What exactly do we mean by Family ? Is it a hierarchy of deriving
classes?
3. If a field is IsFamily, does it mean it's protected?
4. I tried to get some information through the example of msdn at :
http://longhorn.msdn.microsoft.com/lhsdk/ref/ns/system.reflection/c/fieldinfo/p/isfamily.aspx
but i do not succeed to run it. It seems that the GetField method is unable
to get a field by it's name.
Any help would be appreciated.
Thanks in advance.
Abdessamad.
 
Hi Abdessamad,

1. The mappings from the C# access modifiers to the properties on a
FieldInfo are as follows:

C# Access Modifer FieldIInfo Accessibility Property
public IsPublic
internal protected IsFamilyOrAssembly
internal IsAssembly
protected IsFamily
private IsPrivate

Note that IsFamilyAndAssembly has no corresponding C# accessibility modifier

2. You are right, IsFamily, as stated above means that the field is
protected and IsFamilyOrAssembly means that it is internal protected

3. True.

4. You need to make sure you are setting the BindingAttr parameter in the
method "FieldInfo GetField (String name, BindingFlags bindingAttr)" is set
correctly, for instance, if your field is a private instance field, your
BindingAttr should include the following values
(BindingFlags.NonPublic|BindingFlags.Instance) and if it is a public static
field then your BindingAttr should include the following values
(BindingFlags.Public|BindingFlags.Static).

If you want GetField to search for all fields in a given type then your
BindingAttr should include the following values
(BindingFlags.NonPublic|BindingFlags.Public|BindingFlags.Static|BindingFlags
..Instance)

Please let me know if you need more information,

Thanks,

Karim Elias
VC# Compiler QA
Microsoft
 
Back
Top