Reputation: 1728
I want to refactor some of my code, so I would like to create method for few repeating tasks. One of methods, depending on context, include working something with properties. Most of properties are string, but there are also enum and int types. For example, method should look like this:
private void someMethod (int i, 'here should be property') {
//enter code here
}
So, does anybody know how to pass this properties?
Thanks in advance!
Another explanation. This code should change label properties: text, font... But, label.Text should be changed depending on entry parameter.
it should look like this
private void setLabel (Label label, 'I dont know what goes here to pass a property') {
label.Text = user.'property'.toString();
//some more code
}
Upvotes: 0
Views: 1076
Reputation: 244757
If you don't have any particular reason why you want to actually pass the property, you can just pass the value of that property:
private void setLabel (Label label, object propertyValue)
{
label.Text = propertyValue.ToString();
}
And then call it like:
setLabel(myLabel, user.ThePropertyIWant);
Upvotes: 1
Reputation: 9990
You should be able to do such thing with reflection: http://www.dotnetperls.com/reflection-field
Upvotes: 1