Does the .NET framework keep an internal List/Hashtable/Collection of all my Lists that I can loop o

  • Thread starter Thread starter Mark S.
  • Start date Start date
M

Mark S.

Hello,

I have a static class with several hundred private static List<>. Does the
..NET framework keep an internal List/Hashtable/Collection of all my Lists
that I can loop over? If so, please show the syntax or docs reference, if
it's there I am unable to find it.

TIA,

M
 
Mark S. kirjoitti:
Hello,

I have a static class with several hundred private static List<>. Does the
.NET framework keep an internal List/Hashtable/Collection of all my Lists
that I can loop over? If so, please show the syntax or docs reference, if
it's there I am unable to find it.

TIA,

M

I guess you can use Reflection

using System.Reflection;
...
MyClass myInstance = new MyClass();
// Get the type of MyClass.
Type myType = typeof(MyClass);
try {
FieldInfo[] myFields = myType.GetFields(BindingFlags.Static |
BindingFlags.NonPublic);
for(int i = 0; i < myFields.Length; i++) {
// Check that myFields.Name suites
Object obj = myFields.GetValue(myInstance);
}
} catch (Exception ex) {
Console.WriteLine("Exception {0}", ex);
}
 
Thank you for your suggestion. Below is an brief representation of the code
in question. Using your sample code I'm able to get it to generate:
Name: myList1
Name: myList2

However, I don't have the skills (even going to "Google University" this
afternoon), as this is the first time I've coded reflection code, to get it
to show something like:
myList1 ID: 123
myList2 ID: null or not in list

Would you be so kind as extend your example?

using System;
using System.Reflection;

public class TestIDClass
{
public volatile string ID;
}

public static class TestListClass
{
public static List<TestIDClass> myList1 = new List<TestIDClass>();
public static List<TestIDClass> myList2 = new List<TestIDClass>();
}

static class test
{
static void Main(string[] args)
{
TestIDClass myIDClass = new TestIDClass();
myIDClass.ID = "123";
TestListClass.myList1.Add(myIDClass);

Type myType = typeof(TestListClass);
try
{
FieldInfo[] myFields = myType.GetFields(BindingFlags.Static |
BindingFlags.Public);
for (int i = 0; i < myFields.Length; i++)
{
Console.WriteLine("Name: " + myFields.Name);
}
}
catch (Exception ex)
{
Console.WriteLine("Exception {0}", ex);
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top