vb.net反射:使用数组字段构建运行时类 [英] vb.net Reflection: build a runtime class with an array field

查看:113
本文介绍了vb.net反射:使用数组字段构建运行时类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

通过EtherNetIP与omron PLC交谈我写了一些不好的指令,它返回了标签列表和每个标签的模板(结构,数组或原子)。这是非常困难的,因为omron没有关于这个协议的文档而不是Allen Bradley。

现在我想将这些'结构'作为'类'复制到VB.Net中:我想知道反思ad在运行时创建一个类(然后是一个onbject实例)。



某些Plc的结构有数组字段,例如:

类StructRegMT {

按布尔值;

NrItem为整数;

SetPointRegMT(64)为整数;

}结束类



如何在运行时制作这个样本类?

感谢大家的帮助......



Matteo M.

Hi everyone,
speaking with an omron PLC via EtherNetIP i wrote some poor instruction that returned me the list of tags and the template of each tag (structure, array or atomic). It was very difficult cause omron has no documentation about this protocol instead Allen Bradley for example.
Now i would like to replicate these 'structure' as a 'class' into VB.Net: i thougth about reflection ad make a class (then an onbject instance) during runtime.

Some Plc's structure has array field, for instance:
Class StructRegMT {
On as boolean;
NrItem as integer;
SetPointRegMT(64) as integer;
} end Class

How make this sample class during runtime?
Thanks all for help..

Matteo M.

推荐答案

这不仅仅是 System.Reflection ,这将是 System.Reflection.Emit 。如果你真的需要构建一个类,而不仅仅是 DynamicMethod ,你需要动态创建整个程序集。你将不得不学习这一切:

https://msdn.microsoft.com/en-us/library/system.reflection.emit%28v=vs.110%29.aspx [ ^ ],

http://www.codemag.com/article/0301051 [ ^ ],

http://www.c-sharpcorner.com/uploadfile/puranindia/reflection-and-reflection-emit-in- C-Sharp [ ^ ],

http://www.drdobbs.com/generating-code-at-run-time-with-reflect/184416570 [ ^ ]。



这个CodeProject文章也很有用:使用Reflection.Emit的动态类型 [ ^ ]。



我必须说,不仅需要了解Emit本身,而且了解CIL的工作原理非常重要。此外,众所周知,发出的代码很难调试。总的来说,这可能是相当严肃的工作。



-SA
This is not just System.Reflection, this would be System.Reflection.Emit. And if you really need to construct a class, not just DynamicMethod, you will need to create the whole assembly on the fly. You will have to learn it all:
https://msdn.microsoft.com/en-us/library/system.reflection.emit%28v=vs.110%29.aspx[^],
http://www.codemag.com/article/0301051[^],
http://www.c-sharpcorner.com/uploadfile/puranindia/reflection-and-reflection-emit-in-C-Sharp[^],
http://www.drdobbs.com/generating-code-at-run-time-with-reflect/184416570[^].

This CodeProject article can also be useful: Dynamic Type Using Reflection.Emit[^].

I must say, not only it will take learning of Emit itself, but it's really important to understand how CIL works. Also, emitted code is notoriously difficult to debug. Overall, it can be quite serious work.

—SA


这篇关于vb.net反射:使用数组字段构建运行时类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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