I created a class that extends DynamicObject:
public class DynamicEntity : DynamicObject
{
private readonly Dictionary<string, object> values = new Dictionary<string, object>();
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
values.TryGetValue(binder.Name, out result);
return true;
}
public override bool TrySetMember(SetMemberBinder binder, object value)
{
values[binder.Name] = value;
return true;
}
}
I can add property to the instance of my class. For example:
dynamic person = new DynamicEntity();
person.firstName = "John";
person.birthDate = "January 2, 1990";
I can also use methods and properties of string:
Console.WriteLine(person.firstName.Length);
Console.WriteLine(person.firstName.Contains("ohn"));
However, using extension method,
bool empty = person.firstName.IsEmpty();
gives me an error 'string' does not contain a definition for 'IsEmpty'.
The error can only be suppressed when I cast the property to string:
bool empty = ((string)person.firstName).IsEmpty();
I wonder why can I use string methods without casting while I cannot use extension methods without casting.
Any idea why can't I use extension methods without casting?