Подтвердить что ты не робот

Почему исключение Microsoft.CSharp.RuntimeBinder.RuntimeBinderException, если вызываемый метод существует?

У меня есть следующий код, который создает динамический объект, назначенный переменной smtpClient.

public class TranferManager
{
    public void Tranfer(Account from, Account to, Money amount)
    {
        // Perform the required actions
        var smtpClient = New.SmtpClient();
        smtpClient.Send("[email protected]", "from.Email", "Tranfer", "?");
        // In the previous line I get a Microsoft.CSharp.RuntimeBinder.RuntimeBinderException
        // with the description = "'object' does not contain a definition for 'Send'"
    }
}

public static class New
{
    public static dynamic SmtpClient(params object[] parameters)
    {
        return typeof(SmtpClient).New(parameters);
    }
}

public static class CreationExtensions
{
    private static Dictionary<Type, Func<object, dynamic>> builders =
        new Dictionary<Type, Func<object, dynamic>>();

    public static dynamic New(this Type type, params object[] parameters)
    {
        if(builders.ContainsKey(type))
            return builders[type](parameters);

        return Activator.CreateInstance(type, parameters);
    }

    public static void RegisterBuilder(this Type type, Func<object, dynamic> builder)
    {
        builders.Add(type, builder);
    }
}

Чтобы проверить это, я использую UT (ниже):

    [TestMethod()]
    public void TranferTest()
    {
        typeof(SmtpClient).RegisterBuilder(p => 
            new
            {
                Send = new Action<string, string, string, string>(
                (from, to, subject, body) => { })
            }
        );

        var tm = new TranferManager();
        tm.Tranfer(new Account(), new Account(), new Money());
        // Assert
    }

Когда я, используя промежуточные окна, задаю тип smtpClient, я получаю:

smtpClient.GetType()
{<>f__AnonymousType0`1[System.Action`4[System.String,System.String,System.String,System.String]]}

И когда я прошу его членов, я получаю:

smtpClient.GetType().GetMembers()
{System.Reflection.MemberInfo[7]}
    [0]: {System.Action`4[System.String,System.String,System.String,System.String] get_Send()}
    [1]: {System.String ToString()}
    [2]: {Boolean Equals(System.Object)}
    [3]: {Int32 GetHashCode()}
    [4]: {System.Type GetType()}
    [5]: {Void .ctor(System.Action`4[System.String,System.String,System.String,System.String])}
    [6]: {System.Action`4[System.String,System.String,System.String,System.String] Send}

Итак, мой вопрос: почему я получаю это исключение?

4b9b3361

Ответ 1

Анонимные типы являются внутренними, если вы пересекаете границы сборок dynamic не можете разрешить свойство.

Вместо использования анонимного типа попробуйте использовать фактический тип или объект Expando.

Ответ 2

В AssemblyInfo.cs попробуйте добавить следующее:

[assembly: InternalsVisibleTo("NameSpace1.SubNameSpace1")]

где NamsSpace1 - ваше имя проекта, а SubNameSpace - пространство имен вашего объекта dynamic/anonym