In C# it is impossible to pass a property by reference.
This is due to the fact that properties are actually methods in disguise.
They behave like members (which are real objects), but are methods in nature.
Probably, this is done to enhance memory allocation issues for objects in C#.
Whatever the reason is, I faced the problem with passing those properties into the function so that the function alters them.
Initially I tried to do some workarounds using delegates (pointers to the methods).
However, they were not flexible enough to meet my needs.
Here is the final solution, which is based on the Microsoft’s “reflection” library.
public static void assignValueByRef(object obj, string propertyName, object src)
{
PropertyInfo propInfo = obj.GetType().GetProperty(propertyName);
//if “property to set” is INT
if (propInfo.GetValue(obj, null).GetType() == typeof(int))
{
if (src.GetType() == typeof(TextBox))
{
if (((TextBox)src).Text.Trim() != “”)
{
propInfo.SetValue(obj, Convert.ToInt32(((TextBox)src).Text.Trim()), null);
}
}
else if (src.GetType() == typeof(DropDownList))
{
if (((DropDownList)src).SelectedValue.ToString() != “”)
{
propInfo.SetValue(obj, Convert.ToInt32(((DropDownList)src).SelectedValue.ToString()), null);
}
}
}
}
We call the method as follows:
Utilities.assignValueX(objJF,
“PloshadObshaia”,
bridgeControl.FindControl(“FormView1”).FindControl(“dataPloshadObshaia”));
where “PloshadObshaia” is the name of the property of objJF object.