如果调用的方法存在,为什么会出现 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}
所以,我的问题是: 为什么我会得到该异常?
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("[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);
}
}
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
}
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
匿名类型是内部的,如果跨程序集边界
dynamic
无法解析属性。不要使用匿名类型,而是尝试使用实际类型或 Expando 对象。
Anonymous types are internal, if you cross assembly boundaries
dynamic
can't resolve the property.Rather than using an anonymous type, try using an actual type or an Expando Object.
在 AssemblyInfo.cs 中尝试添加以下内容:
其中 NamsSpace1 是您的项目名称,SubNameSpace 是动态/匿名对象的命名空间
In the AssemblyInfo.cs try to add following:
where NamsSpace1 is your project name and SubNameSpace is namespace of your dynamic/anonym object