Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

reflection - SetField

Posted on 2006-05-25
7
Medium Priority
?
420 Views
Last Modified: 2010-05-19
hi there

i have a class that i want to set its own fields/properties dynamically.

the class looks like this

public class person

{
private string name = string.Empty

public void Populate(string fieldName, string fieldValue)
{

string[] myFieldValue = new string[] { fieldValue };

//note field name will be "person";

type.InvokeMember(fieldName, BindingFlags.SetField , null, this, myFieldValue );                               

}
  }

unfortunately this doesnt work - i always get an exception saying cannot find field name "name"??

any ideas -

thanks very much

sean.
0
Comment
Question by:MrKevorkian
  • 4
  • 3
7 Comments
 
LVL 1

Author Comment

by:MrKevorkian
ID: 16759355
ok just found out that it works if i set the private field to public.

is there anyway to keep them private and still write to them?

for some reason in our team we are not allowed to use set properties (we have to populate via constructors for everything) - so i can go down that route.

thanks
0
 
LVL 2

Accepted Solution

by:
__alex earned 2000 total points
ID: 16782707
Ever tried BindingFlags.SetField | BindingFlags.NonPublic ?

I usually use FieldInfo:
someObject.GetType().GetField("nameOfField", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(someObject, newValue);
0
 
LVL 1

Author Comment

by:MrKevorkian
ID: 16788025
cool thanks
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:MrKevorkian
ID: 16788479
update - actually  BindingFlags.SetField | BindingFlags.NonPublic still doesnt find my private fields.  damn.

i think im just going to use a method to set them.
0
 
LVL 2

Expert Comment

by:__alex
ID: 16788538
namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1() {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e) {
            Person p = new Person();
            p.Populate_1("name", "MrKevorkian");
            p.ShowName();
            p.Populate_2("name", "Sean");
            p.ShowName();
        }
    }

    public class Person
    {
        private string name = string.Empty;

        public void Populate_1(string fieldName, string fieldValue) {
            this.GetType().InvokeMember(fieldName, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.SetField, null, this, new object[] { fieldValue });
        }

        public void Populate_2(string fieldName, string fieldValue) {
            this.GetType().GetField(fieldName, BindingFlags.Instance | BindingFlags.NonPublic).SetValue(this, fieldValue);
        }

        public void ShowName() {
            MessageBox.Show(name);
        }
    }
}
0
 
LVL 1

Author Comment

by:MrKevorkian
ID: 16798078
this works - thanks
0
 
LVL 2

Expert Comment

by:__alex
ID: 16798090
You're welcome.
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question