I’ve got an application that’s fairly heavily based on custom classes, and am hitting a wall on attempting to do a custom replacement based on the type that a given replacement token is.
What I’d like to do (looking at the code below) is given a certain string, loop through the _tokens object. For each token found, identify the type of object provided in the TokenReplacement and replace it with the value of the same object, provided by the User object.
Any help or other approaches toward this would be much appreciated.
Edit: Forgot to mention that this gives me a stack overflow error.
private BasicUser BU = new BasicUser();
private readonly List<Token> _tokens = new List<Token>
{
new Token
{
TokenName = "Lan ID",
TokenIdentifier = "<!--LANID-->",
TokenReplacement = this.BU.LanID
},
new Token
{
TokenName = "First Name",
TokenIdentifier = "<!--FirstName-->",
TokenReplacement = new BasicUser().FirstName
},
new Token
{
TokenName = "Last Name",
TokenIdentifier = "<!--LastName-->",
TokenReplacement = new BasicUser().LastName
}
};
public string ReplaceTokens(string Input, string LanID)
{
string OutputString = "";
BasicUser User = GetParticipantInformation(Input);
foreach (var token in _tokens)
{
token.TokenReplacement.GetType();
OutputString = OutputString.Replace(token.TokenName, "Token replacement");
}
return OutputString;
}
Yes, you can use reflection. Just use
PropertyInfotype forTokenReplacementand assign to it the property you need like the following:If you don’t need to this to be based on reflection, you can also go Matti Virkkunen’s way which allows to specify some more complex queries on objects than simple property read.