本文整理汇总了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);
}
示例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));
}
示例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);
}
示例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 (";");
}