Skip to content

Instantly share code, notes, and snippets.

View enisn's full-sized avatar

Enis Necipoglu enisn

View GitHub Profile
@enisn
enisn / BindingExtensions.cs
Last active August 2, 2022 07:43
Challenge #2 - Solution - GetPropValue
public static object GetPropValue(this object obj, string name)
{
if (obj == null)
{
return null;
}
foreach (string part in name.Split('.'))
{
if (obj.IsNonStringEnumerable())
@enisn
enisn / StringBinding.cs
Created May 7, 2020 21:03
Challenge #2 - Solution - 6
// 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);
@enisn
enisn / StringBinding
Created May 7, 2020 21:01
Challenge #2 - Solution - 6
// 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);
@enisn
enisn / StringBinding.cs
Created May 7, 2020 20:58
Challenge #2 - Solution - 5
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);
@enisn
enisn / BindingExtensions.cs
Created May 7, 2020 20:56
Challenge #2 - Solution - BindObjectProperties
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;
@enisn
enisn / ModelBinderExtensions.cs
Created May 7, 2020 20:52
Challenge #2 - Solution - ExtractParams
private static IEnumerable<string> ExtractParams(string str)
{
var splitted = str.Split('{', '}');
for (int i = 1; i < splitted.Length; i += 2)
yield return splitted[i];
}
@enisn
enisn / StringBinding.cs
Created May 7, 2020 20:47
Challenge #2 - Solutiong - 4
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);
@enisn
enisn / StringBinding.cs
Created May 7, 2020 20:40
Challenge #2 - Solution - 3
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));
}
/*
@enisn
enisn / StringBinding.cs
Created May 7, 2020 20:32
Challenge #2 - Solutiont - 2
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);
@enisn
enisn / StringBinding.cs
Created May 7, 2020 20:28
Challenge #2 - solution - 1
var email = user.Email; // Member access with '.' operator.
Console.WriteLine(email);