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


C# FieldInfo.GetRawConstantValue方法代码示例

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


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

示例1: CreateField

		public FieldSpec CreateField (FieldInfo fi, TypeSpec declaringType)
		{
			Modifiers mod = 0;
			var fa = fi.Attributes;
			switch (fa & FieldAttributes.FieldAccessMask) {
				case FieldAttributes.Public:
					mod = Modifiers.PUBLIC;
					break;
				case FieldAttributes.Assembly:
					mod = Modifiers.INTERNAL;
					break;
				case FieldAttributes.Family:
					mod = Modifiers.PROTECTED;
					break;
				case FieldAttributes.FamORAssem:
					mod = Modifiers.PROTECTED | Modifiers.INTERNAL;
					break;
				default:
					// Ignore private fields (even for error reporting) to not require extra dependencies
					if ((IgnorePrivateMembers && !declaringType.IsStruct) ||
						HasAttribute (CustomAttributeData.GetCustomAttributes (fi), "CompilerGeneratedAttribute", CompilerServicesNamespace))
						return null;

					mod = Modifiers.PRIVATE;
					break;
			}

			TypeSpec field_type;

			try {
				field_type = ImportType (fi.FieldType, new DynamicTypeReader (fi));
			} catch (Exception e) {
				// TODO: I should construct fake TypeSpec based on TypeRef signature
				// but there is no way to do it with System.Reflection
				throw new InternalErrorException (e, "Cannot import field `{0}.{1}' referenced in assembly `{2}'",
					declaringType.GetSignatureForError (), fi.Name, declaringType.MemberDefinition.DeclaringAssembly);
			}

			var definition = new ImportedMemberDefinition (fi, field_type, this);

			if ((fa & FieldAttributes.Literal) != 0) {
				var c = Constant.CreateConstantFromValue (field_type, fi.GetRawConstantValue (), Location.Null);
				return new ConstSpec (declaringType, definition, field_type, fi, mod, c);
			}

			if ((fa & FieldAttributes.InitOnly) != 0) {
				if (field_type.BuiltinType == BuiltinTypeSpec.Type.Decimal) {
					var dc = ReadDecimalConstant (CustomAttributeData.GetCustomAttributes (fi));
					if (dc != null)
						return new ConstSpec (declaringType, definition, field_type, fi, mod, dc);
				}

				mod |= Modifiers.READONLY;
			} else {
				var req_mod = fi.GetRequiredCustomModifiers ();
				if (req_mod.Length > 0 && HasVolatileModifier (req_mod))
					mod |= Modifiers.VOLATILE;
			}

			if ((fa & FieldAttributes.Static) != 0) {
				mod |= Modifiers.STATIC;
			} else {
				// Fixed buffers cannot be static
				if (declaringType.IsStruct && field_type.IsStruct && field_type.IsNested &&
					HasAttribute (CustomAttributeData.GetCustomAttributes (fi), "FixedBufferAttribute", CompilerServicesNamespace)) {

					// TODO: Sanity check on field_type (only few types are allowed)
					var element_field = CreateField (fi.FieldType.GetField (FixedField.FixedElementName), declaringType);
					return new FixedFieldSpec (declaringType, definition, fi, element_field, mod);
				}
			}

			return new FieldSpec (declaringType, definition, field_type, fi, mod);
		}
开发者ID:Gobiner,项目名称:ILSpy,代码行数:74,代码来源:import.cs

示例2: PrintField

 // printout fieldinfo
 // field attribute, custom attribute, field.tostring
 void PrintField(FieldInfo fi)
 {
     sw.Write(".field (" + ProcessAttributeString(fi.Attributes.ToString()) + ") " + PrintMember(fi));
     // we should be able to get the literal value out of fieldinfo
     if (fi.IsLiteral)
     {
         try
         {
             sw.WriteLine(" = " + fi.GetRawConstantValue());
         }
         catch (NotSupportedException) //TODO:remove this once VSWhidbey 398502 gets fixed
         {
             sw.Write(Environment.NewLine);
         }
     }
     else
     {
         sw.Write(Environment.NewLine);
     }
     PrintCustomAttributes(CustomAttributeData.GetCustomAttributes(fi));
 }
开发者ID:dbremner,项目名称:clrinterop,代码行数:23,代码来源:AssemPrinter.cs

示例3: CreateConstantFromValue

		Constant CreateConstantFromValue (TypeSpec fieldType, FieldInfo fi)
		{
			var value = fi.GetRawConstantValue ();
			//
			// Metadata value can be encoded using different constant value type
			// than is actual field type
			//
			// e.g. unsigned int16 CONSTANT = int16 (0x0000ffff)
			//
			if (value != null && !fieldType.IsEnum) {
				var c = ImportConstant (value);
				if (c != null) {
					return fieldType == c.Type ? c : c.ConvertExplicitly (false, fieldType);
				}
			}

			return Constant.CreateConstantFromValue (fieldType, value, Location.Null);
		}
开发者ID:ItsVeryWindy,项目名称:mono,代码行数:18,代码来源:import.cs

示例4: OutlineField

	void OutlineField (FieldInfo fi)
	{
		if (fi.IsPublic)   o.Write ("public ");
		if (fi.IsFamily)   o.Write ("protected ");
		if (fi.IsPrivate)  o.Write ("private ");
		if (fi.IsAssembly) o.Write ("internal ");
		if (fi.IsLiteral)  o.Write ("const ");
		else if (fi.IsStatic) o.Write ("static ");
		if (fi.IsInitOnly) o.Write ("readonly ");

		o.Write (FormatType (fi.FieldType));
		o.Write (" ");
		o.Write (fi.Name);
		if (fi.IsLiteral) { 
			object v = fi.GetRawConstantValue ();

			// TODO: Escape values here
			o.Write (" = ");
			if (v is char)
				o.Write ("'{0}'", v);
			else if (v is string)
				o.Write ("\"{0}\"", v);
			else
				o.Write (fi.GetRawConstantValue ());
		}
		o.Write (";");
	}
开发者ID:JokerMisfits,项目名称:linux-packaging-mono,代码行数:27,代码来源:outline.cs


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