我得到一個錯誤,指出:動態不包含從項目引用的一個屬性的定義
'object' does not contain a definition for 'Title'
所有的代碼還對github
我有一個ConsoleApplication1,看起來像這樣
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參考,並添加完全相同的代碼
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));
}
}
}
我得到一個錯誤:
'object' does not contain a definition for 'Title'**
即使它在動態的對象中。
- o.Title 'o.Title' 投擲類型的異常 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException' 動態{Microsoft.CSharp.RuntimeBinder.RuntimeBinderException}
這裏是一個屏幕截圖:
我正在做這樣的事情,並試圖從測試項目調用電影功能。
可能重複://計算器.com/questions/2630370/c-sharp-dynamic-can not-access-properties-from-anonymous-types-declared-in-anot) – 2012-02-23 16:22:41