I am trying to figure out how to call a class method on a property of that class. Here are my two classes:
public class MrBase
{
public int? Id { get; set; }
public String Description { get; set; }
public int? DisplayOrder { get; set; }
public String NullIfEmpty()
{
if (this.ToString().Trim().Equals(String.Empty))
return null;
return this.ToString().Trim();
}
}
public class MrResult : MrBase
{
public String Owner { get; set; }
public String Status { get; set; }
public MrResult() {}
}
MrResult inherits from MrBase.
Now, I want to be able to call the NullIfEmpty method on any of the properties of these classes… like so:
MrResult r = new MrResult();
r.Description = "";
r.Description.NullIfEmpty();
r.Owner = "Eric";
r.Owner.NullIfEmpty();
Thanks.
Eric
You should write an extension method for
string:Usage:
If your main goal is to automatically “modify” each
stringproperty of your classes, you can achieve this using reflection. Here’s a basic example: