Let's suppose I have the following class:
public class Person {
public string Name { get; set; }
public string Surname { get; set; }
public string FullName {
get {
return Name + " " + Surname;
}
}
}
The following block:
Person person = new Person();
person.Name = "Matt";
person.Surname = "Smith";
return person.FullName;
would return Matt Smith
.
Let's change our Person
type to a dynamic ExpandoObject
.
The code would look like this:
dynamic person = new ExpandoObject();
person.Name = "Matt";
person.Surname = "Smith";
But here's where I am stuck. How can I override the get
accessor of a new FullName
property?
I could achieve the same effect creating a new method:
person.GetFullName = (Func<string>)(() => {
return person.Name + " " + person.Surname;
});
But this would end up with a method and not a property, therefore calling it like:
person.GetFullName();
EDIT
Please note that I do not want to know how to define or create a new dynamic property. I would like to know how to override or define a get accessor for a dynamic property.
I picture the code can be something like this:
person.FullName.get = (Func<string>)(() => {
return person.Name + " " + person.Surname;
});
Then, invoked like this:
Console.WriteLine(person.FullName); //Prints out "Matt Smith"