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
public static string BindObjectProperties(this string str, object obj) | |
{ | |
if (obj == null) return str; | |
foreach (var item in ExtractParams(str)) | |
{ | |
str = str.Replace("{" + item + "}", obj.GetPropValue(item)?.ToString()); | |
} | |
return str; | |
} |
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 work perfectly for nested objects. | |
var template = "Welcome back {Name}. You're logged in from {Profile.Address.Country.Code}. Have a good day."; | |
var user = new User { Name = "John", Address = new Address { Country = new Country { Code = "US" } } }; | |
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
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.Reflection; | |
namespace SomeCompany.Extensions | |
{ | |
public static class BindingExtensions | |
{ | |
public static string BindObjectProperties(this string str, object obj) |
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 void DoSomething(string message) | |
{ | |
if(message == null) | |
throw new ArgumentNullException(); | |
// ... | |
} |
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 void DoSomething([NotNull]string message) // Does not affect anything at runtime. | |
{ | |
} | |
public void AnotherMethod() | |
{ | |
DoSomething(null); // MsBuild doesn't allow to build. | |
string parameter = null; | |
DoSomething(parameter); // MsBuild allows build. But nothing happend at runtime. | |
} |
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 class NotNull<T> | |
{ | |
public NotNull(T value) | |
{ | |
this.Value = value; | |
} | |
public T Value { get; set; } | |
public static implicit operator NotNull<T>(T 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
static void Main(string[] args) | |
{ | |
DoSomething("Hello World!"); // Works perfectly 👌 | |
DoSomething(null); // Throws ArgumentNullException at runtime. | |
string parameter = null; | |
DoSomething(parameter); // Throws ArgumentNullException at runtime. | |
} |
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 void DoSomething(NotNull<string> message, NotNull<int> id, NotNull<Product> product) | |
{ | |
// ... | |
} |
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 interface IDataProvider<TData> | |
{ | |
TData GetData(); | |
} |