Let's say I create a class with a property:
public class User
{
private string _userID;
public string UserID
{
get { return _userID; }
set { _userID = value; }
}
}
What do I have to do with the class and property to be able to have a method attached to the UserID property, such as a method that generates Xml around the user ID with the "dot" syntax:
User u = new User();
u.UserID = "Mike";
string xml = u.UserID.ToXml();
I can figure out how to write a method to put Xml tags around the value of the UserID, the part that is eluding me is how to make the method work with the property using the "dot" syntax.
All of these answers are useful, and thanks to everyone for contributing. In fact, the answer I marked as "accepted" was precisely what I was looking for. I appreciate the cautionary remakrs on extension methods (which I had never heard of before this), and of course it could be a problem to apply the extension method to all strings in some circumstances, but in this case I definitely wanted to apply the method ToXml() to all string properties in the class. Just what the doctor ordered. I am quite familiar with XmlSerialization, but in this case needed to avoid it for various reasons.