This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static object GetPropValue(this object obj, string name) | |
{ | |
if (obj == null) | |
{ | |
return null; | |
} | |
foreach (string part in name.Split('.')) | |
{ | |
if (obj.IsNonStringEnumerable()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This will not work with nested object members. | |
var template = "Welcome back {Name}. You're logged in from {Profile.Address.Country.Code}. Have a good day."; | |
var user = new User { Name = "John", Email = "[email protected]" }; | |
var message = template.BindObjectProperties(user); | |
Console.WriteLine(message); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This will not work with nested object members. | |
var template = "Welcome back {Name}. You're logged in from {Profile.Address.Country.Code}. Have a good day."; | |
var user = new User { Name = "John", Email = "[email protected]" }; | |
var message = template.BindObjectProperties(user); | |
Console.WriteLine(message); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var template = "Welcome back {Name}. You're authenticated as {Email}. Have a good day."; | |
var user = new User { Name = "John", Email = "[email protected]" }; | |
var message = template.BindObjectProperties(user) | |
Console.WriteLine(message); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static string BindObjectProperties(this string source, object obj) | |
{ | |
if(obj == default) return source; | |
var type = obj.GetType(); | |
foreach(var parameter in ExtractParams(source)) | |
{ | |
source = source.Replace("{"+ parameter +"}", type.GetProperty(parameter)?.GetValue(obj)?.ToString()); | |
} | |
return source; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static IEnumerable<string> ExtractParams(string str) | |
{ | |
var splitted = str.Split('{', '}'); | |
for (int i = 1; i < splitted.Length; i += 2) | |
yield return splitted[i]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var template = "Welcome back {Name}. You're authenticated as {Email}. Have a good day."; | |
var user = new User { Name = "John", Email = "[email protected]" }; | |
foreach(var property in user.GetType().GetProperties()) | |
{ | |
template = template.Replace("{"+property.Name+"}", property.GetValue(user)); | |
} | |
Console.WriteLine(template); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var user = new User { Name = "John", Email = "[email protected]", Surname = "Doe" /* ... */}; | |
var properties = user.GetType().GetProperties(); | |
foreach(var property in properties) | |
{ | |
Console.WriteLine(property.Name + " : " + property.GetValue(user)); | |
} | |
/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var type = user.GetType(); // find type at runtime. | |
var property = type.GetProperty("Email"); // Get property info with string name. | |
var value = property.GetValue(user); // Gets Email property value of object which placed at 'user' variable. | |
Console.WriteLine(value); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var email = user.Email; // Member access with '.' operator. | |
Console.WriteLine(email); |