For Programmers: Free Programming Magazines  


Home > Archive > C# > January 2006 > itterate throught class members of particular type









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author itterate throught class members of particular type
ipp

2006-01-10, 4:10 am

Hi!
I have some class with some members of String type:
class MyClass{
....
string m1;
string m2;
....
string mx;
....
}

I need to itterate through all members of string type of some instance of
MyClass.
like

MyClass mMycl=new MyClass();
foreach(string s in StringMembersOfClass(mMycl))
{
....
}
Seems I can use reflections for something like this function
StringMembersOfClass.

How to implement this?

Regards


NuTcAsE

2006-01-10, 4:10 am

class Class2 {
public string Member1;
public string Member 2;
private string Member3;
private int NonMember1;
private int NonMember2;
}

class Program {
public static void Main (string[] args) {
Type type = typeof (Class2);
FieldInfo[] fields = type.GetFields (BindingFlags.Public |
BindingFlags.NonPublic |

BindingFlags.Instance);
foreach (FieldInfo field in fields)
{
if (field.FieldType == typeof (string))
Console.WriteLine (field.Name);
}
}
}

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com