Created
January 10, 2016 21:14
-
-
Save miklund/7da52916024540dbfed8 to your computer and use it in GitHub Desktop.
2011-12-18 Failure of the dynamic keyword
This file contains hidden or 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
# Title: Failure of the dynamic keyword | |
# Author: Mikael Lundin | |
# Link: http://blog.mikaellundin.name/2011/12/18/failure-of-the-dynamic-keyword.html |
This file contains hidden or 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 DynamicMatch : DynamicObject | |
{ | |
private readonly Match match; | |
public DynamicMatch(Match match) | |
{ | |
this.match = match; | |
} | |
public override bool TryGetMember(GetMemberBinder binder, out object result) | |
{ | |
var prop = match.GetType().GetProperty(binder.Name); | |
// Match instance property | |
if (prop != null) | |
{ | |
result = prop.GetValue(match, null); | |
return true; | |
} | |
// Group value | |
Group group; | |
if ((group = this.match.Groups[binder.Name]) != null) | |
{ | |
result = group.Value; | |
return true; | |
} | |
result = null; | |
return false; | |
} | |
} |
This file contains hidden or 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 DynamicRegex | |
{ | |
private readonly Regex expression; | |
public DynamicRegex(string expression) | |
{ | |
this.expression = new Regex(expression); | |
} | |
public dynamic Match(string input) | |
{ | |
return new DynamicMatch(expression.Match(input)); | |
} | |
} |
This file contains hidden or 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 object GetEmployee() | |
{ | |
return new { Name = "John", Profession = "Santa" }; | |
} | |
object employee = GetEmployee(); |
This file contains hidden or 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 dynamic GetEmployee() | |
{ | |
dynamic employee = new { Name = "John", Profession = "Santa" }; | |
return employee; | |
} | |
var employee = GetEmployee(); | |
Console.WriteLine("Employee of the month: {0}, {1}", employee.Name, employee.Profession); |
This file contains hidden or 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
string time = "12:00"; | |
var match = Regex.Match(time, @"(?<Hour>\d{2}):(?<Minute>\d{2})"); | |
if (match.Success) | |
{ | |
var hour = match.Groups["Hour"].Value; | |
var minute = match.Groups["Minute"].Value; | |
Console.WriteLine("The time is {0}.{1}", hour, minute); | |
} |
This file contains hidden or 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
string time = "12:00"; | |
var match = new DynamicRegex(@"(?<Hour>\d{2}):(?<Minute>\d{2})").Match(time); | |
if (match.Success) | |
{ | |
Console.WriteLine("The time is {0}.{1}", match.Hour, match.Minute); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment