- 浏览: 399594 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (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 1976first 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 1925Topic: C# – CoerceValueCallbac ... -
wpf – ListView交替背景色
2013-07-02 20:56 6551Wpf – Alternate background col ... -
C# - 简单介绍TaskScheduler
2013-06-29 17:18 12038标题: C# - 简单介绍TaskSchedulerTit ... -
c# - Get enum from enum attribute
2013-06-27 21:32 1244DescriptionAttribute gives the ... -
C# - PInvoke, gotchas on the RegisterClassEx and the CreateWindowEx
2013-06-24 13:49 2571I get an exception message li ... -
c# - Use PInvoke to create simple win32 Application
2013-06-24 11:59 10946In this post, .net platform h ... -
c# - Linq's Select method as the Map function
2013-06-19 18:47 1287If you comes from a functiona ... -
c# - Tips of Linq expression Any to determine if a collection is Empty
2013-06-19 18:29 938When you are @ the linq expres ... -
myth buster - typeof accepting array of types not acceptable
2013-06-19 17:17 813I 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 1918You might as well as I would s ... -
WPF - Enhanced TabControl - TabControlEx aka Prerendering TabControl
2013-06-13 13:12 5330As an opening word, let's che ... -
wpf - ControlTemplate and AddLogicChild/RemoveLogicalChild
2013-06-10 15:42 1185Recently I was trying to debug ... -
c# - P/Invoke, DllImport, Marshal Structures and Type conversions
2013-06-05 15:25 1712P/Invoke as in the following q ... -
c# - A study on the NativeWindow - encapsulate window handle and procedure
2013-06-05 14:40 6091NativeWindow gives you a way t ... -
WCF - Notify server when client connects
2013-06-03 18:19 1221It is sometimes very importan ... -
wcf - Debug to enable Server exception in Fault message
2013-06-03 15:47 1091WCF will be able to send back ... -
c# - determine if a type/object is serialzable
2013-05-30 16:35 867In WCF, primitives type are s ...
相关推荐
eventEmitter.emit('data_received'); } // 绑定 connection 事件处理程序 eventEmitter.on('connection', connectHandler); // 触发 connection 事件 eventEmitter.emit('connection'); console.log("程序...
Managed.Reflection, System.Reflection [.Emit ]的托管替换 Managed.ReflectionManaged.Reflection 是对 System.Reflection 和 System.Reflection.Emit的完全管理的替换。 System.Reflection 不同,它不绑定到
--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 ...
4. 记录日志:调用logger的`emit`方法,传递日志数据,例如`logger.emit('logtag', {'key': 'value'})`。 fluent_logger库的一大特点是支持结构化的日志数据,这使得日志数据更加易读和分析。此外,它还提供了异步...
包括组件定义、props传递、事件通信($emit和$v-on)、自定义指令、插槽等内容。 3. **状态管理**:Vue 2.0引入了Vuex作为官方推荐的状态管理工具,用于集中管理组件间的共享状态。学习Vuex的基本原理、状态、动作、...
- 发送数据:`socket.emit('event-name', data);` - 监听数据:`socket.on('event-name', (res) => { console.log(res); });` 6. **错误处理**:库通常会提供错误处理机制,如连接失败、断线重连等,开发者需要...
4. **Namespace**:命名空间是SocketIO的一个特性,允许在一个SocketIO服务器上创建多个独立的通信通道。每个命名空间可以有不同的事件和逻辑,这样可以在同一个Socket连接上处理不同的数据类型或应用场景。 5. **...
6. **API接口**:Swift Socket.IO客户端提供了丰富的API,如`emit()`用于发送事件,`on()`用于监听事件,以及`disconnect()`用于断开连接等,方便开发者集成到自己的应用中。 7. **错误处理**:客户端还提供了错误...
socketIO.emit('my_request', {'message': 'Hello Server!'}) ``` 在分布式和云原生环境(Cloud Native)中,socketIO-client因其实时性和可扩展性得到了广泛应用。例如,在Zookeeper这样的分布式协调服务中,...
当用户输入消息并发送时,触发`emit`方法将消息发送到服务器。 - **展示聊天记录**:服务器广播的消息会在客户端接收到,更新聊天界面显示最新的聊天记录。 5. **HiChat-master项目分析** "HiChat-master--计网...
MSIL使得开发者能够在运行时动态生成和执行代码,这是通过System.Reflection.Emit命名空间中的类来实现的。本教程将深入探讨如何使用MSIL和Emit API来生成和注入C#代码。 首先,让我们了解Emit API的基本概念。Emit...
相反,C#的`System.Reflection.Emit`命名空间提供了动态生成IL代码的能力,这使得在运行时创建类型和方法成为可能。通过这种方式,我们可以动态地构建一个代理类,该类在内部调用本地库的函数,从而避免了P/Invoke的...
The Watchers in AngularJS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 The $watchCollection() Function . . . . . . . . . . . . . . . . . . . . . . . 56 The $apply() Function ...
- Vue2.x中,组件间通信可以通过props、事件($emit/$on)、Vuex状态管理或非父子组件间的Bus(事件总线)。 - Vue3.x增加了提供通信的新方法,如提供和注入(provide/inject)以及使用Context API。 面试中,...
Emit学习之旅是一个深入探索C# Emit技术的教程,它为开发者提供了动态生成IL(Intermediate Language)代码的能力。Emit是.NET Framework的一部分,允许程序员在运行时构建和执行方法,这是实现元编程的重要工具。...
- 解决方案一:One possible solution is ①-----------------------. Although it has the advantage of ②-----------------, there might be a potential drawback ③-----------------. - 解决方案二:Another ...
using namespace QtCharts; private: //曲线 QSplineSeries* line; //绘图变量和坐标 QChart* chart; //发来数据的接收槽函数 private slots: void receive_list(QVector<QPointF> list); 在子.cpp中 line =...
io.emit('message', data); }); }); ``` 3. **客户端集成**:在 HTML 页面中,引入 `socket.io-client` 库,并创建一个 `Socket` 实例,连接到服务器。然后,可以监听服务器发送的事件,以及发送自定义事件。 ...
然后,他们可以调用对象的方法来记录不同级别的日志事件,例如`emit()`方法,同时传递事件的标签和数据。 在分布式和云原生环境中,这种日志记录方案的优势在于它能够集中管理大量节点的日志,方便监控、搜索和分析...
console.log(`Message "${msg}" stored in DB.`); } catch (error) { console.error(error); } io.emit('chat message', msg); }); ``` 这里我们创建了一个名为`messages`的表,用于存储聊天记录。当收到新...