Closed maxshenfield closed 7 years ago
Hmmm... good catch!
Not sure how this can be fixed though. The dynamic internals handle the routing. Ultimately if you use the dynamic invocations you end up responsible for the type safety.
I'll take a look.
Ok I've added some logic that now throws when the assignment of type is invalid.
[TestMethod]
[ExpectedException(typeof(RuntimeBinderException))]
public void InvalidAssignmentErrorOnStaticProperty()
{
dynamic dynUser = new User(); // has string Name
dynUser.Name = 100; // RuntimeBinderException
// this should never run
var user = dynUser as User;
user.Name = "Rick";
Console.WriteLine(user.Name);
Console.WriteLine(user["Name"]);
Assert.Fail("Invalid Assignment should have thrown exception");
//>> 100
}
Inheriting from Expando is great - but if the inheriting class implements strongly typed properties, setting a property dynamically causes some problems. If the types don't agree, casting the dynamic instance back to its static type will give different values for the property depending on whether you use the indexer or property to access it. For example