/// <summary> /// 动态设置指定名称的成员的值。 /// </summary> /// <param name="name">成员的名称,可以是一个属性或字段。</param> /// <param name="value">成员的值。</param> public virtual void Set(string name, object value) { if (string.IsNullOrWhiteSpace(name)) { throw new ArgumentNullException(nameof(name)); } var memberInfo = Type.GetMember(name, AllBindingFalgs).FirstOrDefault(); DynamicMemberSetter d = null; if (memberInfo != null) { if (memberInfo.MemberType == MemberTypes.Property) { d = (memberInfo as PropertyInfo).CreatePropertySetter(); } else if (memberInfo.MemberType == MemberTypes.Field) { d = (memberInfo as FieldInfo).CreateFieldSetter(); } } if (d == null) { throw new MissingMemberException(Type.FullName, name); } d(Instance, value); }
public DynamicMemberHandle(string memberName, Type memberType, DynamicMemberGetter dynamicMemberGet, DynamicMemberSetter dynamicMemberSet) { MemberName = memberName; MemberType = memberType; DynamicMemberGet = dynamicMemberGet; DynamicMemberSet = dynamicMemberSet; }
public SerializableFieldInfo(FieldInfo field, int depth) { this.Field = field; if(depth == 0) this.Name = field.Name; else this.Name = depth.ToString() + "#" + field.Name; this.GetValue = field.CreateFieldGetter(); this.SetValue = field.CreateFieldSetter(); //this.Name //this.NameHashCode = this.Name.GetHashCode(); }
public SerializableFieldInfo(FieldInfo field, int depth) { this.Field = field; if (depth == 0) { this.Name = field.Name; } else { this.Name = depth.ToString() + "#" + field.Name; } this.GetValue = field.CreateFieldGetter(); this.SetValue = field.CreateFieldSetter(); //this.Name //this.NameHashCode = this.Name.GetHashCode(); }