0
class A
{
    public string a { get; set; } 
    --and so on--   //lets we have 30-50 class variables//
}

i know its bad. but how can i get the values of all variables by just looping and without knowing their name just with object or instanse of class?

like

for(int i;i<30;i++)
{
    variable[i] = object.? ;
}
1
  • can some give me little example? Commented Dec 23, 2010 at 11:42

2 Answers 2

6

Use reflection.

typeof(A).GetFields()
typeof(A).GetProperties()
Sign up to request clarification or add additional context in comments.

4 Comments

keep in mind though that reflection is a performance killer...I tried it in an OR/M once to do some automatic mapping
@Ozzy: It depends what you're doing - and which version of .NET you're using. I believe it improved a lot between CLR 1 and CLR 2 for example. You can also take a one-off hit to create delegates to fetch each field/property - which can make it much, much faster.
@Jon Is there any way to take advantage of dynamic stuff introduced in .NET 4? AFAIK dynamic implements IDictionary.
@Jani - are you confusing dynamic with ExandoObject? AFAIK dynamic doesn't implement anything, it's effectively just object with a flag that says defer binding until runtime.
0

In your example you have a field rather than a property. The below shows an example of how to iterate through all the members of a type. You can filter down using FindMembers instead of GetMemebrs

public class Reflector
{
    public void ShowMembers(object o)
    {
        Type type = o.GetType();
        foreach (MemberInfo member in type.GetMembers())
        {
            Console.WriteLine("{0} is a {1}", member.Name, member.MemberType);
        }
    }
}

Running the above code against a button and you get something like:

...skip...
ManipulationDelta is a Event
ManipulationInertiaStarting is a Event
ManipulationBoundaryFeedback is a Event
ManipulationCompleted is a Event
IsDefaultProperty is a Field
IsCancelProperty is a Field
IsDefaultedProperty is a Field
...

So just to be clear:

Public string a;

is a Field, while

public string a { get; set; } 

would be a property.

I didn't actually answer the question. That's clever of me.

public class Reflector
{
    public void ShowMembers(object o)
    {
        Type type = o.GetType();
        foreach (MemberInfo member in type.GetMembers())
        {
            Console.WriteLine("{0} is a {1}", member.Name, member.MemberType);
        }
    }

    public void Set(object o, string fieldName, int val)
    {
        MemberInfo[] info = o.GetType().GetMember(fieldName);
        FieldInfo field = info[0] as FieldInfo;
        field.SetValue(o, val);
    }

    public int x;
}

If I call reflector.Set(reflector, "x", 10); so I'm calling Set on itself, the above method will set the value to 10. If you want to read the value it is GetValue.

2 Comments

sorry but i don't get it if i don't know the name of fieldName how to Use GetValue?
That's fair enough, all the bits are there. ShowMembers has a loop which iterates through all members of a class. If you run that code against your object, you'll see a list of all the members and what sort it is, i.e. Field, Event, etc. Set here actually sets a field to a value. So by combining both, putting a call to Set inside the loop of ShowMembers where MemberType== MemberType.Field you'll win :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.