473,511 Members | 14,981 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Having problem getting private members using reflection.

I have an ASP.NET application that is calling a custom class that is trying
to parse all of the members of my Page object using Type.GetMembers(). The
problem that I am having is that private members are not returned. I did
some digging and the MSDN documentation states that the caller must have
ReflectionPermission in order to get the private members of a class. I am a
little unfamiliar with this stipulation. I have checked the docs on
ReflectionPermission, but the examples do not make much sense. Could
someone please clarify on what I need to do in order for my code to be able
to parse private members of my Page using reflection?

--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.

EmailID = varnk
Domain = Diebold.com
-----------------------------------
Nov 19 '05 #1
6 4777
You need to use the overload that accepts a BindingFlags with this bitmask:

BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance

The simple version of GetMembers only returns the public ones (and maybe
internal and protected too. I can't remember exactly).

As for the ReflectionPermission, if you're running in FullTrust you have
the permission. This security check is so mobile downloaded code won't be
able to scrape thru all private members changing their values. That would
be a problem.

-Brock
DevelopMentor
http://staff.develop.com/ballen
I have an ASP.NET application that is calling a custom class that is
trying to parse all of the members of my Page object using
Type.GetMembers(). The problem that I am having is that private
members are not returned. I did some digging and the MSDN
documentation states that the caller must have ReflectionPermission in
order to get the private members of a class. I am a little unfamiliar
with this stipulation. I have checked the docs on
ReflectionPermission, but the examples do not make much sense. Could
someone please clarify on what I need to do in order for my code to be
able to parse private members of my Page using reflection?

EmailID = varnk
Domain = Diebold.com
-----------------------------------

Nov 19 '05 #2
I am using the BindingFlags that you indicated, and I still cannot get
private members.

--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.

EmailID = varnk
Domain = Diebold.com
-----------------------------------
"Brock Allen" <ba****@NOSPAMdevelop.com> wrote in message
news:b8**************************@msnews.microsoft .com...
You need to use the overload that accepts a BindingFlags with this bitmask:
BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance

The simple version of GetMembers only returns the public ones (and maybe
internal and protected too. I can't remember exactly).

As for the ReflectionPermission, if you're running in FullTrust you have
the permission. This security check is so mobile downloaded code won't be
able to scrape thru all private members changing their values. That would
be a problem.

-Brock
DevelopMentor
http://staff.develop.com/ballen
I have an ASP.NET application that is calling a custom class that is
trying to parse all of the members of my Page object using
Type.GetMembers(). The problem that I am having is that private
members are not returned. I did some digging and the MSDN
documentation states that the caller must have ReflectionPermission in
order to get the private members of a class. I am a little unfamiliar
with this stipulation. I have checked the docs on
ReflectionPermission, but the examples do not make much sense. Could
someone please clarify on what I need to do in order for my code to be
able to parse private members of my Page using reflection?

EmailID = varnk
Domain = Diebold.com
-----------------------------------


Nov 19 '05 #3
Private members from the concrete class or the base class? IIRC, there's
one last bit of detail where private base class members are inaccessible
via reflection on the derived type. You have to walk up to the Type.BaseType
to fetch privates. Perhaps this is the problem?

-Brock
DevelopMentor
http://staff.develop.com/ballen
I am using the BindingFlags that you indicated, and I still cannot get
private members.

EmailID = varnk
Domain = Diebold.com
-----------------------------------
"Brock Allen" <ba****@NOSPAMdevelop.com> wrote in message
news:b8**************************@msnews.microsoft .com...
You need to use the overload that accepts a BindingFlags with this

bitmask:
BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance

The simple version of GetMembers only returns the public ones (and
maybe internal and protected too. I can't remember exactly).

As for the ReflectionPermission, if you're running in FullTrust you
have the permission. This security check is so mobile downloaded code
won't be able to scrape thru all private members changing their
values. That would be a problem.

-Brock
DevelopMentor
http://staff.develop.com/ballen
I have an ASP.NET application that is calling a custom class that is
trying to parse all of the members of my Page object using
Type.GetMembers(). The problem that I am having is that private
members are not returned. I did some digging and the MSDN
documentation states that the caller must have ReflectionPermission
in order to get the private members of a class. I am a little
unfamiliar with this stipulation. I have checked the docs on
ReflectionPermission, but the examples do not make much sense.
Could someone please clarify on what I need to do in order for my
code to be able to parse private members of my Page using
reflection?

EmailID = varnk
Domain = Diebold.com
-----------------------------------

Nov 19 '05 #4
Here is a short sample of the problem.
public class test : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Button Button2;

private string TestValue;

public string TestValue2;

private void Page_Load(object sender, System.EventArgs e)
{
ParseMambers(this);
}

// Web Page Initialize code omitted...

private int ParseMembers(object StateObj)
{
Type T = StateObj.GetType();
int Count = 0;
MemberInfo[] MemberArray;

BindingFlags BindFlags = BindingFlags.Instance |
BindingFlags.Static |
BindingFlags.Public |
BindingFlags.NonPublic;

MemberArray = T.GetMembers(BindFlags);

foreach (MemberInfo M in MemberArray)
{
if (M.Name == "TestValue")
{
Count++;
}

if (M.Name == "TestValue2")
{
Count++;
}
}

}

// When we get here, Count will only be 1, it should be 2.
return Count;
}
Note, that if I change TestValue to public, then Count is 2 when calling
ParseMembers.

The wierd this is that this problem only seems to be localized to the main
web Page object. If I declare some other class with private fields, it
seems to work fine.

--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.

EmailID = varnk
Domain = Diebold.com
-----------------------------------
"Brock Allen" <ba****@NOSPAMdevelop.com> wrote in message
news:b8**************************@msnews.microsoft .com...
Private members from the concrete class or the base class? IIRC, there's
one last bit of detail where private base class members are inaccessible
via reflection on the derived type. You have to walk up to the Type.BaseType to fetch privates. Perhaps this is the problem?

-Brock
DevelopMentor
http://staff.develop.com/ballen
I am using the BindingFlags that you indicated, and I still cannot get
private members.

EmailID = varnk
Domain = Diebold.com
-----------------------------------
"Brock Allen" <ba****@NOSPAMdevelop.com> wrote in message
news:b8**************************@msnews.microsoft .com...
You need to use the overload that accepts a BindingFlags with this

bitmask:
BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance

The simple version of GetMembers only returns the public ones (and
maybe internal and protected too. I can't remember exactly).

As for the ReflectionPermission, if you're running in FullTrust you
have the permission. This security check is so mobile downloaded code
won't be able to scrape thru all private members changing their
values. That would be a problem.

-Brock
DevelopMentor
http://staff.develop.com/ballen
I have an ASP.NET application that is calling a custom class that is
trying to parse all of the members of my Page object using
Type.GetMembers(). The problem that I am having is that private
members are not returned. I did some digging and the MSDN
documentation states that the caller must have ReflectionPermission
in order to get the private members of a class. I am a little
unfamiliar with this stipulation. I have checked the docs on
ReflectionPermission, but the examples do not make much sense.
Could someone please clarify on what I need to do in order for my
code to be able to parse private members of my Page using
reflection?

EmailID = varnk
Domain = Diebold.com
-----------------------------------


Nov 19 '05 #5
I think I figured out what is going on, but not sure how to solve it.

I just discovered that an ASP.NET web page is not actually running as the
type that I declare for the page. It appears that ASP.NET wraps my page
class as a derived class. When I look at it in the debugger, my type shows
up as a base class, not as the root derived class. Thus, when I try to
iterate using the this object, I get the wrong type and thus cannot find the
private members.

There is some strange voodoo going on behind the scenes here.

Now the question is, how can I actually get the type of my class by passing
it into a function? Apparently passing 'this' will not work for an ASP.NET
page.
--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.

EmailID = varnk
Domain = Diebold.com
-----------------------------------
"Ken Varn" <nospam> wrote in message
news:u4****************@tk2msftngp13.phx.gbl...
I have an ASP.NET application that is calling a custom class that is trying to parse all of the members of my Page object using Type.GetMembers(). The problem that I am having is that private members are not returned. I did
some digging and the MSDN documentation states that the caller must have
ReflectionPermission in order to get the private members of a class. I am a little unfamiliar with this stipulation. I have checked the docs on
ReflectionPermission, but the examples do not make much sense. Could
someone please clarify on what I need to do in order for my code to be able to parse private members of my Page using reflection?

--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.

EmailID = varnk
Domain = Diebold.com
-----------------------------------

Nov 19 '05 #6
Ah, yes, makes more sense now. Yeah, that's how ASP.NET does things -- the
ASPX is a class which inherits form the codebehind file. That's why it says
"Inherits" in the @Page directive in the ASPX ;)

Anyway, just use typeof(YourCodeBehindClass) instead.

-Brock
DevelopMentor
http://staff.develop.com/ballen
I think I figured out what is going on, but not sure how to solve it.

I just discovered that an ASP.NET web page is not actually running as
the type that I declare for the page. It appears that ASP.NET wraps
my page class as a derived class. When I look at it in the debugger,
my type shows up as a base class, not as the root derived class.
Thus, when I try to iterate using the this object, I get the wrong
type and thus cannot find the private members.

There is some strange voodoo going on behind the scenes here.

Now the question is, how can I actually get the type of my class by
passing it into a function? Apparently passing 'this' will not work
for an ASP.NET page.

EmailID = varnk
Domain = Diebold.com
-----------------------------------
"Ken Varn" <nospam> wrote in message
news:u4****************@tk2msftngp13.phx.gbl...
I have an ASP.NET application that is calling a custom class that is

trying
to parse all of the members of my Page object using
Type.GetMembers().

The
problem that I am having is that private members are not returned. I
did some digging and the MSDN documentation states that the caller
must have ReflectionPermission in order to get the private members of
a class. I am

a
little unfamiliar with this stipulation. I have checked the docs on
ReflectionPermission, but the examples do not make much sense. Could
someone please clarify on what I need to do in order for my code to
be

able
to parse private members of my Page using reflection?

--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.
EmailID = varnk
Domain = Diebold.com
-----------------------------------

Nov 19 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2321
by: frazer | last post by:
hi i have a class in which i have some private and some public members. using reflection i iterate thru the memebers but i dont get the private ones .. i only get the public ones. how do i get the...
10
7341
by: Sunny | last post by:
Hi, I have an old problem which I couldn't solve so far. Now I have found a post in that group that gave me an idea, but I can not fully understand it. The problem is: I'm trying to use a...
5
2014
by: Bob | last post by:
I want to find a way to detect the existance of the private member of a particular type in the derived class from inside the base class itself and call its Dispose() method. Reflection GetFields()...
2
2719
by: tolisss | last post by:
Hi could someone explain to me why the followind code fails using System; using System.Collections; using System.Reflection; using MbUnit.Core.Framework; using MbUnit.Framework; namespace...
10
25716
by: Abelardo Vacca | last post by:
Hi, The title sums up the question pretty much. I would like to access all private members of a class including the private members of its base classes.( I already have the ReflectionPermission )...
20
27729
by: Shawnk | last post by:
I would like to get the class INSTANCE name (not type name) of an 'object'. I can get the object (l_obj_ref.GetType()) and then get the (l_obj_typ.Name) for the class name. I there any way of...
11
25389
by: David Veeneman | last post by:
I need to get a parent's private variable from within a control. Here's what I'm doing: I have a control that I want to be able to detect the presence of a particular (System.ComponentModel)...
11
6423
by: Yarco | last post by:
For example: <?php class Test { private $name = 'yarco'; } $p = new ReflectionPropery('Test', 'name'); print $p->getValue();
0
7137
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7349
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7074
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5659
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4734
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3219
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1572
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
445
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.