Groups | Blog | Home
all groups > vj# > march 2004 >

vj# : Type class - GetField() issue with protected member and 'BindingFlags.NonPublic' flag


Jitesh Chourasia
3/18/2004 2:31:05 AM
Hi
Type class - GetField() member function does not return protected fields when 'BindingFlags.NonPublic' flag is specified

Is this a bug in GetField() implementation in J#

Thanks
Jites
mikegreonline NO[at]SPAM microsoft.com
3/22/2004 11:49:05 PM
Jitesh,

I am unaware of any problems with Type.GetField. Do you have a small sample
app that reproduces the problem you are seeing?

Thanks,

Michael Green
Microsoft Developer Support
Jitesh Chourasia
3/23/2004 8:21:09 PM
using System
using System.Reflection
public class Myfield

protected string field = "B public field"
public string Fiel

get{return field;
set{if(field!=value){field=value;}



public class Myfieldinf

public static int Main(

Myfieldb Myfieldb = new Myfieldb()

// Get the Type and FieldInfo
Type MyTypea = Type.GetType("Myfielda")
FieldInfo Myfieldinfoa = MyTypea.GetField("field"
BindingFlags.NonPublic|BindingFlags.Instance)
Type MyTypeb = Type.GetType("Myfieldb")
FieldInfo Myfieldinfob = MyTypeb.GetField("field"
BindingFlags.NonPublic|BindingFlags.Instance)

// For the first field, get and display the Name, field, and IsFamily
Console.Write("\n{0} - ", MyTypea.FullName)
Console.Write("{0} - ", Myfieldinfoa.GetValue(Myfielda))
Console.Write("\n IsFamily - {0}", Myfieldinfoa.IsFamily)
Console.Write("\n FieldAttributes - {0}"
Myfieldinfoa.Attributes.ToString())

// For the second field, get and Display the Name, field, and IsFamily
Console.WriteLine()
Console.Write("\n{0} - ", MyTypeb.FullName)
Console.Write("{0} - ", Myfieldinfob.GetValue(Myfieldb))
Console.Write("\n IsFamily - {0}", Myfieldinfob.IsFamily)
FieldAttributes Myfieldattributesb = Myfieldinfob.Attributes
Console.Write("\n FieldAttributes - {0}"
Myfieldinfob.Attributes.ToString())

return 0



mikegreonline NO[at]SPAM microsoft.com
3/25/2004 10:12:29 PM
There are a couple of things going on here. First off in J# you need to
append get_ to any property you are trying to access. So if I was trying to
access a property called prop from an object I would use code like this:

MyObj.get_prop()

Also the property you are trying to access in your code is public, but you
are passing in BindingFlags.NonPublic to GetField, that will tell GetField
to ignore any members that are not declared public. If you replace that
flag with BindingFlags.Public as I have done in the following code,
everything works as expected:

import System.*;
import System.Reflection.*;

public class Myfieldb
{
protected String field = "B public field";
public String getField()
{
return field;
}
void setField(String value)
{
if(field!=value)
{
field=value;
}
}
}

public class Myfieldinfo
{
public static void main()
{
Type MyTypeb = Type.GetType("Myfieldb");
FieldInfo Myfieldinfob = MyTypeb.GetField("field",
BindingFlags.Public|BindingFlags.Instance);
}
}


I hope this is helpful,

Michael Green
Microsoft Developer Support
AddThis Social Bookmark Button