Help ! Why isn't this working?

  • Thread starter Thread starter Christina Androne
  • Start date Start date
C

Christina Androne

I've read the docs and evrything seems fine. Then what I am missing ?


This is the code snippet:


try
{


Assembly LAssembly = Assembly.LoadFrom(args[0]);

Type[] LTypes = LAssembly.GetTypes();

foreach (Type LType in LTypes)
{
if (LType.IsClass)
{
Console.WriteLine ("Type Name : {0}", LType.FullName);

try
{
foreach (FieldInfo LFieldInfo in
LType.GetFields(BindingFlags.Public | BindingFlags.NonPublic))
{
Console.WriteLine ("{0}", LFieldInfo.Name);
}
Console.WriteLine("__");
}
catch (Exception LEx)
{
Console.WriteLine(LEx.Message);
}
}

}
}
catch (Exception LEx)
{
Console.WriteLine(LEx.Message);
}



The assembmly is ok, i get all types in it listed but I can't list the
fields! What am I missing ????


Christina Androne
 
Christina Androne said:
I've read the docs and evrything seems fine. Then what I am missing ?

You haven't specified BindingFlags.Instance or BindingFlags.Static.

<snip>
 
Hi Christina,

The error is in the call to GetProperties:
LType.GetFields(BindingFlags.Public | BindingFlags.NonPublic))

From MSDN:

You must specify Instance or Static along with Public or NonPublic or no
members will be returned.

So if you change the above line by:
LType.GetFields(BindingFlags.Public | BindingFlags.NonPublic
|BindingFlags.Instance ))

you should get the result you want.

Hope this help,
 
Jon said:
You haven't specified BindingFlags.Instance or BindingFlags.Static.

<snip>

I did :( ... It's not working :( ... I've tried all sort of flags, not
to mention the GetFields() method. I copypasted the last thing I tried,
that's why it was without the BindingFlags.Instance.


Christina Androne
 
Ignacio said:
Hi Christina,

The error is in the call to GetProperties:
LType.GetFields(BindingFlags.Public | BindingFlags.NonPublic))

From MSDN:

You must specify Instance or Static along with Public or NonPublic or
no members will be returned.


Thanks a lot for your answer, but I tried that. Please see the my reply
to Jon.

Christina Androne
 
Christina Androne said:
I did :( ... It's not working :( ... I've tried all sort of flags, not
to mention the GetFields() method. I copypasted the last thing I tried,
that's why it was without the BindingFlags.Instance.

Did you try with *all* of:

BindingFlags.Instance |
BindingFlags.Static |
BindingFlags.Public |
BindingFlags.NonPublic

in the same run?

If you could put it in a short but complete program which demonstrates
the problem (eg listing the fields of a single test type, for
simplicity) that would help.

See http://www.pobox.com/~skeet/csharp/complete.html
 
Hi Chistina,

This may seems a rather silly question, but ..
Does the class has any field?
I tried this with a class with several properties and it worked fine
changing GetField by GetProperties and it worked fine


I now tried this code below and it also worked fine:

class A
{
public int a;
public int c;
public int b;
}

Type LType = typeof(A);

if (LType.IsClass)
{
Console.WriteLine ("Type Name : {0}", LType.FullName);

try
{
foreach (FieldInfo LFieldInfo in
LType.GetFields(BindingFlags.Public | BindingFlags.NonPublic|
BindingFlags.Instance))
{
Console.WriteLine ("{0}", LFieldInfo.Name);
}
Console.WriteLine("__");
}
catch (Exception LEx)
{
Console.WriteLine(LEx.Message);
}
}


Hope this help,
 
Back
Top