Object text tokens is a library that allows you to do text templating within your .net objects. Use it to provide simple text templating within your application.
- Object Properties (string, int, double, etc.)
- Child Object Properties
- Properties using List or Arrays of strings (output as comma separated lists)
- Object Properties (string)
- Dictionary Collections Dictionary<K,V> where V is string
- IEnumerable<KeyValuePair<string, string>
- IEnumerable
- string[]
- IEnumerable where T is Class
- Cascading token replacement (tokens that depend on other tokens to be replaced first)
- Object Methods
- Private Fields
By default tokens that are not found with be replaced with an empty string. If you would prefer to get an error when a token isn't matched, set ObjectTextFieldTokenization.ThrowOnUnfoundToken = true;
Use the NuGet package manager to install ObjectTextTokens.
Install-Package ObjectTextTokens
Use case insensitive tokens in the form of @object.property@, as placeholders within the text of your object properties.
using ObjectTextTokens;
// Example: Single object with templating and data
var objectSchema = new YourObject {
Property1 = "Dog",
Property2 = "@property1@",
Child = new ChildObject {
ChildProp1 = "Cat",
ChildProp2 = "@Property1@",
ChildArray = new string[] { "Chicken", "Eagle" }
},
Property3 = "@child.childProp@",
Property4 = "@child.childArray@, @property1@"
};
ObjectTextFieldTokenization.Tokenize(objectSchema);
// Returns
// objectSchema.Property2 // "Dog";
// objectSchema.Property3 // "Cat";
// objectSchema.Property4 // "Chicken, Eagle, Dog"
// objectSchema.Child.ChildProp2 // "Dog"
// Example: Templated object and separate object with lookup data
var templatedObj = new YourObject {
Property2 = "@property1@",
Property3 = "@child.childProp@"
};
var lookupObj = new YourOtherObject {
Property1 = "Dog",
Child = new ChildObject {
ChildProp1 = "Cat"
}
};
ObjectTextFieldTokenization.Tokenize(templatedObj, lookupObj);
// Returns
// templatedObj.Property2 // "Dog";
// templatedObj.Property3 // "Cat";
Currently this is a static class. The implication for testing is that your code will run through this class when testing. If this is problematic wrap this code with an interface before using in your project.
- Future work might be on providing an instantiatable class and an interface for injecting, along with a static wrapper.
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.