方法参数中的动态类型 [英] Dynamic type in method parameter

查看:25
本文介绍了方法参数中的动态类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将动态类型传递到方法中,但在运行代码时遇到了一些问题.想知道您是否能够使用 out 关键字将动态对象作为参数传入.

I am passing in a dynamic type into a method and having some issues running the code. Wondering if you are able to pass a dynamic object into as a parameter using the out keyword.

下面是代码.

dynamic btApp = AutomationFactory.CreateObject("Test.Application");
dynamic btMessages;

dynamic btFormat = btApp.Formats.Open("c:\\Temp/Format1.btw", false, "");
btFormat.SetNamedSubStringValue("testing", "testtest");
btFormat.Print("Job1", true, -1, out btMessages);
btFormat.Close(2);

问题在于打印方法.其中最后一个参数传入一个动态对象.

issue is in the print method. where the last argument is passing in a dynamic object.

推荐答案

这取决于 Print 方法的实际类型签名是什么.dynamic 类型在运行时表示为 object,所以如果 Print 方法采用 out 类型的参数code>object(或 dynamic),那么它应该可以工作.

It depends on what the actual type signature of the Print method is. The dynamic type is represented as object at runtime, so if the Print method takes an out parameter of type object (or dynamic), then it should work.

如果 Print 方法具有某种其他类型的实际 out 参数,则调用方使用的实际运行时类型与实际类型不匹配声明,所以它不会工作.

If the Print method has actual out parameter of some other type, then the actual runtime type used at the side of the caller doesn't match the actual type of the declaration, so it will not work.

这篇关于方法参数中的动态类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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