How do you create a domain-specific string class in .NET?
Given that in .NET the System.String
class is sealed, how do yo开发者_Go百科u create a simple domain-specific string class so you can use type checking to ensure that the correct sort of string is used? Do you have a favourite implementation of such a class?
You might, for example, want to create a class to represent an email address to ensure that email is not accidentally sent to someone's postal address. Although that's not a great example I'm thinking about the cases where all you need is the type safety but no additional logic or validation.
You can wrap the string class in one of your own and add all the type checking you want.
Think composition, instead of inheritance.
You might be interested in http://msdn.microsoft.com/en-us/library/system.net.mail.mailaddress.aspx in terms of your e-mail address example.
精彩评论