Thursday 20 September 2018

Passing properties by reference in C#




I'm trying to do do the following:



GetString(
inputString,
ref Client.WorkPhone)

private void GetString(string inValue, ref string outValue)
{
if (!string.IsNullOrEmpty(inValue))
{

outValue = inValue;
}
}


This is giving me a compile error. I think its pretty clear what I'm trying to achieve. Basically I want GetString to copy the contents of an input string to the WorkPhone property of Client.



Is it possible to pass a property by reference?


Answer



Properties cannot be passed by reference. Here are a few ways you can work around this limitation.




1. Return Value



string GetString(string input, string output)
{
if (!string.IsNullOrEmpty(input))
{
return input;
}
return output;

}

void Main()
{
var person = new Person();
person.Name = GetString("test", person.Name);
Debug.Assert(person.Name == "test");
}



2. Delegate



void GetString(string input, Action setOutput)
{
if (!string.IsNullOrEmpty(input))
{
setOutput(input);
}
}


void Main()
{
var person = new Person();
GetString("test", value => person.Name = value);
Debug.Assert(person.Name == "test");
}


3. LINQ Expression




void GetString(string input, T target, Expression> outExpr)
{
if (!string.IsNullOrEmpty(input))
{
var expr = (MemberExpression) outExpr.Body;
var prop = (PropertyInfo) expr.Member;
prop.SetValue(target, input, null);
}
}


void Main()
{
var person = new Person();
GetString("test", person, x => x.Name);
Debug.Assert(person.Name == "test");
}


4. Reflection




void GetString(string input, object target, string propertyName)
{
if (!string.IsNullOrEmpty(input))
{
prop = target.GetType().GetProperty(propertyName);
prop.SetValue(target, input);
}
}

void Main()

{
var person = new Person();
GetString("test", person, nameof(Person.Name));
Debug.Assert(person.Name == "test");
}

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...