Dynamic does not contain a property definition from a project link

I get an error message:

"object" does not contain a definition for "Title"

all code is also on github

I have ConsoleApplication1 that looks like

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 in the SAME project, but if I add ConsoleApplication2 with reference to ConsoleApplication1 and add the exact 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 message:

'object' does not contain a definition for 'Title' **

although it is in a dynamic object.

  • o.Title 'o.Title' threw an exception of type 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException' dynamic {Microsoft.CSharp.RuntimeBinder.RuntimeBinderException}

Here is a screenshot: enter image description here

I am doing something like this and trying to call a movie function from a test project.

+79
c # dynamic dynamicobject
Feb 23 2018-12-12T00:
source share
3 answers

You need to use ExpandoObject

  dynamic o = new ExpandoObject(); o.Title = "Ghostbusters"; o.Rating = "PG"; Console.WriteLine(m.PrintMovie(o)); 
+73
Feb 23 '12 at 16:40
source share

Jahamal's answer does not say why you get the error. The reason is because the anonymous class is internal for assembly. The dynamic keyword does not bypass element visibility.

The solution is to replace the anonymous class with a named public class.

Here is another good example explaining the reason and another possible solution .

The reason that the call to data2.Person fails is because information about the data2 type data2 not available at runtime. The reason it is not available is because anonymous types are not publicly available. When a method returns an instance of this anonymous type, it returns System.Objec t, which refers to an instance of an anonymous type - a type whose information is not available to the main program. The dynamic runtime tries to find a property called Person for the object, but cannot remove it from the type information it has. Thus, it throws an exception. Calling data.Name works fine, as Person is a public class, this information is available and can be easily resolved.

This may affect you in any of the following cases (if not more):

  • You are returning a non-public non-internal type using System.Object .
  • You return a non-public non-derived derived type through a public base type and get access to the property in a derived type that is not a base type.
  • You are returning something wrapped in an anonymous type from another assembly.
+124
Mar 20 '14 at 18:55
source share

In my case, I had a Unit Test project that I created in Visual Studio, and in many cases when I needed to test methods in a data layer library. I did not want to change them, so I marked the test assembly as a friend using:

[assembly: InternalsVisibleTo ("MyDataLayerAssemblyName")]

And that decided.

Example:

 using System.Runtime.CompilerServices; using Microsoft.VisualStudio.TestTools.UnitTesting; [assembly: InternalsVisibleTo( "MyDataLayerAssembly" )] namespace MyUnitTestProject.DataTests { [TestClass] public class ContactTests { ... 

References: InternalsVisibleToAttribute Class

Friends Collections

+25
Apr 16 '15 at 19:46
source share



All Articles