为什么Microsoft.CSharp.RuntimeBinder.RuntimeBinderException如果被调用的方法是存在的? [英] Why a Microsoft.CSharp.RuntimeBinder.RuntimeBinderException if the invoked method is there?

查看:1600
本文介绍了为什么Microsoft.CSharp.RuntimeBinder.RuntimeBinderException如果被调用的方法是存在的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的代码创建将分配给smtpClient变量动态对象。

I have the following code which creates a dynamic object that is assigned to the smtpClient variable.

public class TranferManager
{
    public void Tranfer(Account from, Account to, Money amount)
    {
        // Perform the required actions
        var smtpClient = New.SmtpClient();
        smtpClient.Send("info@bank.com", "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(下)

To test it I am using the UT (below):

    [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
    }

当我使用inmediate窗口,询问对于smtpClient型我得到:

When I, using the inmediate windows, ask for the smtpClient type I get:

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

当我问它的成员,我得到:

And when I ask for its members I get:

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}

所以,我的问题是:为什么会出现这个异常?

So, my question is: Why am I getting that exception?

推荐答案

匿名类型是内部的,如果你跨组装边界动态 CAN T解决该房产。

Anonymous types are internal, if you cross assembly boundaries dynamic can't resolve the property.

而不是使用匿名类型,请尝试使用一个实际的类型或在Expando对象。

Rather than using an anonymous type, try using an actual type or an Expando Object.

这篇关于为什么Microsoft.CSharp.RuntimeBinder.RuntimeBinderException如果被调用的方法是存在的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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