本文整理汇总了C#中System.Reflection.FieldInfo.GetValue方法的典型用法代码示例。如果您正苦于以下问题:C# FieldInfo.GetValue方法的具体用法?C# FieldInfo.GetValue怎么用?C# FieldInfo.GetValue使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Reflection.FieldInfo
的用法示例。
在下文中一共展示了FieldInfo.GetValue方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ConstructField
private void ConstructField(FieldInfo fieldInfo)
{
string valueStr = null;
if (fieldInfo.FieldType.IsArray)
{
var arrayBuilder = new StringBuilder();
arrayBuilder.Append("[");
var arr = fieldInfo.GetValue(Obj) as Array;
foreach (var element in arr)
{
arrayBuilder.Append(string.Format("{0}, ", ConstructValue(element)));
}
arrayBuilder.Remove(arrayBuilder.Length - 2, 2);
arrayBuilder.Append("]");
valueStr = arrayBuilder.ToString();
}
else
{
valueStr = ConstructValue(fieldInfo.GetValue(Obj));
}
if (valueStr == null)
{
throw new Exception("Unsupported data type");
}
builder.Append(string.Format("\"{0}\": {1},\n", fieldInfo.Name, valueStr));
}
示例2: Visit
public void Visit(string settingsNamesapce, string fieldPath, FieldInfo rawSettingsField, object rawSettings)
{
// Skip 'sealed' fields.
if (rawSettingsField.IsDefined<SealedAttribute>()) return;
if (rawSettingsField.FieldType == typeof(string))
{
var originalString = (string)rawSettingsField.GetValue(rawSettings);
if (originalString != null)
{
var expandedString = ExpandVariables(originalString, _variables);
rawSettingsField.SetValue(rawSettings, expandedString);
}
}
else if (rawSettingsField.FieldType == typeof(string[]))
{
string[] arr = (string[])rawSettingsField.GetValue(rawSettings);
for (int i = 0; i < arr.Length; i++)
{
string originalString = arr[i];
if (originalString != null)
{
var expandedString = ExpandVariables(originalString, _variables);
arr[i] = expandedString;
}
}
}
}
示例3: push_primitive
static void push_primitive(object obj, FieldInfo field, BuffBuilder bb)
{
if (field.FieldType == typeof(uint))
{
bb.PushUint((uint)field.GetValue(obj));
}
else if (field.FieldType == typeof(int))
{
bb.PushInt((int)field.GetValue(obj));
}
else if (field.FieldType == typeof(ushort))
{
bb.PushUshort((ushort)field.GetValue(obj));
}
else if (field.FieldType == typeof(short))
{
bb.PushShort((short)field.GetValue(obj));
}
else if (field.FieldType == typeof(ulong))
{
bb.PushUlong((ulong)field.GetValue(obj));
}
else if (field.FieldType == typeof(long))
{
bb.PushLong((long)field.GetValue(obj));
}
else if (field.FieldType == typeof(byte))
{
bb.PushByte((byte)field.GetValue(obj));
}
else if (field.FieldType == typeof(float))
{
bb.PushFloat((float)field.GetValue(obj));
}
}
示例4: Visit
public void Visit(string settingsNamesapce, string fieldPath, FieldInfo refinedSettingsField, object refinedSettings, FieldInfo rawSettingsField, object rawSettings)
{
if (refinedSettingsField.IsDefined<RefAttribute>()) return;
object rawValue = null;
object refinedValue = refinedSettingsField.GetValue(refinedSettings);
if (refinedValue != null)
{
Type refinedValueType = refinedValue.GetType();
if (refinedValueType.IsSettingsType())
{
rawValue = SettingsConstruction.CreateSettingsObject(refinedValue, rawSettingsField, _typeMappings);
}
else if (refinedValueType.IsSettingsOrObjectArrayType())
{
rawValue = SettingsConstruction.CreateSettingsArray((Array)refinedValue, rawSettingsField, _typeMappings);
}
else if (refinedValueType != typeof(string) && rawSettingsField.FieldType == typeof(string))
{
rawValue = ToString(refinedSettingsField, refinedSettings);
}
else
{
rawValue = refinedSettingsField.GetValue(refinedSettings);
}
rawSettingsField.SetValue(rawSettings, rawValue);
}
}
示例5: EvaluateMemberAccessField
private static object EvaluateMemberAccessField(ConstantExpression node, FieldInfo fieldAccessor)
{
object value;
if (fieldAccessor.IsStatic)
value = fieldAccessor.GetValue(null);
else
value = node == null ? null : fieldAccessor.GetValue(node.Value);
return ConvertMemberAccessValue(value);
}
示例6: GetFieldValues
public static string[] GetFieldValues(object ruleInfo, FieldInfo fieldInfo, Action<string> emitError)
{
var type = fieldInfo.FieldType;
if (type == typeof(string))
return new[] { (string)fieldInfo.GetValue(ruleInfo) };
if (type == typeof(string[]))
return (string[])fieldInfo.GetValue(ruleInfo);
emitError("Bad type for reference on {0}.{1}. Supported types: string, string[]"
.F(ruleInfo.GetType().Name, fieldInfo.Name));
return new string[] { };
}
示例7: StepArgument
public StepArgument(FieldInfo member, object declaringObject)
{
Name = member.Name;
_get = () => member.GetValue(declaringObject);
_set = o => member.SetValue(declaringObject, o);
ArgumentType = member.FieldType;
}
示例8: GetInstancePage
private static IBaseElement GetInstancePage(object parent, FieldInfo field, Type type, Type parentType)
{
var instance = (IBaseElement) field.GetValue(parent) ?? (IBaseElement) Activator.CreateInstance(type);
var pageAttribute = field.GetAttribute<PageAttribute>();
pageAttribute?.FillPage((WebPage) instance, parentType);
return instance;
}
示例9: TsEnumValue
/// <summary>
/// Initializes a new instance of the TsEnumValue class with the specific name and value.
/// </summary>
/// <param name="name">The name of the enum value.</param>
/// <param name="value">The value of the enum value.</param>
public TsEnumValue(FieldInfo field)
{
this.Field = field;
this.Name = field.Name;
var value = field.GetValue(null);
var valueType = Enum.GetUnderlyingType(value.GetType());
if (valueType == typeof(byte)) {
this.Value = ((byte)value).ToString();
}
if (valueType == typeof(sbyte)) {
this.Value = ((sbyte)value).ToString();
}
if (valueType == typeof(short)) {
this.Value = ((short)value).ToString();
}
if (valueType == typeof(ushort)) {
this.Value = ((ushort)value).ToString();
}
if (valueType == typeof(int)) {
this.Value = ((int)value).ToString();
}
if (valueType == typeof(uint)) {
this.Value = ((uint)value).ToString();
}
if (valueType == typeof(long)) {
this.Value = ((long)value).ToString();
}
if (valueType == typeof(ulong)) {
this.Value = ((ulong)value).ToString();
}
}
示例10: ObjectMeta
public ObjectMeta(FieldInfo field, object obj)
{
Type = field.FieldType;
Value = field.GetValue(obj);
Name = field.Name;
DisplayName = GetDisplayName();
}
示例11: LocalizeField
private static void LocalizeField(Control control, ResourceManager resourceManager, FieldInfo fi, string propertyName)
{
MemberInfo[] mia = fi.FieldType.GetMember(propertyName);
if (mia.GetLength(0) > 0)
{
try
{
string resourceName = control.GetType().Namespace.Replace(".", "_") + "_" + control.GetType().Name + "_" + fi.Name + "_" + propertyName;
string localizedText = resourceManager.GetString(resourceName);
if (localizedText == null)
{
}
else if (!string.IsNullOrEmpty(localizedText))
{
Object o = fi.GetValue(control);
if (o != null)
{
PropertyInfo pi = o.GetType().GetProperty(propertyName);
if (pi != null)
{
pi.SetValue(o, localizedText, null);
}
}
}
}
catch (System.Resources.MissingManifestResourceException)
{
//just ignore
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("Error when localizing: " + fi.Name + ":\r\n" + ex.ToString());
}
}
}
示例12: IsObjectField
/// <summary>
/// Checks to see if a field's type is an object.
/// </summary>
/// <param name="field"></param>
/// <param name="obj"></param>
/// <returns></returns>
internal static bool IsObjectField(FieldInfo field, object obj)
{
var fieldValue = field.GetValue(obj);
return fieldValue != null
&& !fieldValue.IsPrimitive();
}
示例13: WriteClassNameIfNeedBe
private void WriteClassNameIfNeedBe(object value, FieldInfo field) {
object fieldValue = field.GetValue(value);
if (fieldValue == null) return;
Type actualType = fieldValue.GetType();
if (!field.FieldType.Equals(actualType))
writer.WriteAttribute(Attributes.classType, actualType.AssemblyQualifiedName);
}
示例14: FieldMemberDescriptor
/// <summary>
/// Initializes a new instance of the <see cref="PropertyMemberDescriptor" /> class.
/// </summary>
/// <param name="fi">The FieldInfo.</param>
/// <param name="accessMode">The <see cref="InteropAccessMode" /> </param>
public FieldMemberDescriptor(FieldInfo fi, InteropAccessMode accessMode)
{
if (Script.GlobalOptions.Platform.IsRunningOnAOT())
accessMode = InteropAccessMode.Reflection;
FieldInfo = fi;
AccessMode = accessMode;
Name = fi.Name;
IsStatic = FieldInfo.IsStatic;
if (FieldInfo.IsLiteral)
{
IsConst = true;
m_ConstValue = FieldInfo.GetValue(null);
}
else
{
IsReadonly = FieldInfo.IsInitOnly;
}
if (AccessMode == InteropAccessMode.Preoptimized)
{
OptimizeGetter();
}
}
示例15: CreateSpecificationFromBehavior
public Specification CreateSpecificationFromBehavior(Behavior behavior, FieldInfo specificationField)
{
bool isIgnored = behavior.IsIgnored || specificationField.HasAttribute(new IgnoreAttributeFullName());
var it = (Delegate) specificationField.GetValue(behavior.Instance);
string name = specificationField.Name.ToFormat();
return new BehaviorSpecification(name, specificationField.FieldType, it, isIgnored, specificationField, behavior.Context, behavior);
}