用于参数化属性的C#后期绑定 [英] C# Late Binding for Parameterized Property

查看:98
本文介绍了用于参数化属性的C#后期绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用C#WinForms项目使用后期绑定来连接到名为Amibroker的程序提供的COM自动化API.到目前为止,我已经可以连接到API中的所有内容,但只有一项除外,我认为这是基于广泛的Google搜索的参数化属性".

I'm trying to use late binding to connect to a COM automation API provided by a program called Amibroker, using a C# WinForms project. So far I've been able to connect to everything in the API except one item, which I believe to be a "parameterized property" based on extensive Googling.

这是根据文档显示的API规范(此处为完整版本: http://www.amibroker.com/guide/objects.html ):

Here's what the API specification looks like according to the docs (Full version here: http://www.amibroker.com/guide/objects.html):

Property Filter(ByVal nType  As Integer, ByVal pszCategory As String) As Long [r/w]

用于更新值的javascript代码段如下所示:

A javascript snippet to update the value looks like this:

AB = new ActiveXObject("Broker.Application");
AA = AB.Analysis;
AA.Filter( 0, "market" ) = 0;

使用以下C#后期绑定代码,我可以获取属性的值,但是我一生都无法弄清楚如何设置该值:

Using the following C# late-binding code, I can get the value of the property, but I can't for the life of me figure out how to set the value:

object[] parameter = new object[2];
parameter[0] = Number;
parameter[1] = Type;
object filters = _analysis.GetType().InvokeMember("Filter", BindingFlags.GetProperty, null, _analysis, parameter);

到目前为止,我已经尝试过:

So far I have tried:

  • 使用BindingFlags.SetProperty,BindingFlags.SetField
  • 将返回的对象投射到PropertyInfo对象,并尝试使用它更新值
  • 将包含该值的额外对象添加到参数对象
  • 其他事情作为最后的努力

据我所知,这应该很简单,但是我发现C#中的后期绑定充其量是繁琐的.该属性对我来说就像是一个方法调用,这就是让我失望的原因.如何为一个方法分配一个值,以及后期绑定C#代码的原型是什么样的?

From what I can see, this should be straight-forward, but I'm finding the late binding in C# to be cumbersome at best. The property looks like a method call to me, which is what is throwing me off. How does one assign a value to a method, and what would the prototype for late-binding C# code look like for it?

希望这可以解释得足够好,但是请随时询问我是否还有任何不清楚的地方.预先感谢您的帮助!

Hopefully that explains it well enough, but feel free to ask if I've left anything unclear. Thanks in advance for any help!

丹尼尔

推荐答案

是的,COM中的属性是作为幕后方法实现的.方法名称应为"set_Filter".我认为您遇到问题的真正原因是因为您要遵循VB6声明. VB6 Long类型实际上不是C#中的long类型,而是一个int类型.尝试为属性分配长值会失败.

Yes, properties in COM are implemented as methods under the hood. The method name ought to be "set_Filter". I reckon the real reason you are having a problem is because you are going by the VB6 declarations. The VB6 Long type isn't actually a long in C#, it is an int. Trying to assign the property with a long value will fail.

在VB.NET中编写此代码可以使其更容易使用很多,它使用类似于Java的自然"语法完全支持后期绑定方法和属性访问.并支持索引属性,这与C#不同.编写一个小的包装程序集,您可以在C#项目中引用该程序集.自VS2010起,使用新的 dynamic 关键字,它也可以在C#中使用.

Writing this code in VB.NET could make it a lot easier, it fully supports late-bound method and property access, using the "natural" syntax similar to Java. And supports indexed properties, unlike C#. Write a little wrapper assembly that you can reference in your C# project. It is available in C# too since VS2010 with the new dynamic keyword.

这篇关于用于参数化属性的C#后期绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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