What is the exception you are getting?
It looks a little redundant to me. First, you are trying to call
InvokeMember, but you are not passing a member to invoke! Also, I would
think that this is only for executing properties and methods, not for
fetching a value from a field.
I would recommend getting the FieldInfo that corresponds to the field
you want the value of, and then calling the GetValue/SetValue methods to get
the value.
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
-
mv*@spam.guard.caspershouse.com
"zlf" <zl***@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
Hi,
I am trying to access private field by code below. But it runs with
error.
Please tell me what's the problem or any method that may allow me to
access private field. Thank you
class Love
{
private string Name;
}
Love love = new Love();
Type type = love.GetType();
Object obj = type.InvokeMember(null,
BindingFlags.DeclaredOnly |
BindingFlags.Public |
BindingFlags.NonPublic |
BindingFlags.Instance |
BindingFlags.CreateInstance, null, null, args);
string
str=(string)type.InvokeMember("Name",BindingFlags. GetProperty,null,obj,null);