Unable to access private fields in reflection even after using ReflectionPermission

Unable to access private fields in reflection even after using ReflectionPermission

Post by Prashant D » Sat, 20 Dec 2003 16:58:09


I am using the ReflectionPermissionAttribute in my code but still i am
unable to access private fields of an object during reflection.

Here is some sample code. I have put in extra ReflectionPermissionAttrbute
everywhere but still it doesnt acees the private fields? I have .net
framework 1.0 installed.

using System;
using System.Reflection;
using System.Security.Permissions;

[assembly:ReflectionPermission(SecurityAction.RequestMinimum,TypeInformation
=true)]
namespace ConsoleApplication3
{

[ReflectionPermission(SecurityAction.Demand,TypeInformation=true)]
class Class1
{

[STAThread]
[ReflectionPermission(SecurityAction.Demand,TypeInformation=true)]
[ReflectionPermissionAttribute(SecurityAction.Demand, Unrestricted=true)]
static void Main(string[] args)
{
Test t = new Test();
Type myType = t.GetType();
FieldInfo f = myType.GetField("a",BindingFlags.NonPublic);
//f is ALWAYS NULL after the call to GetField
Console.WriteLine(f.ToString());
}
}

[ReflectionPermission(SecurityAction.Demand,TypeInformation=true)]
class Test{
//I cant access this field in reflection
private int a=5;

}
}

Any help will be appreciated.
PRASHANT
 
 
 

Unable to access private fields in reflection even after using ReflectionPermission

Post by Mattias Sj » Sun, 21 Dec 2003 04:58:30

Prashant,


The ReflectionPermission attribute will not help you here. What you
need to do is to include BindingFlags.Instance in the GetField call.



Mattias

--
Mattias Sjren [MVP] mattias @ mvps.org
http://www.yqcomputer.com/ | http://www.yqcomputer.com/
Please reply only to the newsgroup.

 
 
 

Unable to access private fields in reflection even after using ReflectionPermission

Post by Prashant D » Mon, 22 Dec 2003 04:00:25

Thanks that worked :)
Although i didnt understand why??

PRASHANT



ReflectionPermissionAttrbute
 
 
 

Unable to access private fields in reflection even after using ReflectionPermission

Post by Mattias Sj » Wed, 24 Dec 2003 05:20:53


Because you're looking for an *instance* field. You have to be pretty
specific and indicate what you're interested in with BindingFlags
argument.



Mattias

--
Mattias Sjren [MVP] mattias @ mvps.org
http://www.yqcomputer.com/ | http://www.yqcomputer.com/
Please reply only to the newsgroup.
 
 
 

Unable to access private fields in reflection even after using ReflectionPermission

Post by Prashant D » Thu, 25 Dec 2003 20:35:23

> Because you're looking for an *instance* field. You have to be pretty

Just what is the difference between a 'normal' field and an *'instance'*
field???

PRASHANT
 
 
 

Unable to access private fields in reflection even after using ReflectionPermission

Post by Mattias Sj » Sat, 03 Jan 2004 00:57:10


I never said instance fields weren't "normal". :-)

There are two kinds of fields (and this applies to other members as
well, such as methods and events); static and instance.

Static members are associated with a type and shared by all object
instances of that type. So there's only one copy of the data per
appdomain.

Instance fields on the other hand are associated with an instance of
the type, and each object has its own copy of the data.

You have to specify which kind you're looking for when using
Reflection. You can of course specify both BindingFlags.Instance and
BindingFlags.Static if you're interested in both.



Mattias

--
Mattias Sjren [MVP] mattias @ mvps.org
http://www.yqcomputer.com/ | http://www.yqcomputer.com/
Please reply only to the newsgroup.