FieldInfo GetValue()

  • Thread starter Thread starter Cezar
  • Start date Start date
C

Cezar

Hi,

I'm trying to get the value of a field in a class using
Reflection.

First of all, I can't access private fields. I know MSDN says you can access
them if the code is fully trusted, but what that means?
I added at the end some code I used.

Then, if the field is static everything works fine, but it doesn't work for
public fields. When I try to get the value it returns an ArgumentException
"Object type cannot be converted to target type"
What target type ?? the return type of GetValue is an Object of the type,
so if the field is Double, the GetValue should be a Double, nothing
complicated...

Is there something I miss about GetValue?

Thank you,
Cezar
<snip>
Assembly asm = Assembly.Load(Assembly.GetExecutingAssembly().FullName);
Type [] cls = asm.GetTypes();

FieldInfo fi;
foreach(Type cl in cls)
{
if(cl.Name == "Class1")
{
fi = cl.GetField(fieldName);
Console.WriteLine("Name : ", fi.Name.ToString());
if(fi == null)
{
Console.WriteLine("object fi null !!! ");
return;
}

Console.WriteLine("Name : ", fi.Name.ToString());

Console.WriteLine("FieldType : ", fi.FieldType.Name.ToString());

try
{
Object obj = fi.GetValue(fi);
if(obj != null)
{
Console.WriteLine("Value = : {0} ", obj.ToString());
}
}
catch(FieldAccessException ex1)
{
Console.WriteLine("FieldAccessException : {0}", ex1);
}
catch(TargetException ex2)
{
Console.WriteLine("TargetException : {0}", ex2);
}
catch(NotSupportedException ex3)
{
Console.WriteLine("NotSupportedException : {0}", ex3);
}
catch(ArgumentException ex4)
{
Console.WriteLine("ArgumentException : {0}", ex4);
}
catch(Exception ex)
{
Console.WriteLine("General Exception : {0}", ex);
}
}
}
</snip>
 
Cezar said:
I'm trying to get the value of a field in a class using
Reflection.

First of all, I can't access private fields. I know MSDN says you can access
them if the code is fully trusted, but what that means?
I added at the end some code I used.

Then, if the field is static everything works fine, but it doesn't work for
public fields. When I try to get the value it returns an ArgumentException
"Object type cannot be converted to target type"
What target type ?? the return type of GetValue is an Object of the type,
so if the field is Double, the GetValue should be a Double, nothing
complicated...

It's not the return value that can't be converted - it's the parameter
to FieldInfo.GetValue. You're passing "fi" itself, rather than a
reference to an instance of Class1.

Look at the documentation for the parameter to GetValue for more
information.
 
It's not the return value that can't be converted - it's the parameter
to FieldInfo.GetValue. You're passing "fi" itself, rather than a
reference to an instance of Class1.

Look at the documentation for the parameter to GetValue for more
information.

Thank you again Jon,

I missinterpreted the documentation. I can access public fields
now.
Do you know if there is a way to access the private fields?
What means code fully trusted?

Cezar
 
Cezar said:
I missinterpreted the documentation. I can access public fields
now.
Do you know if there is a way to access the private fields?

You should be able to - have you tried using the version of GetField
which takes a BindingFlags parameter, and specified NonPublic there?
What means code fully trusted?

That's much too big a topic to really cover here. Basically, code runs
under various different trust levels, and depending on its trust level,
it can or can't do various things (writing to files, getting private
fields etc).
 
Hi Jon,
You should be able to - have you tried using the version of GetField
which takes a BindingFlags parameter, and specified NonPublic there?

My mistake :-(
I was using GetField(fieldName);

Thank you,
Cezar
 
Hi,

For non static fields, FieldInfo's GetValue needs an instance of a class
that inherits or declares the field.
What I do is I create an instance of the class that defines the field
using
Object instObj = Activator.CreateInstance(cl);

but now I need to get the value of the field from the current instance of
the class in the "already running" application.
Kind of what the debugger does when it shows the values
for fields, params etc as it runs the application.

I looked at RuntimeFieldHandle. GetHashCode returns the code of an
instance. Can I use this to find the running instance or I'm looking
in the wrong direction?

Any idea how can I get the class' current instance (suppose that is
already
running)

I hope it makes sense what I said...

Thank you
Cezar
 
Back
Top