我如何添加一个方法到财产?财产、方法

2023-09-04 02:12:45 作者:笔尖ㄣ的徘徊

让我们说我创建一个类的属性:

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; }
   }
}

我有什么做的类和属性,以便能够具有连接到UserID属性,方法如方法生成一个XML各地的用户ID与点语法:

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();

我可以弄清楚如何写一个方法把XML标记周围的用户ID的值,即躲避我是如何使用点语法,使与财产法工作的部分。

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.

所有这些答案都是有用的,感谢大家的贡献。事实上,我标记为答案接受是precisely什么,我一直在寻找。我AP preciate的扩展方法的警示remakrs(我在这之前从来没有听说过的),当然这可能是一个问题的扩展方法适用于在某些情况下所有的字符串,但在这种情况下,我肯定要到一个方法toXML()适用于类中的所有字符串属性。正是医生下令。我很熟悉XmlSerialization,但在这种情况下,根据需要,以避免它由于各种原因。

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.

推荐答案

您需要的方法添加到属性的键入。在这种情况下,用户ID是一个字符串,那么这将意味着将它添加到字符串类型,使之从的所有的字符串进行访问。我不知道这是一个好主意,但如果这是真的想你想要做的:

You have to add the method to the type of the property. In this case the UserID is a string, so that will mean adding it to the string type and making it accessible from all strings. I'm not sure that's a good idea, but if that's really want you want to do:

public static class StringUtils
{
    public string ToXmlElement(this string s, string elementName)
    {
       return string.Format("<{0}>{1}</{0}>", elementName, s);
    }
}

这不正是你的函数签名匹配,但它的东西,使更多一点意义附加到较大的字符串类。唯一的其他选择是使用一个不同的基本类型牵你的用户名,也许隐式转换为字符串自定义类。

This doesn't exactly match your function signature, but it's something that makes a little more sense to attach to the larger string class. The only other option is to use a different underlying type to hold your UserID, perhaps a custom class with an implicit conversion to string.

最后,如果你只是想编写你的用户类到XML的通用方式,看看的System.Xml.Serialization 命名空间。然后,你可以标记你的类像这样和使用的XmlSerializer 实例来读取或写入类。

Finally, if you're just trying to write your user class to Xml in a generic way, look at the System.Xml.Serialization namespace. Then you can just mark up your class like this and use an XmlSerializer instance to read or write the class.

[Serializable]
public class User
{
   private string _userID;

   [XmlElement()]
   public string UserID
   {
      get { return _userID; }
      set { _userID = value; }
   }
}
 
精彩推荐
图片推荐