/// <summary> /// 通过字段信息获得成员对象 /// </summary> /// <param name="field">字段</param> /// <returns>字段成员对象</returns> /// <example> /// <code lang="c#"> /// <![CDATA[ /// public class UserInfo{ /// public int ID; /// /// public string Name; /// } /// UserInfo user= new UserInfo(); /// user.ID=1; /// /// Type type=typeof(UserInfo); /// FiledInfo filed=type.GetField("ID"); /// var member=Reflector.GetField(field); /// /// int value=member.GetValue(user).ToType<int>(); /// /// member.SetValue(user,2); /// /// ]]> /// </code> /// </example> public static IMemberAccessor GetField(FieldInfo field) { if (field == null) { throw new ArgumentNullException("field"); } IMemberAccessor accessor; if (_fieldCache.TryGetValue(field, out accessor)) { return(accessor); } lock (_fieldCache) { if (!_fieldCache.TryGetValue(field, out accessor)) { accessor = _refFactory.GetFieldAccessor(field); _fieldCache[field] = accessor; } } return(accessor); }
/// <summary> /// 通过字段信息获得成员连接器 /// </summary> /// <param name="field"></param> /// <returns></returns> public static IMemberAccessor GetAccessor(FieldInfo field) { IMemberAccessor accessor; if (_fieldCache.TryGetValue(field, out accessor)) { return(accessor); } lock (_fieldCache) { if (!_fieldCache.TryGetValue(field, out accessor)) { accessor = _refFactory.GetFieldAccessor(field); _fieldCache[field] = accessor; } } return(accessor); }