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>
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>