FieldBuilder

We found 10 examples in language CSharp for this search. You will see 46 fragments of code.
        private static FieldBuilder<TSourceType, TReturnType> BuildAstMeta<TSourceType, TReturnType>(FieldBuilder<TSourceType, TReturnType> fieldBuilder, string name, string value = null)
        {
            fieldBuilder.FieldType.BuildAstMeta(name, value);
            return fieldBuilder;
        }

        public static FieldBuilder<TSourceType, TReturnType> Key<TSourceType, TReturnType>(this FieldBuilder<TSourceType, TReturnType> fieldBuilder, string fields) => BuildAstMeta(fieldBuilder, "key", fields);
        public static FieldBuilder<TSourceType, TReturnType> Requires<TSourceType, TReturnType>(this FieldBuilder<TSourceType, TReturnType> fieldBuilder, string fields) => BuildAstMeta(fieldBuilder, "requires", fields);
        public static FieldBuilder<TSourceType, TReturnType> Provides<TSourceType, TReturnType>(this FieldBuilder<TSourceType, TReturnType> fieldBuilder, string fields) => BuildAstMeta(fieldBuilder, "provides", fields);
        public static FieldBuilder<TSourceType, TReturnType> External<TSourceType, TReturnType>(this FieldBuilder<TSourceType, TReturnType> fieldBuilder) => BuildAstMeta(fieldBuilder, "external");


            protected abstract void EmitConstructor(
                TypeBuilder typeBuilder,
                FieldBuilder fieldBuilder);

            public Type Emit()
            {
                this.Initialize(out var typeBuilder, out var fieldBuilder);

                this.EmitConstructor(typeBuilder, fieldBuilder);
                this.EmitProxynatedInterface(typeBuilder, fieldBuilder);
                this.EmitImplementationInterface(typeBuilder, fieldBuilder);
                this.EmitObjectInterface(typeBuilder, fieldBuilder);

                return typeBuilder.CreateTypeInfo();
            }


        private void AddRangeAttribute(FieldBuilder fieldBuilder, float min, float max)
        {
            var ctorParams = new Type[] { typeof(float), typeof(float) };
            var ctorInfo = typeof(RangeAttribute).GetConstructor(ctorParams);
            var rangeAttributeBuilder = new CustomAttributeBuilder(ctorInfo, new object[] { min, max });

            fieldBuilder.SetCustomAttribute(rangeAttributeBuilder);
        }

        private void AddNameAttribute(FieldBuilder fieldBuilder, string name)
        {
            var ctorParams = new Type[] { typeof(string) };
            var ctorInfo = typeof(NameAttribute).GetConstructor(ctorParams);
            var rangeAttributeBuilder = new CustomAttributeBuilder(ctorInfo, new object[] { name });

            fieldBuilder.SetCustomAttribute(rangeAttributeBuilder);
        }


        [Fact]
        public void SetConstant_Bool()
        {
            FieldBuilder field = s_type.DefineField("BoolField", typeof(bool), FieldAttributes.Public);
            
            field.SetConstant(false);
            field.SetConstant(true);
        }

        [Fact]
        public void SetConstant_SByte()
        {
            FieldBuilder field = s_type.DefineField("SByteField", typeof(sbyte), FieldAttributes.Public);
            
            field.SetConstant(sbyte.MinValue);
            field.SetConstant(sbyte.MaxValue);
        }

        [Fact]
        public void SetConstant_Short()
        {
            FieldBuilder field = s_type.DefineField("ShortField", typeof(short), FieldAttributes.Public);
            
            field.SetConstant(short.MaxValue);
            field.SetConstant(short.MinValue);
        }

        [Fact]
        public void SetConstant_Int()
        {
            FieldBuilder field = s_type.DefineField("IntField", typeof(int), FieldAttributes.Public);
            
            field.SetConstant(int.MinValue);
            field.SetConstant(int.MaxValue);
        }

        [Fact]
        public void SetConstant_Long()
        {
            FieldBuilder field = s_type.DefineField("LongField", typeof(long), FieldAttributes.Public);
            
            field.SetConstant(long.MaxValue);
            field.SetConstant(long.MinValue);
        }

        [Fact]
        public void SetConstant_Byte()
        {
            FieldBuilder field = s_type.DefineField("ByteField", typeof(byte), FieldAttributes.Public);
            
            field.SetConstant(byte.MinValue);
            field.SetConstant(byte.MaxValue);
        }


        public static string ToFistWordCase(string str, bool isLower = true)
        {
            if (string.IsNullOrEmpty(str)) return "";
            string first = str[0].ToString();
            return (isLower ? first.ToLower() : first.ToUpper()) + str.Substring(1);
        }

        /// <summary>
        /// 转换为成员变量名
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static string ToMemberVarName(string name)
        {
            return string.Format("_{0}", ToFistWordCase(name));
        }

        public static Hashtable LoadEnumApplication(int slnid, bool clean)
        {
            if (System.Web.HttpContext.Current.Application[slnid.ToString()] == null || clean)
            {
                Hashtable ht = new Hashtable();
                var list = DbDataLoader.GetEnumInfo(slnid);
                foreach (var dr in list)
                {
                    string key = "【" + dr.enumName + "】";
                    if (!ht.Contains(key))
                    {
                        ht.Add(key, string.Format("{0}\n\n{1}", dr.enumDescription,
                            dr.enumValueInfo));
                    }
                }
                System.Web.HttpContext.Current.Application[slnid.ToString()] = ht;
                return ht;
            }
            else
            {
                return (Hashtable)System.Web.HttpContext.Current.Application[slnid.ToString()];
            }
        }
        /// <summary>
        /// 开启通用方法
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static string ReadTemp(string fileName)
        {
            string code = Path.GetFileName(fileName);
            TemplateInfo tempInfo;
            if (!_tempCacheSet.TryGet(code, out tempInfo))
            {
                tempInfo = new TemplateInfo();
                tempInfo.Content = File.ReadAllText(fileName, Encoding.UTF8);
                _tempCacheSet.TryAdd(code, tempInfo);

            }
            return tempInfo.Content;
        }

        /// <summary>
        /// 赋值客户端模板
        /// </summary>
        /// <param name="content"></param>
        /// <param name="paramList"></param>
        /// <param name="reqParams"></param>
        /// <param name="title"></param>
        /// <param name="contractId"></param>
        /// <returns></returns>
        public static string FromatClientLuaTemp(string content, int contractId, List<ParamInfoModel> paramList, List<ParamInfoModel> reqParams, string title)
        {
            string[] expressList = new string[] { "##ID##", "##Description##", "##Parameter##", "##Fixed##" };
            foreach (string exp in expressList)
            {
                StringBuilder fieldBuilder = new StringBuilder();
                fieldBuilder.Append(exp.Replace("##", ""));
                if (fieldBuilder.ToString() == "ID")
                {
                    fieldBuilder.Remove(0, fieldBuilder.Length);
                    fieldBuilder.Append(contractId);
                }
                else if (fieldBuilder.ToString() == "Description")
                {
                    fieldBuilder.Remove(0, fieldBuilder.Length);
                    fieldBuilder.Append(title);
                }
                else if (fieldBuilder.ToString() == "Parameter")
                {
                    fieldBuilder.Remove(0, fieldBuilder.Length);
                    foreach (var paramInfo in reqParams)
                    {
                        if (paramInfo.ParamType == 1)
                        {
                            fieldBuilder.Append(", ");
                            fieldBuilder.Append(ToFistWordCase(paramInfo.Field));
                        }
                    }
                }
                else if (fieldBuilder.ToString() == "Fixed")
                {
                    //write to request param
                    fieldBuilder.Remove(0, fieldBuilder.Length);
                    string currIndent = GetSpaceIndent(1, 0);
                    foreach (var paramInfo in reqParams)
                    {
                        if (paramInfo.ParamType == 1)
                        {
                            fieldBuilder.Append(Environment.NewLine);
                            fieldBuilder.Append(currIndent);
                            switch (paramInfo.FieldType)
                            {
                                case FieldType.UInt:
                                case FieldType.Int:
                                    fieldBuilder.Append("ZyWriter:writeInt32(\"");
                                    break;
                                case FieldType.UShort:
                                case FieldType.Short:
                                    fieldBuilder.Append("ZyWriter:writeWord(\"");
                                    break;
                                case FieldType.DateTime:
                                case FieldType.ULong:
                                case FieldType.Long:
                                    fieldBuilder.Append("ZyWriter:writeInt64(\"");
                                    break;
                                case FieldType.Float:
                                    fieldBuilder.Append("ZyWriter:writeFloat(\"");
                                    break;
                                default:
                                    fieldBuilder.Append("ZyWriter:writeString(\"");
                                    break;
                            }
                            fieldBuilder.Append(paramInfo.Field);
                            fieldBuilder.Append("\", ");
                            fieldBuilder.Append(ToFistWordCase(paramInfo.Field));
                            fieldBuilder.Append(")");
                        }
                    }
                }
                content = content.Replace(exp, fieldBuilder.ToString());

            }
            return ReplaceClientLuaCallback(content, paramList);
        }

        public static string FromatClientQuickSendTemp(string content, int contractId, List<ParamInfoModel> paramList, List<ParamInfoModel> reqParams, string title)
        {
            string[] expressList = new string[] { "##ID##", "##Description##", "##Parameter##", "##Fixed##" };
            foreach (string exp in expressList)
            {
                StringBuilder fieldBuilder = new StringBuilder();
                fieldBuilder.Append(exp.Replace("##", ""));
                if (fieldBuilder.ToString() == "ID")
                {
                    fieldBuilder.Remove(0, fieldBuilder.Length);
                    fieldBuilder.Append(contractId);
                }
                else if (fieldBuilder.ToString() == "Description")
                {
                    fieldBuilder.Remove(0, fieldBuilder.Length);
                    fieldBuilder.Append(title);
                }
                else if (fieldBuilder.ToString() == "Parameter")
                {
                    fieldBuilder.Remove(0, fieldBuilder.Length);
                    foreach (var paramInfo in reqParams)
                    {
                        if (paramInfo.ParamType == 1)
                        {
                            fieldBuilder.Append(ToFistWordCase(paramInfo.Field));
                            fieldBuilder.Append(", ");
                        }
                    }
                }
                else if (fieldBuilder.ToString() == "Fixed")
                {
                    //write to request param
                    fieldBuilder.Remove(0, fieldBuilder.Length);
                    string currIndent = GetSpaceIndent(1, 0);
                    foreach (var paramInfo in reqParams)
                    {
                        if (paramInfo.ParamType == 1)
                        {
                            fieldBuilder.Append(Environment.NewLine);
                            fieldBuilder.Append(currIndent);
                            fieldBuilder.Append("url:add(\"");
                            fieldBuilder.Append(paramInfo.Field);
                            fieldBuilder.Append("\", ");
                            fieldBuilder.Append(ToFistWordCase(paramInfo.Field));
                            fieldBuilder.Append(")");
                        }
                    }
                }
                content = content.Replace(exp, fieldBuilder.ToString());

            }
            return content;//ReplaceClientLuaCallback(content, paramList);
        }


		[SetUp]
		protected void SetUp ()
		{
			AssemblyName assemblyName = new AssemblyName ();
			assemblyName.Name = "MonoTests.System.Reflection.Emit.FieldBuilderTest";

			AssemblyBuilder assembly = Thread.GetDomain ().DefineDynamicAssembly (
				assemblyName, AssemblyBuilderAccess.Run);

			module = assembly.DefineDynamicModule ("module1");
			_tb = module.DefineType (genTypeName (), TypeAttributes.Public);
		}

		[Test]
		public void TestFieldProperties ()
		{
			FieldBuilder field = _tb.DefineField ("name",
				typeof(string), FieldAttributes.Public);
			Assert.AreEqual (FieldAttributes.Public, field.Attributes);
			Assert.AreEqual (_tb, field.DeclaringType);
			Assert.AreEqual (typeof(string), field.FieldType);
			Assert.AreEqual ("name", field.Name);
			Assert.AreEqual (_tb, field.ReflectedType);
		}

		[Test]
		[ExpectedException (typeof(NotSupportedException))]
		public void TestFieldHandleIncomplete ()
		{
			FieldBuilder field = _tb.DefineField ("name",
				typeof(string), FieldAttributes.Public);
			RuntimeFieldHandle handle = field.FieldHandle;
		}

		[Test]
		[ExpectedException (typeof(NotSupportedException))]
		public void TestFieldHandleComplete ()
		{
			FieldBuilder field = _tb.DefineField ("name",
				typeof(string), FieldAttributes.Public);
			_tb.CreateType ();
			RuntimeFieldHandle handle = field.FieldHandle;
		}

		[Test]
		[ExpectedException (typeof(NotSupportedException))]
		public void TestGetCustomAttributesIncomplete ()
		{
			FieldBuilder field = _tb.DefineField ("name",
				typeof(string), FieldAttributes.Public);
			field.GetCustomAttributes (false);
		}

		[Test]
		[ExpectedException (typeof(NotSupportedException))]
		[Ignore ("mono supports this")]
		public void TestGetCustomAttributesComplete ()
		{
			FieldBuilder field = _tb.DefineField ("name",
				typeof(string), FieldAttributes.Public);
			_tb.CreateType ();
			field.GetCustomAttributes (false);
		}

        private static FieldBuilder<TSourceType, TReturnType> BuildAstMeta<TSourceType, TReturnType>(FieldBuilder<TSourceType, TReturnType> fieldBuilder, string name, string value = null)
        {
            fieldBuilder.FieldType.BuildAstMeta(name, value);
            return fieldBuilder;
        }

        public static FieldBuilder<TSourceType, TReturnType> Key<TSourceType, TReturnType>(this FieldBuilder<TSourceType, TReturnType> fieldBuilder, string fields) => BuildAstMeta(fieldBuilder, "key", fields);
        public static FieldBuilder<TSourceType, TReturnType> Requires<TSourceType, TReturnType>(this FieldBuilder<TSourceType, TReturnType> fieldBuilder, string fields) => BuildAstMeta(fieldBuilder, "requires", fields);
        public static FieldBuilder<TSourceType, TReturnType> Provides<TSourceType, TReturnType>(this FieldBuilder<TSourceType, TReturnType> fieldBuilder, string fields) => BuildAstMeta(fieldBuilder, "provides", fields);
        public static FieldBuilder<TSourceType, TReturnType> External<TSourceType, TReturnType>(this FieldBuilder<TSourceType, TReturnType> fieldBuilder) => BuildAstMeta(fieldBuilder, "external");


        public override void LoadAddressOfReference(ILGenerator gen)
        {
            if (isStatic)
            {
                gen.Emit(OpCodes.Ldsflda, Reference);
            }
            else
            {
                gen.Emit(OpCodes.Ldflda, Reference);
            }
        }

        public override void LoadReference(ILGenerator gen)
        {
            if (isStatic)
            {
                gen.Emit(OpCodes.Ldsfld, Reference);
            }
            else
            {
                gen.Emit(OpCodes.Ldfld, Reference);
            }
        }

        public override void StoreReference(ILGenerator gen)
        {
            if (isStatic)
            {
                gen.Emit(OpCodes.Stsfld, Reference);
            }
            else
            {
                gen.Emit(OpCodes.Stfld, Reference);
            }
        }


		public FieldBuilder DefineLiteral(string literalName, object literalValue)
		{
			FieldBuilder fb = typeBuilder.DefineField(literalName, typeBuilder, FieldAttributes.Public | FieldAttributes.Static | FieldAttributes.Literal);
			fb.SetConstant(literalValue);
			return fb;
		}

		public Type CreateType()
		{
			return typeBuilder.CreateType();
		}

		public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute)
		{
			typeBuilder.SetCustomAttribute(con, binaryAttribute);
		}

		public void SetCustomAttribute(CustomAttributeBuilder customBuilder)
		{
			typeBuilder.SetCustomAttribute(customBuilder);
		}

		public override Type GetEnumUnderlyingType()
		{
			return fieldBuilder.FieldType;
		}


        public void Offer(ByteString input)
        {
            _buffer += input;
        }

        public List<ByteString> Poll(bool requireLineEnd)
        {
            if (!_buffer.IsEmpty)
            {
                var preFirstData = _firstData;
                var prePos = _position;
                var preSieldStart = _fieldStart;
                var line = ParseLine(requireLineEnd);
                if (line!= null && line.Count > 0)
                {
                    _currentLineNo++;
                    DropReadBuffer();
                }
                else
                {
                    _firstData = preFirstData;
                    _position = prePos;
                    _fieldStart = preSieldStart;
                }
                return line;
            }
            return null;
        }

        private void DropReadBuffer()
        {
            _buffer = _buffer.Slice(_position);
            _position = 0;
            _fieldStart = 0;
        }

            public void Init(byte x)
            {
                if (!_useBuilder)
                {
                    _builder.Clear();
                    var start = _parser._fieldStart;
                    var count = _parser._position - start;
                    _builder.AddRange(_buffer.Slice(start, count));
                    _builder.Add(x);
                    _useBuilder = true;
                }
                else
                {
                    _builder.Add(x);
                }
            }

            public void Add(byte x)
            {
                if (_useBuilder)
                    _builder.Add(x);
            }

            public ByteString Result(int pos)
            {
                if (_useBuilder)
                {
                    _useBuilder = false;
                    return ByteString.FromBytes(_builder.ToArray());
                }
                return _buffer.Slice(_parser._fieldStart, pos - _parser._fieldStart);
            }