动态不从项目引用包含属性的定义 [英] dynamic does not contain a definition for a property from a project reference

查看:170
本文介绍了动态不从项目引用包含属性的定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到一个错误,指出对象不包含标题

I am getting an error that says 'object' does not contain a definition for 'Title'

所有code也在 github上

我有一个ConsoleApplication1,看起来像这样

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));
        }
    }
} 

Movie.cs

public class Movie : DynamicObject
{
    public string PrintMovie(dynamic o)
    {
        return string.Format("Title={0} Rating={1}", o.Title, o.Rating);
    }
} 

它工作正常,从相同的项目,但如果我添加ConsoleApplication2与参考ConsoleApplication1并添加完全相同的code

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

  • 在o.Titleo.Title'引发了异常类型的Micr​​osoft.CSharp.RuntimeBinder.RuntimeBinderException动态{Microsoft.CSharp.RuntimeBinder.RuntimeBinderException}

下面是一个屏幕截图。

Here is a screen shot.

我在做这样的事情,并试图调用从测试项目中的短片拍摄功能。

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

感谢

推荐答案

您需要使用ExpandoObject

You need to use an ExpandoObject

 dynamic o = new ExpandoObject();
 o.Title = "Ghostbusters";
 o.Rating = "PG";

 Console.WriteLine(m.PrintMovie(o));

这篇关于动态不从项目引用包含属性的定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆