Last active
August 29, 2015 14:19
-
-
Save arn-ob/d625e2c4b25218ddb77a to your computer and use it in GitHub Desktop.
By Using DynamicObject is to send a binder(Data) And get the data output . And also get the time date
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
#region Dynamic Object | |
class dynamicClass : DynamicObject | |
{ | |
Dictionary<string, object> _dynamicData = new Dictionary<string, object>(); | |
public override bool TryGetMember(GetMemberBinder binder, out object result) | |
{ | |
bool success = false; | |
result = null; | |
if (_dynamicData.ContainsKey(binder.Name)) | |
{ | |
result = _dynamicData[binder.Name]; | |
success = true; | |
} | |
else | |
{ | |
result = "Property Not Found"; | |
success = false; | |
} | |
return success; | |
} | |
public override bool TrySetMember(SetMemberBinder binder, object value) | |
{ | |
_dynamicData[binder.Name] = value; | |
return true; | |
} | |
public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result) | |
{ | |
dynamic method = _dynamicData[binder.Name]; | |
result = method((DateTime)args[0]); | |
return result != null; | |
} | |
} | |
class Program | |
{ | |
static void Main() | |
{ | |
dynamic dynamicProg = new dynamicClass(); | |
dynamicProg.FirstName = "Bug"; | |
dynamicProg.LastName = "Bunny"; | |
Console.WriteLine(dynamicProg.GetType()); | |
Console.WriteLine("{0} {1}", dynamicProg.FirstName, dynamicProg.LastName); | |
/// Use of DateTime | |
Func<DateTime, string> GetTomorrow = today => today.AddDays(1).ToShortDateString(); | |
dynamicProg.GetTomorrowDate = GetTomorrow; | |
Console.WriteLine("Tomorrow is {0}", dynamicProg.GetTomorrowDate(DateTime.Now)); | |
Console.ReadLine(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can you try something like this? I think dynamicClass shouldnt be responsible for parsing your arg to a DateTime