private object GetValue(object obj, int index) { PropertyInfo prop; FieldInfo field; if ((prop = members[index] as PropertyInfo) != null) { if (obj == null) { return(Helpers.IsValueType(prop.PropertyType) ? Activator.CreateInstance(prop.PropertyType) : null); } //return prop.GetValue(obj, null); return(PropertyGetMethod.GetValueNew(prop, obj)); } else if ((field = members[index] as FieldInfo) != null) { if (obj == null) { return(Helpers.IsValueType(field.FieldType) ? Activator.CreateInstance(field.FieldType) : null); } return(field.GetValue(obj)); } else { throw new InvalidOperationException(); } }
public override bool TryGet(string key, bool publicOnly, out object value) { MemberInfo[] members = Helpers.GetInstanceFieldsAndProperties(attribute.GetType(), publicOnly); foreach (MemberInfo member in members) { #if FX11 if (member.Name.ToUpper() == key.ToUpper()) #else if (string.Equals(member.Name, key, StringComparison.OrdinalIgnoreCase)) #endif { PropertyInfo prop = member as PropertyInfo; if (prop != null) { //value = prop.GetValue(attribute, null); value = PropertyGetMethod.GetValueNew(prop, attribute); return(true); } FieldInfo field = member as FieldInfo; if (field != null) { value = field.GetValue(attribute); return(true); } throw new NotSupportedException(member.GetType().Name); } } value = null; return(false); }
public override void Write(object value, ProtoWriter dest) { Helpers.DebugAssert(value != null); //value = property.GetValue(value, null); value = PropertyGetMethod.GetValueNew(property, value); if (value != null) { Tail.Write(value, dest); } }
public override void Implement(DynamicTypeBuilder config, TypeBuilder typeBuilder) { // GET method il var baseProperty = typeBuilder.BaseType.GetProperty(this.MemberName); var getMethodIlGenerator = PropertyGetMethod.GetILGenerator(); if (HasBase) { getMethodIlGenerator.Emit(OpCodes.Ldarg_0); getMethodIlGenerator.Emit(OpCodes.Call, baseProperty.GetGetMethod()); getMethodIlGenerator.Emit(OpCodes.Ret); } else { getMethodIlGenerator.Emit(OpCodes.Ldarg_0); getMethodIlGenerator.Emit(OpCodes.Ldfld, this.BackingField); getMethodIlGenerator.Emit(OpCodes.Ret); } // SET method il if (!IsReadOnly) { var setMethodIlGenerator = PropertySetMethod.GetILGenerator(); Label returnLabel; Label originalReturn = returnLabel = setMethodIlGenerator.DefineLabel(); foreach (var interceptor in this.SetInterceptors) { interceptor.Intercept(this, PropertySetMethod, setMethodIlGenerator, ref returnLabel); } if (HasBase) { setMethodIlGenerator.Emit(OpCodes.Ldarg_0); setMethodIlGenerator.Emit(OpCodes.Ldarg_1); setMethodIlGenerator.Emit(OpCodes.Call, baseProperty.GetSetMethod()); } else { setMethodIlGenerator.Emit(OpCodes.Ldarg_0); setMethodIlGenerator.Emit(OpCodes.Ldarg_1); setMethodIlGenerator.Emit(OpCodes.Stfld, BackingField); } setMethodIlGenerator.Emit(OpCodes.Br_S, returnLabel); setMethodIlGenerator.MarkLabel(originalReturn); setMethodIlGenerator.Emit(OpCodes.Ret); } }
public override object Read(object value, ProtoReader source) { Helpers.DebugAssert(value != null); //object oldVal = Tail.RequiresOldValue ? property.GetValue(value, null) : null; object oldVal = Tail.RequiresOldValue ? PropertyGetMethod.GetValueNew(property, value) : null; object newVal = Tail.Read(oldVal, source); if (readOptionsWriteValue && newVal != null) // if the tail returns a null, intepret that as *no assign* { if (shadowSetter == null) { property.SetValue(value, newVal, null); } else { shadowSetter.Invoke(value, new object[] { newVal }); } } return(null); }
public PropertyAccessorInfo(PropertyGetMethod getMethod, PropertySetMethod setmethod) { _getMethod = getMethod; _setMethod = setmethod; }