How can I provide property-level string formatting?
Posted
by jhubsharp
on Stack Overflow
See other posts from Stack Overflow
or by jhubsharp
Published on 2010-05-21T14:12:48Z
Indexed on
2010/05/21
14:20 UTC
Read the original article
Hit count: 128
Not sure if the question wording is accurate, but here's what I want to do: I want to databind a class with some strings in it:
class MyClass
{
public string MyProperty { get; set; }
public string MyProperty2 { get; set; }
}
When databinding, everything behaves normally. On the back end, I'm writing to a file and I want MyProperty2 to always be encrypted using some encryption algorithm. I want my back-end code to write each string without needing to know that encryption is required (I want the class to know it should be encrypted, not the consumer). Can I do this with a type converter, or something similar?
EDIT: There are other scenarios as well. Some booleans I want to format as "Y" or "N", other booleans I want formatted as "Enabled" / "Disabled", etc. I can write (and have written) helper methods and let the file writer call the helper methods as appropriate, I'm just wondering if there's a way to do this without the file writer needing to know which objects need which kind of formatting and let the objects tell that to the file writer.
© Stack Overflow or respective owner