Programing

dynamic은 프로젝트 참조의 속성에 대한 정의를 포함하지 않습니다.

lottogame 2020. 10. 9. 08:41
반응형

dynamic은 프로젝트 참조의 속성에 대한 정의를 포함하지 않습니다.


다음과 같은 오류가 발생합니다.

'개체'에 '제목'에 대한 정의가 없습니다.

모든 코드는 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);
    }
} 

동일한 프로젝트에서 잘 작동하지만 ConsoleApplication1에 대한 참조와 함께 ConsoleApplication2를 추가하고 정확히 동일한 코드를 추가하면

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

오류가 발생합니다.

'객체'에 '제목'에 대한 정의가 없습니다 **

동적 개체에 있더라도.

  • o.Title 'o.Title'에서 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException'유형의 예외가 발생했습니다. dynamic {Microsoft.CSharp.RuntimeBinder.RuntimeBinderException}

다음은 스크린 샷입니다. 여기에 이미지 설명 입력

나는 이와 같은 일을하고 테스트 프로젝트에서 영화 함수를 호출하려고합니다.


ExpandoObject를 사용해야합니다.

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

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

Jahamal의 대답은 오류가 발생하는 이유말하지 않습니다 . 그 이유는 익명 클래스가 internal어셈블리에 있기 때문입니다 . 키워드로 dynamic회원 가시성을 우회 할 수 없습니다.

해결책은 익명 클래스를 명명 된 공용 클래스로 바꾸는 것입니다.

그 이유와 또 다른 가능한 해결책을 설명하는 또 다른 좋은 예가 있습니다 .

The reason the call to data2.Person fails is that the type information of data2 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 a System.Object which references an instance of an anonymous type - a type who's info isn't available to the main program. The dynamic runtime tries to find a property called Person on the object, but can't resolve it from the type information it has. As such, it throws an exception. The call to data.Name works fine since Person 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):

  1. 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.

In my case I had a Unit Test project that I created on Visual Studio and a lot of cases where I needed to test methods on a data layer library. I didn't want to change all of them so I marked the test assembly as a friend by using:

[assembly:InternalsVisibleTo("MyDataLayerAssemblyName")]

And that solved it.

Example:

using System.Runtime.CompilerServices;
using Microsoft.VisualStudio.TestTools.UnitTesting;

[assembly: InternalsVisibleTo( "MyDataLayerAssembly" )]
namespace MyUnitTestProject.DataTests
{

   [TestClass]
   public class ContactTests
   {
      ...

References:

참고URL : https://stackoverflow.com/questions/9416095/dynamic-does-not-contain-a-definition-for-a-property-from-a-project-reference

반응형