简体   繁体   English

为什么Microsoft.CSharp.RuntimeBinder.RuntimeBinderException如果有调用的方法?

[英]Why a Microsoft.CSharp.RuntimeBinder.RuntimeBinderException if the invoked method is there?

I have the following code which creates a dynamic object that is assigned to the smtpClient variable. 我有以下代码,它创建一个分配给smtpClient变量的动态对象。

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

To test it I am using the UT (below): 为了测试它我使用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
    }

When I, using the inmediate windows, ask for the smtpClient type I get: 当我使用中间窗口时,请求我得到的smtpClient类型:

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? 所以,我的问题是:为什么我得到那个例外?

Anonymous types are internal, if you cross assembly boundaries dynamic can't resolve the property. 匿名类型是内部的,如果跨越程序集边界,则dynamic无法解析该属性。

Rather than using an anonymous type, try using an actual type or an Expando Object. 不要使用匿名类型,请尝试使用实际类型或Expando对象。

In the AssemblyInfo.cs try to add following: 在AssemblyInfo.cs中尝试添加以下内容:

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

where NamsSpace1 is your project name and SubNameSpace is namespace of your dynamic/anonym object 其中NamsSpace1是您的项目名称,SubNameSpace是您的动态/匿名对象的名称空间

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Microsoft.CSharp.RuntimeBinder.RuntimeBinderException错误 - Microsoft.CSharp.RuntimeBinder.RuntimeBinderException Error 发生``Microsoft.CSharp.RuntimeBinder.RuntimeBinderException&#39;&#39; - 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException' occurred 在私有类型上动态抛出Microsoft.CSharp.RuntimeBinder.RuntimeBinderException - Dynamic throwing Microsoft.CSharp.RuntimeBinder.RuntimeBinderException on private types 发生了“Microsoft.CSharp.RuntimeBinder.RuntimeBinderException”类型的第一次机会异常 - A first chance exception of type 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException' occurred #SpecFlow# For CreateDynamicInstance() - 错误 - Microsoft.CSharp.RuntimeBinder.RuntimeBinderException:最佳重载方法 - #SpecFlow# For CreateDynamicInstance() - error - Microsoft.CSharp.RuntimeBinder.RuntimeBinderException : The best overloaded method ASP.Net CORE 3.1 - 抛出错误异常:Microsoft.CSharp.dll 中的“Microsoft.CSharp.RuntimeBinder.RuntimeBinderException” - ASP .Net CORE 3.1 - Error Exception thrown: 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException' in Microsoft.CSharp.dll LINQ加入Microsoft.CSharp.RuntimeBinder.RuntimeBinderException:&#39;对象&#39;不包含以下内容的定义: - LINQ Join Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'object' does not contain a definition for 当我尝试在View中访问Model时出现Microsoft.CSharp.RuntimeBinder.RuntimeBinderException - Microsoft.CSharp.RuntimeBinder.RuntimeBinderException when I try to access Model in View 访问动态属性会引发“Microsoft.CSharp.RuntimeBinder.RuntimeBinderException”类型的异常 - Accessing a dynamic's property threw an exception of type 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException' Microsoft.CSharp.RuntimeBinder.RuntimeBinderException:“System.__ComObject”不包含“语言”的定义 - Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: ''System.__ComObject' does not contain a definition for 'Language''
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM