1

Sorry if the tile is misleading. What i would like to do is to use a string to get the values from a class. What i have:

class foo
{
    public string field1 {get;set;}
    public string field2 {get;set;}
}

public void run()
{
    //Get all fields in class
    List<string> AllRecordFields = new List<string>();
    Type t = typeof(foo);
    foreach (MemberInfo m in t.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
    {
        AllRecordFields.Add(m.Name);
    }

    foo f = new foo();
    foreach(var field in AllRecordFields)
    { 
        //field is a string with the name of the real field in class
        f.field = "foobar";
    }
}

This a really simple example, so the problem is on the line f.field = "foobar"; The field is a string with a name of the real class field what i want to assignt the value to.

5
  • 1
    what exactly do you want to achieve ?? Commented Nov 18, 2011 at 8:56
  • Do you want to set the value of foo.field1 or do you want to change the name of foo.field1 to e.g. foo.bar1? Commented Nov 18, 2011 at 8:56
  • In this example i want to set all the class foo field values to "foobar". Commented Nov 18, 2011 at 8:58
  • You also have FieldInfo.GetValue not sure as what are you looking out for ? Commented Nov 18, 2011 at 9:01
  • if you want to set field values of class to "foobar" then it is simple right, what is the problem i didn't get. just make your field public Commented Nov 18, 2011 at 9:04

4 Answers 4

3

Use PropertyInfo instead of MemberInfo and then SetValue.

public void run()
{
  foo f = new foo();
  Type t = typeof(foo);

  foreach (PropertyInfo info in t.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
  {
     info.SetValue(f, "foobar", new object[0]);
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

FYI The question was changed, you need PropertyInfo and GetProperties now
0

First of all, it is better to use Properties instead of fields. Second your fields are private, can't be accessed from outside foo. You need to declare them as public.

5 Comments

Using reflection you can access a private field. Sure it is not a good practice in common, but some applications and libraries like ORM need this kind of access.
Hi @H-Man2 when do ORM need to access your internal implementation? fields should be hidden, if they need to access your inner core something is wrong.
No there is nothing wrong, shure it breaks encapsulating. You need this access for generic algorithms, like (de-)serialization (as used in .NET itself).
Thanks for the explanation, some resources about that?
I found two possibly interesting links: 1 2
0

For your example you have to use reflection to access those files. But that is slow and it is not very good style. You better use the class directly (with property setter) or us an interface.

Comments

0

Add method into foo class to change all properties

   class foo
    {
        public string field1 {get;set;}
        public string field2 { get; set; }

        public void SetValueForAllString( string value)
        {
            var vProperties = this.GetType().GetProperties();
            foreach (var vPropertie in vProperties)
            {
                if (vPropertie.CanWrite 
                    && vPropertie.PropertyType.IsPublic 
                    && vPropertie.PropertyType == typeof(String))
                {
                    vPropertie.SetValue(this, value, null);
                }
            }

        }
    }

    foo f = new foo() { field1 = "field1", field2 = "field2" };
                f.SetValueForAllString("foobar");
                var field1Value = f.field1; //"foobar"

             var field2Value = f.field2; //"foobar"

Comments

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.