本文整理汇总了C#中IReflect.GetField方法的典型用法代码示例。如果您正苦于以下问题:C# IReflect.GetField方法的具体用法?C# IReflect.GetField怎么用?C# IReflect.GetField使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IReflect
的用法示例。
在下文中一共展示了IReflect.GetField方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetFieldByPropertyName
private static FieldInfo GetFieldByPropertyName(IReflect viewModelType, string propertyName)
{
var charList = new List<char> { char.ToLower(propertyName[0]) };
charList.AddRange(propertyName.Substring(1));
var fieldName = new string(charList.ToArray());
var field = viewModelType.GetField(fieldName, BindingFlags.Instance | BindingFlags.NonPublic);
return field;
}
示例2: GetMember
private static MemberInfo GetMember(IReflect entityType, string[] memberNames)
{
var publicProperties =
from name in memberNames
select entityType.GetProperty(name, BindingFlags.Instance | BindingFlags.Public) into propertyInfo
where propertyInfo != null && IsPropertyOk(propertyInfo)
select propertyInfo;
var privateProperties =
from name in memberNames
select entityType.GetProperty(name, BindingFlags.Instance | BindingFlags.NonPublic) into propertyInfo
where propertyInfo != null && IsPropertyOk(propertyInfo)
select propertyInfo;
var publicFields =
from name in memberNames
select entityType.GetField(name, BindingFlags.Instance | BindingFlags.Public) into fieldInfo
where fieldInfo != null && IsFildOk(fieldInfo)
select fieldInfo;
return publicProperties.OfType<MemberInfo>().Concat(privateProperties).Concat(publicFields).FirstOrDefault();
}
示例3: SetField
/// <summary>
/// Sets the field.
/// </summary>
/// <param name="type">The type.</param>
/// <param name="strMethod">The STR method.</param>
/// <param name="objInstance">The obj instance.</param>
/// <param name="eFlags">The e flags.</param>
/// <param name="setValue">The set value.</param>
/// <returns></returns>
private static object SetField(IReflect type, string strMethod, object objInstance, BindingFlags eFlags, object setValue)
{
FieldInfo field;
try
{
if ((eFlags == InstanceBindingFlags) && (objInstance == null))
{
throw new ArgumentException("The reflection non-static object argument was invalid");
}
if ((eFlags == StaticBindingFlags) && (objInstance != null))
{
throw new ArgumentException("The reflection static object argument was invalid");
}
if ((objInstance != null) && (objInstance.GetType() != (Type)type) && (objInstance.GetType().BaseType != (Type)type))
{
throw new ArgumentException("The object instance was of type '" + objInstance.GetType() + "' for type '" + type + "'.");
}
if (string.IsNullOrEmpty(strMethod))
{
throw new ArgumentException("The reflection method argument was invalid");
}
field = type.GetField(strMethod, eFlags);
if (field == null)
{
throw new ArgumentException("There is no field '" + strMethod + "' for type '" + type + "'.");
}
if (setValue.GetType() != field.FieldType)
{
throw new ArgumentException("The value instance was of type '" + setValue.GetType() + "' for field type '" + field.FieldType + "'.");
}
field.SetValue(objInstance, setValue);
var objRet = field.GetValue(objInstance);
return objRet;
}
catch (Exception exception)
{
Console.WriteLine(exception.Message);
throw;
}
}
示例4: GetInstanceField
private static object GetInstanceField(IReflect type, object instance, string fieldName)
{
const BindingFlags bindFlags = BindingFlags.Instance | BindingFlags.NonPublic;
var field = type.GetField(fieldName, bindFlags);
return field.GetValue(instance);
}
示例5: GetField
private static FieldInfo GetField(IReflect type, string fieldName) {
return type.GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
}