当前位置: 首页>>代码示例>>C#>>正文


C# Class.EmitType方法代码示例

本文整理汇总了C#中Mono.CSharp.Class.EmitType方法的典型用法代码示例。如果您正苦于以下问题:C# Class.EmitType方法的具体用法?C# Class.EmitType怎么用?C# Class.EmitType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Mono.CSharp.Class的用法示例。


在下文中一共展示了Class.EmitType方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: CompileBlock

		static CompiledMethod CompileBlock (Class host, Undo undo, Report Report)
		{
			AssemblyDefinitionDynamic assembly;
			AssemblyBuilderAccess access;

			if (Environment.GetEnvironmentVariable ("SAVE") != null) {
				access = AssemblyBuilderAccess.RunAndSave;
				assembly = new AssemblyDefinitionDynamic (RootContext.ToplevelTypes, current_debug_name, current_debug_name);
				assembly.Importer = loader.Importer;
			} else {
#if NET_4_0
				access = AssemblyBuilderAccess.RunAndCollect;
#else
				access = AssemblyBuilderAccess.Run;
#endif
				assembly = new AssemblyDefinitionDynamic (RootContext.ToplevelTypes, current_debug_name);
			}

			assembly.Create (AppDomain.CurrentDomain, access);

			if (host != null) {
				host.CreateType ();
				host.Define ();
			}

			RootContext.ToplevelTypes.CreateType ();
			RootContext.ToplevelTypes.Define ();

			if (Report.Errors != 0){
				undo.ExecuteUndo ();
				return null;
			}

			TypeBuilder tb = null;
			MethodBuilder mb = null;
				
			if (host != null){
				tb = host.TypeBuilder;
				mb = null;
				foreach (MemberCore member in host.Methods){
					if (member.Name != "Host")
						continue;
					
					MethodOrOperator method = (MethodOrOperator) member;
					mb = method.MethodBuilder;
					break;
				}

				if (mb == null)
					throw new Exception ("Internal error: did not find the method builder for the generated method");

				host.EmitType ();
			}
			
			RootContext.ToplevelTypes.Emit ();
			if (Report.Errors != 0){
				undo.ExecuteUndo ();
				return null;
			}

			RootContext.ToplevelTypes.CloseType ();
			if (host != null)
				host.CloseType ();

			if (access == AssemblyBuilderAccess.RunAndSave)
				assembly.Save ();

			if (host == null)
				return null;
			
			//
			// Unlike Mono, .NET requires that the MethodInfo is fetched, it cant
			// work from MethodBuilders.   Retarded, I know.
			//
			var tt = assembly.Builder.GetType (tb.Name);
			MethodInfo mi = tt.GetMethod (mb.Name);
			
			// Pull the FieldInfos from the type, and keep track of them
			foreach (Field field in queued_fields){
				FieldInfo fi = tt.GetField (field.Name);

				Tuple<FieldSpec, FieldInfo> old;
				
				// If a previous value was set, nullify it, so that we do
				// not leak memory
				if (fields.TryGetValue (field.Name, out old)) {
					if (old.Item1.MemberType.IsStruct) {
						//
						// TODO: Clear fields for structs
						//
					} else {
						try {
							old.Item2.SetValue (null, null);
						} catch {
						}
					}

					fields [field.Name] = Tuple.Create (field.Spec, fi);
				} else {
					fields.Add (field.Name, Tuple.Create (field.Spec, fi));
//.........这里部分代码省略.........
开发者ID:keith512,项目名称:mono,代码行数:101,代码来源:eval.cs

示例2: CompileBlock

		CompiledMethod CompileBlock (Class host, Undo undo, Report Report)
		{
#if STATIC
			throw new NotSupportedException ();
#else
			string current_debug_name = "eval-" + count + ".dll";
			++count;

			AssemblyDefinitionDynamic assembly;
			AssemblyBuilderAccess access;

			if (Environment.GetEnvironmentVariable ("SAVE") != null) {
				access = AssemblyBuilderAccess.RunAndSave;
				assembly = new AssemblyDefinitionDynamic (module, current_debug_name, current_debug_name);
				assembly.Importer = importer;
			} else {
#if NET_4_0
				access = AssemblyBuilderAccess.RunAndCollect;
#else
				access = AssemblyBuilderAccess.Run;
#endif
				assembly = new AssemblyDefinitionDynamic (module, current_debug_name);
			}

			assembly.Create (AppDomain.CurrentDomain, access);

			Method expression_method;
			if (host != null) {
				var base_class_imported = importer.ImportType (base_class);
				var baseclass_list = new List<FullNamedExpression> (1) {
					new TypeExpression (base_class_imported, host.Location)
				};

				host.AddBasesForPart (host, baseclass_list);

				host.CreateType ();
				host.DefineType ();
				host.Define ();

				expression_method = (Method) host.Methods[0];
			} else {
				expression_method = null;
			}

			module.CreateType ();
			module.Define ();

			if (Report.Errors != 0){
				if (undo != null)
					undo.ExecuteUndo ();

				return null;
			}

			if (host != null){
				host.EmitType ();
			}
			
			module.Emit ();
			if (Report.Errors != 0){
				if (undo != null)
					undo.ExecuteUndo ();
				return null;
			}

			module.CloseType ();
			if (host != null)
				host.CloseType ();

			if (access == AssemblyBuilderAccess.RunAndSave)
				assembly.Save ();

			if (host == null)
				return null;
			
			//
			// Unlike Mono, .NET requires that the MethodInfo is fetched, it cant
			// work from MethodBuilders.   Retarded, I know.
			//
			var tt = assembly.Builder.GetType (host.TypeBuilder.Name);
			var mi = tt.GetMethod (expression_method.Name);

			if (host.Fields != null) {
				//
				// We need to then go from FieldBuilder to FieldInfo
				// or reflection gets confused (it basically gets confused, and variables override each
				// other).
				//
				foreach (Field field in host.Fields) {
					var fi = tt.GetField (field.Name);

					Tuple<FieldSpec, FieldInfo> old;

					// If a previous value was set, nullify it, so that we do
					// not leak memory
					if (fields.TryGetValue (field.Name, out old)) {
						if (old.Item1.MemberType.IsStruct) {
							//
							// TODO: Clear fields for structs
							//
//.........这里部分代码省略.........
开发者ID:runefs,项目名称:Marvin,代码行数:101,代码来源:eval.cs


注:本文中的Mono.CSharp.Class.EmitType方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。