将动态属性传递给内部构造函数
在 C# 4.0b 中使用动态参数调用内部构造函数会导致以下异常
System.ArgumentNullException:值 不能为空。 参数名称: 构造函数
示例代码(感谢 Jon Skeet)
public class Test
{
internal Test(string x)
{
}
static void Main()
{
dynamic d = "";
new Test(d);
}
}
运行时似乎在尝试选择正确的构造函数时并不考虑内部构造函数。 这似乎是一个错误,所以我将其发布在 Connect 上: http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback。 aspx?FeedbackID=472924
看来他们在新版本中修复了这个问题。
Calling an internal constructor with a dynamic argument in C# 4.0b results in the following exception
System.ArgumentNullException: Value
cannot be null. Parameter name:
constructor
Example code (thanks to Jon Skeet)
public class Test
{
internal Test(string x)
{
}
static void Main()
{
dynamic d = "";
new Test(d);
}
}
It seems the runtime does not consider internal constructors when it's trying to pick the right one. This seems to be a bug, so I posted it on Connect:
http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=472924
It seems they fixed it for the new version.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编辑:好的,我现在已经进一步追踪它 - 它使用了导致问题的内部构造函数。
这是一个非常简短但完整的示例,它演示了该问题:
我建议您使用 Connect - 然后在此处发布 URL,我们可以对其进行投票:)
(我的猜测是,在 DLR 内部有一个对 GetConstructor 的调用,但没有适当的 BindingFlags.NonPublic,但这只是一个猜测...)
EDIT: Okay, I've now tracked it down a lot further - it's using an internal constructor that causes a problem.
Here's a really short but complete example which demonstrates the problem:
I suggest you log this with Connect - then post the URL here and we can vote on it :)
(My guess is that inside the DLR there's a call to GetConstructor without the appropriate BindingFlags.NonPublic, but that's just a guess...)
如果没有看到代码,我建议您将非实例化的类传递给构造函数。 在将它们传递给非动态对象之前,请确保它们在范围内并且已通过使用 new 等进行实例化。
编辑
在看到您的代码时,我建议您对辅助构造函数和实体属性使用DynamicObject而不是dynamic。
看到乔恩的回答后进行编辑
我认为问题在于使用 GetEntity() 方法生成动态对象实例。
我注意到 Jon 在他使用 MyDynamicObject 的同一范围内创建了一个实例。
我假设您正在 GetEntity() 方法中生成对象的实例,在这种情况下,当您使用它时,它不再在范围内,而是被归类为本地对象。
使用“MyDynamicObject e =实体;” 将强制编译器隐式使用 MyDynamicObject 构造函数并将结果映射到它。 因此,地址空间已经分配,并且在将其传递给 Helper 构造函数时要使用的范围内。
Without seeing the code I would suggest that you are passing a non-instantiated class to your constructor. Ensure that they are within scope and have been instantiated e.g. by use of new, before they are passed to your non-dynamic object.
Edit
On seeing your code I would suggest that you use DynamicObject rather than dynamic for your helper costructor and Entity property.
Edit after seeing Jon's answer
I think that the problem is in using the GetEntity() method to generate the dynamic object instance.
I note that Jon creates an instance of MyDynamicObject within the same scope as he uses it.
I assume that you're generating an instance of your object within the GetEntity() method, in which case it is no longer inscope when you come to use it, being classed as a local object.
Using "MyDynamicObject e = entity;" will force the compiler to imlicitly use the MyDynamicObject constructor and map your result to it. Hence address space is already allocated and in scope to be used when passing it to the Helper constructor.