- 浏览: 390397 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (309)
- xaml C# wpf (0)
- scala java inner clas (1)
- Tools UML Eclipse UML2 (1)
- Timer .NET Framework (1)
- perl (6)
- python function paramter (1)
- Python Docstring (1)
- Python how to compare types (1)
- Python (8)
- java (5)
- C# (76)
- C# WPF (0)
- p4 (0)
- WPF (46)
- .net (6)
- xaml (1)
- javascript (40)
- windows (10)
- scala (4)
- winform (1)
- c++ (48)
- tools (12)
- cmd (1)
- os (0)
- CI (0)
- shell (0)
- C (2)
- haskell (49)
- functional (1)
- tool (1)
- gnu (1)
- linux (1)
- kaskell (0)
- svn (0)
- wcf (3)
- android (1)
最新评论
The namespace of System.Reflection.Emit is the rennovated namespace for reflection based APIs. In this topic, we are going to discuss the how to use the ModuleBuilder from this particular namespace.
the original MSDN documentation on the ModuleBuilder is avaiable here in this page: Module Builder Class
public class CodeGenerator { AssemblyBuilder myAssemblyBuilder; public CodeGenerator() { // Get the current application domain for the current thread AppDomain myCurrentDomain = AppDomain.CurrentDomain; AssemblyName myAssemblyName = new AssemblyName(); myAssemblyName.Name = "TempAssembly"; // looks like we are creating the assemblies on the fly // Define a dynamic assembly in the current application domain. myAssemblyBuilder = myCurrentDomain.DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.Run); // indicate what kind of acces the generated assemblies is supposed to used! // now we have the Assembly, we can then create the module in this assembly ModuleBuilder myModuleBuilder = myAssemblyBuilder.DefineDynamicModule("TempModule"); // Define a runtime class with specified name and attributes TypeBuilder myTypeBuilder = myModuleBuilder.DefineType("TempClass", TypeAttributes.Public); // Add a Field, which is called "Greeting" to the class, with the specified attribute and type. FieldBuilder greetingField = myTypeBuilder.DefineField("Greeting", typeof(string), FieldAttributes.Public); Type[] myMethodArgs = { typeof(string) }; // type of the Method Args is defined as such // Add 'MyMethod' method to the class, with teh specified attributes and signature MethodBuilder myMethod = myTypeBuilder.DefineMethod("MyMethod", MethodAttributes.Public, CallingConventions.Standard, null, myMethodArgs); ILGenerator methodIL = myMethod.GetILGenerator(); methodIL.EmitWriteLine("In the method..."); // the following code has the same effect as // anonymous_fun(string arg) { // GreetingField = arg; // return; // } methodIL.Emit(OpCodes.Ldarg_0); // load this pointer methodIL.Emit(OpCodes.Ldarg_1); // load the first argument, which "value" methodIL.Emit(OpCodes.Stfld, greetingField); // store the value of "value" to the GreetingField methodIL.Emit(OpCodes.Ret); // return from the call myTypeBuilder.CreateType(); // Generate the code } public AssemblyBuilder MyAssembly { get { return this.myAssemblyBuilder; } } } public class TestClass { [PermissionSetAttribute(SecurityAction.Demand, Name = "FullTrust")] public static void Main() { CodeGenerator myCodeGenerator = new CodeGenerator(); // Get the Assembly builder for 'myCodeGenerator' object. AssemblyBuilder myAssemblyBuilder = myCodeGenerator.MyAssembly; // Get the module builder for the above assembly builder object. ModuleBuilder myModuleBuidler = myAssemblyBuilder.GetDynamicModule("TempModule"); // Display the fully qualified name of the moduel Console.WriteLine("The fully qualified name and path to this " + "module is :" + myModuleBuidler.FullyQualifiedName); // Type myType = myModuleBuidler.GetType("TempClass"); MethodInfo myMethodInfo = myType.GetMethod("MyMethod"); // Get the token used to identify the method iwthin this module // SO be careful that the method does not return the MethodInfo, but rather return // the method tokens MethodToken myMethodToken = myModuleBuidler.GetMethodToken(myMethodInfo); Console.WriteLine("Token used to identify the method of 'myType'" + "within the module is {0:x}", myMethodToken.Token); object[] args = { "Hello." }; object myObject = Activator.CreateInstance(myType, null, null); myMethodInfo.Invoke(myObject, args); } }
As you can see:
- there are different types of builder, e.g. ModuleBuilder, TypeBuilder, FieldBuidler, MethodBuilder which build Module, Type, Field or Method;
- The various xxxBuilder class has the DefineXXX method to get a builder of subordinate constructs, e.g. ModuleBuilder.DefienType return TypeBuilder.
- Each Builder is a subclass to XXXInfo, e.g. FiledBuilder is subclass to FieldInfo.
- To generate the IL code, you may use the ILGenerator, and there is a bunch of Emit calls to generate the body of the method.
- From each builder, you can query some adjunct construct, e.g. you can get ModuleBuilder from AssemblyBuilder...
发表评论
-
wpf - example to enhance ComboBox for AutoComplete
2014-09-19 15:56 1942first let’s see an example ... -
Investigate and troubleshoot possible memory leak issue of .NET application
2014-07-31 10:42 0Hi All, I would like to sh ... -
C# – CoerceValueCallback合并、替换元数据值
2013-08-05 21:59 1895Topic: C# – CoerceValueCallbac ... -
wpf – ListView交替背景色
2013-07-02 20:56 6497Wpf – Alternate background col ... -
C# - 简单介绍TaskScheduler
2013-06-29 17:18 11947标题: C# - 简单介绍TaskSchedulerTit ... -
c# - Get enum from enum attribute
2013-06-27 21:32 1203DescriptionAttribute gives the ... -
C# - PInvoke, gotchas on the RegisterClassEx and the CreateWindowEx
2013-06-24 13:49 2538I get an exception message li ... -
c# - Use PInvoke to create simple win32 Application
2013-06-24 11:59 10891In this post, .net platform h ... -
c# - Linq's Select method as the Map function
2013-06-19 18:47 1250If you comes from a functiona ... -
c# - Tips of Linq expression Any to determine if a collection is Empty
2013-06-19 18:29 896When you are @ the linq expres ... -
myth buster - typeof accepting array of types not acceptable
2013-06-19 17:17 785I have seen from some book whe ... -
windows - trying to create WIN32 application with PInvoke
2013-06-19 14:34 0While it is stupid to do such ... -
WPF - Setting foreground color of Entire window
2013-06-13 16:00 1883You might as well as I would s ... -
WPF - Enhanced TabControl - TabControlEx aka Prerendering TabControl
2013-06-13 13:12 5294As an opening word, let's che ... -
wpf - ControlTemplate and AddLogicChild/RemoveLogicalChild
2013-06-10 15:42 1149Recently I was trying to debug ... -
c# - P/Invoke, DllImport, Marshal Structures and Type conversions
2013-06-05 15:25 1663P/Invoke as in the following q ... -
c# - A study on the NativeWindow - encapsulate window handle and procedure
2013-06-05 14:40 6046NativeWindow gives you a way t ... -
WCF - Notify server when client connects
2013-06-03 18:19 1186It is sometimes very importan ... -
wcf - Debug to enable Server exception in Fault message
2013-06-03 15:47 1060WCF will be able to send back ... -
c# - determine if a type/object is serialzable
2013-05-30 16:35 840In WCF, primitives type are s ...
相关推荐
Managed.Reflection, System.Reflection [.Emit ]的托管替换 Managed.ReflectionManaged.Reflection 是对 System.Reflection 和 System.Reflection.Emit的完全管理的替换。 System.Reflection 不同,它不绑定到
eventEmitter.emit('data_received'); } // 绑定 connection 事件处理程序 eventEmitter.on('connection', connectHandler); // 触发 connection 事件 eventEmitter.emit('connection'); console.log("程序...
--timestamps <format> emit a timestamp at the start of each output line (using optional format string as per strftime(3)) -d, --debug emit debugging output -v, --version show version information ...
The Watchers in AngularJS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 The $watchCollection() Function . . . . . . . . . . . . . . . . . . . . . . . 56 The $apply() Function ...
using namespace QtCharts; private: //曲线 QSplineSeries* line; //绘图变量和坐标 QChart* chart; //发来数据的接收槽函数 private slots: void receive_list(QVector<QPointF> list); 在子.cpp中 line =...
Real time dynamic fluid systems integrate seamlessly with the rest of your scene, with loads of customisation options packed into an easy to use package, you can create realistic looking fluid in a ...
AS-23-Pardomuan-Emit-My-Keystroke
使用MSIL采用Emit方式实现C#的代码生成与注入.rar
左侧为产品自制件树形结构,采用递归组件实现,组件会根据层级的大小改变字体大小和颜色,背景图也会相应改变,竖线也单独做成递归组件,并计算其位置,递归组件采用vue的$on和$emit进行全局传参,避免了递归组件...
Map函数调用emit(key,value)遍历集合中所有的记录,将key与value传给Reduce函数进行处理。 Map函数和Reduce函数是使用Javascript编写的,并可以通过db.runCommand或mapreduce命令来执行MapReduce操作。 GridFS是...
emit@resource://devtools/shared/event-emitter.js:178:15 emit@resource://devtools/shared/event-emitter.js:255:5 setNodeFront@resource://devtools/client/framework/selection.js:153:5 onDetached@resource:...
主要介绍了node.js中的emitter.emit方法使用说明,本文介绍了emitter.emit的方法说明、语法、接收参数、使用实例和实现源码,需要的朋友可以参考下
节点聊天应用 基于socket.io的服务器-客户端聊天应用程序。 跑步 克隆存储库。... emit ( event , message ) ; // Event Emitter function + Scoket . broadcast . emit ( event , message ) ; // Fun
It works in a pair-set, converting 100BASE-T1 (a.k.a BroadR-Reach (TM from Broadcom)) to optical fibre and back to 100BASE-T1 and is specially designed to emit below the industry standard ...
<PackageReference Include="System.Reflection.Emit" Version="4.7.0" /> <PackageReference Include="System.Reflection.TypeExtensions" Version="4.5.0" /> <PackageReference Include="System.Xml.XPath" ...
Syntax: x265 [options] infile [-o] outfile infile can be YUV or Y4M outfile is raw HEVC bitstream Executable Options: -h/--help Show this help text and exit -V/--version Show version info and exit ...
vue-events一个Vue.js插件,可简化事件。 适用于Vue 1.0和Vue 2.0。 安装1.)通过Yarn或NPM $ yarn安装软件包add vue-ev vue-events一个Vue.js插件,可以简化事件。 适用于Vue 1.0和Vue ...虚拟机$ emit虚拟机$ event
vlc-control-io 使用 Node.js 控制您的 VLC。 带有 socket.io + vlc-control-node 模块的 JS 设置 首先,您必须从客户端发出一个... uri : '/path/to/the/file' , noaudio : false , novideo : false } } )
The following are all the settings which are different in either the production ; or development versions of the INIs with respect to PHP's default behavior. ; Please see the actual settings later in...
面试官-Vue.js 中的 $emit 和 $on 方法有什么区别?.pdf