本文介绍了在C#4中的方法的静态参数中传递动态变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我想要做的:

public void method(int myVal, string myOtherVal)
{
  // doing something
}

dynamic myVar = new SomeDynamicObjectImplementer();
method(myVar.IntProperty, myVar.StringProperty);

注意,我的属性也是DynamicObjects。我的问题是TryConvert方法从来没有调用,我得到一个运行时错误说方法签名是无效的。

Note that my properties are also DynamicObjects. My problem is that the TryConvert method is never called and that I get a runtime error saying the method signature is invalid.

以下是非常好的:

string strVar = myVar.StringProperty;
int intVar = myVar.IntProperty;

我想避免

method((int)myVar.IntProperty, (string)myVar.StringProperty);

是否可以覆盖DynamicObject中的某些内容以允许这样做? (或其他)

Is it possible to override something in DynamicObject to allow this? (or something else)

谢谢

推荐答案

你的假设,它将尝试动态隐式转换动态调用的参数,使方法调用工作,这不是真的。

The problem is your assumption that it will try a dynamic implicit convert on arguments of an dynamic invocation to make a method call work, this is not true.

当您的参数不是静态类型时,它将使用运行时类型来查找最佳匹配方法(如果运行时类型与静态规则匹配,因为你的IntProperty,StringProperty似乎是返回一个DynamicObject而不是一个Int和一个String或者可以静态隐式转换的东西,这个查找会失败。

When your arguments aren't statically typed, it will use the runtime type to find the best matching method (if the runtime type matches the static rules for implicit conversion to the argument type this will work too), since your your IntProperty,StringProperty seem to be returning a DynamicObject rather than an Int and a String or something that could statically be converter implicitly, this lookup will fail.

如果SomeDynamicObjectImplementer实际上可以为IntProperty和StringProperty返回一个Int,你的方法调用没有强制转换实际上可以工作。如果你的数据类型是基于实际类型的数据,而不是使用try转换,这也可能是一个更好的动态类型练习。你可以为每个可能的类型添加实际的隐式转换方法,你可以返回到返回的DynamicObject类型,但是这可能会导致奇怪的解决方案问题,这取决于你是多少重载。

If SomeDynamicObjectImplementer could actually return an Int for IntProperty and a String for StringProperty your method call for without casting would actually work. It's also probably a better dynamic typing practice if you data type is based on the actually type of data rather than usage using try convert. You could add actually implicit convert methods for every possible type that you could return to that returned DynamicObject type, but that could cause strange resolution issues to depending on how much you are overloading.

但是,保持动态实现相同的另一个选项是混合一些受控的静态类型,可以使用ImpromputInterface(在nuget中)将,如果你这样做,那么TryConvert方法将在你返回的DynamicObjects上被调用。

However, another option to keep your dynamic implementation the same is to mix a little controlled static typing in, you can use ImpromputInterface (in nuget) to put an interface on top of a dynamic object, if you do that then the TryConvert method would be called on your returned DynamicObjects.

public interface ISomeStaticInterface{
      int IntProperty {get;}
      string StringProperty {get;}
}
...
var myVar = new SomeDynamicObjectImplementer().ActLike<ISomeStaticInterface>();
method(myVar.IntProperty, myVar.StringProperty);

这篇关于在C#4中的方法的静态参数中传递动态变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 23:00