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


C# Compiler.Add方法代码示例

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


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

示例1: CreateView

        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        private TinyView CreateView(IControllerContext context, IViewData data)
        {
            string templateName = context.ViewPath + ".tiny";
            Resource resource = _provider.Get(templateName);
            if (resource == null)
            {
                _logger.Warning("Failed to load " + templateName);
                return null;
            }

            // Add all namespaces and assemblies needed.
            var compiler = new Compiler();
            compiler.Add(typeof (IView));
            compiler.Add(typeof (string));
            compiler.Add(typeof (TextWriter));
            foreach (var parameter in data)
                compiler.Add(parameter.Value.GetType());

            using (resource.Stream)
            {
                var buffer = new byte[resource.Stream.Length];
                resource.Stream.Read(buffer, 0, buffer.Length);

                TextReader reader = new StreamReader(resource.Stream);
                var sb = new StringBuilder();

                // parse
                Parse(reader, sb);

                // and add it in the type.
                string code = ClassContents;
                code = code.Replace("{body}", sb.ToString());

                // add view data types.
                string types = string.Empty;
                foreach (var parameter in data)
                {
                    types = Compiler.GetTypeName(parameter.Value.GetType()) + " " + parameter.Key + " = (" +
                            Compiler.GetTypeName(parameter.Value.GetType()) + ")viewData[\"" + parameter.Key + "\"];";
                }
                code = code.Replace("{viewDataTypes}", types);

                try
                {
                    compiler.Compile(code);
                }
                catch (Exception err)
                {
                    _logger.Error("Failed to compile template " + templateName + ", reason: " + err);
                }
                return compiler.CreateInstance<TinyView>();
            }
        }
开发者ID:oblivious,项目名称:Oblivious,代码行数:59,代码来源:TinyViewEngine.cs

示例2: EmitWriteArrayLoop

        private void EmitWriteArrayLoop(Compiler.CompilerContext ctx, Compiler.Local i, Compiler.Local arr)
        {
            // i = 0
            ctx.LoadValue(0);
            ctx.StoreValue(i);

            // range test is last (to minimise branches)
            Compiler.CodeLabel loopTest = ctx.DefineLabel(), processItem = ctx.DefineLabel();
            ctx.Branch(loopTest, false);
            ctx.MarkLabel(processItem);

            // {...}
            ctx.LoadArrayValue(arr, i);
            if (SupportNull)
            {
                Tail.EmitWrite(ctx, null);
            }
            else
            {
                ctx.WriteNullCheckedTail(itemType, Tail, null);
            }

            // i++
            ctx.LoadValue(i);
            ctx.LoadValue(1);
            ctx.Add();
            ctx.StoreValue(i);

            // i < arr.Length
            ctx.MarkLabel(loopTest);
            ctx.LoadValue(i);
            ctx.LoadLength(arr, false);
            ctx.BranchIfLess(processItem, false);
        }
开发者ID:CragonGame,项目名称:GameCloud.IM,代码行数:34,代码来源:ArrayDecorator.cs

示例3: EmitReadEndPacked

		private void EmitReadEndPacked(Compiler.CompilerContext ctx, Compiler.Local list, Compiler.Local oldArr,
										Compiler.Local newArr, Type listType)
		{
			// leave this "using" here, as it can share the "FieldNumber" local with EmitReadList
			using(Compiler.Local oldLen = AppendToCollection ? new ProtoBuf.Compiler.Local(ctx, typeof(int)) : null) {
				Type[] copyToArrayInt32Args = new Type[] { typeof(Array), typeof(int) };

				if (AppendToCollection)
				{
					ctx.LoadLength(oldArr, true);
					ctx.CopyValue();
					ctx.StoreValue(oldLen);

					ctx.LoadAddress(list, listType);
					ctx.LoadValue(listType.GetProperty("Count"));
					ctx.Add();
					ctx.CreateArray(itemType, null); // length is on the stack
					ctx.StoreValue(newArr);

					ctx.LoadValue(oldLen);
					Compiler.CodeLabel nothingToCopy = ctx.DefineLabel();
					ctx.BranchIfFalse(nothingToCopy, true);
					ctx.LoadValue(oldArr);
					ctx.LoadValue(newArr);
					ctx.LoadValue(0); // index in target

					ctx.EmitCall(ExpectedType.GetMethod("CopyTo", copyToArrayInt32Args));
					ctx.MarkLabel(nothingToCopy);

					ctx.LoadValue(list);
					ctx.LoadValue(newArr);
					ctx.LoadValue(oldLen);
                        
				}
				else
				{
					ctx.LoadAddress(list, listType);
					ctx.LoadValue(listType.GetProperty("Count"));
					ctx.CreateArray(itemType, null);
					ctx.StoreValue(newArr);

					ctx.LoadAddress(list, listType);
					ctx.LoadValue(newArr);
					ctx.LoadValue(0);
				}

				copyToArrayInt32Args[0] = ExpectedType; // // prefer: CopyTo(T[], int)
				MethodInfo copyTo = listType.GetMethod("CopyTo", copyToArrayInt32Args);
				if (copyTo == null)
				{ // fallback: CopyTo(Array, int)
					copyToArrayInt32Args[1] = typeof(Array);
					copyTo = listType.GetMethod("CopyTo", copyToArrayInt32Args);
				}
				ctx.EmitCall(copyTo);
			}

			ctx.LoadValue(newArr);
		}
开发者ID:JayCap,项目名称:Protobuf-net-Patch-and-T4-TypeModel-Generator,代码行数:58,代码来源:ArrayDecorator.cs

示例4: TemplateCompiler

 /// <summary>
 /// Create a new template compiler
 /// </summary>
 public TemplateCompiler()
 {
     _compiler = new Compiler();
     _compiler.Add(GetType());
     _compiler.Add(typeof(ITinyTemplate));
 }
开发者ID:Horlos,项目名称:web-server,代码行数:9,代码来源:TemplateCompiler.cs

示例5: EmitRead

        public void EmitRead(Compiler.CompilerContext ctx, Compiler.Local incoming)
        {
			bool issueReferenceDirectives = !baseTupleAsReference && asReference;
        	
			Compiler.CodeLabel @end = ctx.DefineLabel();

			using (Compiler.Local tupleKey = new Compiler.Local(ctx, typeof(int)))
			using (Compiler.Local oldTuple = new Compiler.Local(ctx, typeof(object)))
			using (Compiler.Local objValue = ctx.GetLocalWithValue(ExpectedType, incoming))
			{
				Compiler.Local[] locals = new Compiler.Local[members.Length];
				try
				{
					if (issueReferenceDirectives)
					{
						//int tupleKey = 0;
						ctx.LoadValue(0);
						ctx.StoreValue(tupleKey);

						//object oldTuple = null;
						ctx.LoadNullRef();
						ctx.StoreValue(oldTuple);

						//tupleKey = (int)source.ReadUInt32();
						ctx.LoadReaderWriter();
						ctx.EmitCall(typeof (ProtoReader).GetMethod("ReadUInt32"));
						//ctx.CastToObject(typeof (int));
						ctx.StoreValue(tupleKey);

						Compiler.CodeLabel @objectNotFound = ctx.DefineLabel();

						ctx.LoadValue(tupleKey);
						ctx.LoadValue(0);
						ctx.BranchIfEqual(@objectNotFound, true);

						//// return source.NetCache.GetKeyedObject(tupleKey);
						ctx.LoadReaderWriter();
						ctx.LoadValue(typeof (ProtoReader).GetProperty("NetCache"));
						ctx.LoadValue(tupleKey);
						ctx.EmitCall(typeof (NetObjectCache).GetMethod("GetKeyedObject"));
						ctx.CastFromObject(ExpectedType);
						ctx.StoreValue(objValue);
						ctx.Branch(@end, false);

						ctx.MarkLabel(@objectNotFound);

						ctx.EmitCtor(typeof (object));
						ctx.StoreValue(oldTuple);

						// tupleKey = source.NetCache.AddObjectKey(oldTuple, out dummy);
						using (Compiler.Local dummy = new Compiler.Local(ctx, typeof (bool)))
						{
							ctx.LoadReaderWriter();
							ctx.LoadValue(typeof (ProtoReader).GetProperty("NetCache"));
							ctx.LoadValue(oldTuple);
							ctx.LoadAddress(dummy, typeof (bool));
							ctx.EmitCall(typeof (NetObjectCache).GetMethod("AddObjectKey", new Type[] { typeof(object), typeof(bool).MakeByRefType() }));
							ctx.StoreValue(tupleKey);
						}
					}

					for (int i = 0; i < locals.Length; i++)
					{
						Type type = GetMemberType(i);
						bool store = true;
						locals[i] = new Compiler.Local(ctx, type);
						if (!ExpectedType.IsValueType)
						{
							// value-types always read the old value
							if (type.IsValueType)
							{
								switch (Helpers.GetTypeCode(type))
								{
									case ProtoTypeCode.Boolean:
									case ProtoTypeCode.Byte:
									case ProtoTypeCode.Int16:
									case ProtoTypeCode.Int32:
									case ProtoTypeCode.SByte:
									case ProtoTypeCode.UInt16:
									case ProtoTypeCode.UInt32:
										ctx.LoadValue(0);
										break;
									case ProtoTypeCode.Int64:
									case ProtoTypeCode.UInt64:
										ctx.LoadValue(0L);
										break;
									case ProtoTypeCode.Single:
										ctx.LoadValue(0.0F);
										break;
									case ProtoTypeCode.Double:
										ctx.LoadValue(0.0D);
										break;
									case ProtoTypeCode.Decimal:
										ctx.LoadValue(0M);
										break;
									case ProtoTypeCode.Guid:
										ctx.LoadValue(Guid.Empty);
										break;
									default:
										ctx.LoadAddress(locals[i], type);
//.........这里部分代码省略.........
开发者ID:JayCap,项目名称:Protobuf-net-Patch-and-T4-TypeModel-Generator,代码行数:101,代码来源:TupleSerializer.cs


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