I am getting an error that says:
'object' does not contain a definition for 'Title'
all the code is also on github
I have a ConsoleApplication1 that looks like this
namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Movie m = new Movie(); var o = new { Title = "Ghostbusters", Rating = "PG" }; Console.WriteLine(m.PrintMovie(o)); } } }
and Movie.cs
public class Movie : DynamicObject { public string PrintMovie(dynamic o) { return string.Format("Title={0} Rating={1}", o.Title, o.Rating); } }
it works fine from the SAME project, but if I add ConsoleApplication2 with a reference to ConsoleApplication1 and add the Exact same code
namespace ConsoleApplication2 { class Program { static void Main(string[] args) { Movie m = new Movie(); var o = new { Title = "Ghostbusters", Rating = "PG" }; Console.WriteLine(m.PrintMovie(o)); } } }
I get an error:
'object' does not contain a definition for 'Title'**
even though it is in the dynamic object.
Here is a screen shot:
I am doing something like this and trying to call the movie function from a test project.
Dynamic objects expose members such as properties and methods at run time, instead of at compile time. This enables you to create objects to work with structures that do not match a static type or format.
For ExpandoObject, you can simply check whether the property is defined as a key in the underlying dictionary. For other implementations, it might be challenging and sometimes the only way is to work with exceptions.
The ExpandoObject class enables you to add and delete members of its instances at run time and also to set and get values of these members. This class supports dynamic binding, which enables you to use standard syntax like sampleObject. sampleMember instead of more complex syntax like sampleObject.
Jahamal's answer doesn't say why you get the error. The reason is that the anonymous class is internal
to the assembly. Keyword dynamic
doesn't allow you to bypass member visibility.
The solution is to replace the anonymous class with named public class.
Here's another good example explaining the reason and another possible solution.
The reason the call to
data2.Person
fails is that the type information ofdata2
is not available at runtime. The reason it's not available is because anonymous types are not public. When the method is returning an instance of that anonymous type, it's returning aSystem.Objec
t which references an instance of an anonymous type - a type whose info isn't available to the main program. The dynamic runtime tries to find a property calledPerson
on the object, but can't resolve it from the type information it has. As such, it throws an exception. The call todata.Name
works fine sincePerson
is a public class, that information is available and can be easily resolved.
This can affect you in any of the following cases (if not more):
- You're returning a non-public, non-internal type using
System.Object
. 2. You're returning a non-public, non-internal derived type via a public base type and accessing a property in the derived type that's not in the base type. 3. You're returning anything wrapped inside an anonymous type from a different assembly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With