static public TEntity GenEntityProxy2 <TEntity>(Func <string, object> getPropertyValue, Action <string, object> setPropertyValue) { GetPropertyDelegate getDelegate = new GetPropertyDelegate((methodInfo, propertyName) => CommonExtension.ToObject(getPropertyValue(propertyName), methodInfo.ReturnType)); SetPropertyDelegate setDelegate = new SetPropertyDelegate((methodInfo, propertyName, value) => setPropertyValue(propertyName, value)); return(GenEntityProxy <TEntity>(getDelegate, setDelegate)); }
/// <summary> /// Constructs a map that converts one or more values of one or more cells to a value of /// the of property or field. /// </summary> /// <param name="member">The property or field to map the value of a one or more cells to.</param> protected ExcelPropertyMap(MemberInfo member) { if (member == null) { throw new ArgumentNullException(nameof(member)); } if (member is PropertyInfo property) { if (!property.CanWrite) { throw new ArgumentException($"Property \"{member.Name}\" is read-only.", nameof(member)); } Member = member; SetPropertyFactory = (instance, value) => property.SetValue(instance, value); } else if (member is FieldInfo field) { Member = member; SetPropertyFactory = (instance, value) => field.SetValue(instance, value); } else { throw new ArgumentException($"Member \"{member.Name}\" is not a field or property.", nameof(member)); } }
private CustomViewRegistry() { _createCallback = new CreateControlDelegate(CreateControl); _getPropertyCallback = new GetPropertyDelegate(GetProperty); _setPropertyCallback = new SetPropertyDelegate(SetProperty); _constructorMap = new Dictionary <string, Func <CustomView> >(); _propertyRangeManager = new PropertyRangeManager(); }
public PropertyInvoker(object obj, string propertyName) { name = propertyName; string getter = "get_" + propertyName; string setter = "set_" + propertyName; Get = (GetPropertyDelegate <T>)Delegate.CreateDelegate(typeof(GetPropertyDelegate <T>), obj, getter); Set = (SetPropertyDelegate <T>)Delegate.CreateDelegate(typeof(SetPropertyDelegate <T>), obj, setter); }
public void Get_setter_method_for_simple_properties() { Type type = typeof(Test); PropertyInfo propertyInfo = type.GetProperty("TestProperty"); SetPropertyDelegate setMethod = JsvDeserializeType.GetSetPropertyMethod(type, propertyInfo); Test test = new Test(); setMethod.Invoke(test, "test"); Assert.AreEqual("test", test.TestProperty); }
/// <summary> Increment property value. If the value does not exist /// set it to 1. </summary> /// <example> For example: /// <code> /// IncrementRoomProperty("NumberOfRobbers"); /// </code> /// If the number of robbers is currently 2, the number will become 3. /// If the number of robbers is not set, the number will become 1. /// </example> public void IncrementProperty(string key, Hashtable properties, SetPropertyDelegate setProperty) { if (properties.ContainsKey(key)) { SetRoomProperty(key, (int)properties[key] + 1); } else { SetRoomProperty(key, 1); } }
public void Get_setter_method_for_dictionary_properties() { var dict = new Dictionary <string, string>(); Type type = typeof(Dictionary <string, string>); foreach (var propertyInfo in type.GetProperties()) { SetPropertyDelegate setMethod = JsvDeserializeType.GetSetPropertyMethod(type, propertyInfo); if (setMethod == null) { continue; } Console.WriteLine(propertyInfo.Name); setMethod.Invoke(dict, propertyInfo.Name); } }
internal bool SetPropertyDelegated <T>(T property, T newValue, SetPropertyDelegate setProperty, [CallerMemberName] string propertyName = "") { if (string.IsNullOrEmpty(propertyName)) { throw new ArgumentNullException(nameof(propertyName)); } if (!IsDifferent(property, newValue)) { return(false); } RaisePropertyChanging(propertyName); setProperty(); RaisePropertyChanged(propertyName); return(true); }
/// <summary> /// 设置目标对象的属性值 /// </summary> /// <param name="target">目标对象</param> /// <param name="value">需要设置的值</param> public void Set(object target, object value) { if (_PropertyInfo.CanWrite) { if (_SetDelegate == null) { _SetDelegate = setPropertyValue(_TargetType, _PropertyInfo); } //先把值转换为对应的数据类型 object cVal = MB.Util.MyReflection.Instance.ConvertValueType(_PropertyInfo.PropertyType, value); _SetDelegate.Invoke(target, cVal); } else { throw new DynamicPropertyAccessorException( string.Format("属性 \"{0}\" 不存在一个SET 方法.", _PropertyInfo.Name)); } }
public static void SetPropertyValue(Control ctrl, string propertyName, object value, params object[] args) { if (ctrl.InvokeRequired) { SetPropertyDelegate del = new SetPropertyDelegate(CrossThreadUI.SetPropertyValue); if (CrossThreadUI.ExecSync) { ctrl.Invoke(del, ctrl, propertyName, value, args); } else { ctrl.BeginInvoke(del, ctrl, propertyName, value, args); } } else { Type ctrlType = ctrl.GetType(); PropertyInfo pi = ctrlType.GetProperty(propertyName, Binding); // Make sure the object "Value" matches the property's Type. if (!value.GetType().IsAssignableFrom(pi.PropertyType)) { throw new ArgumentException("Value Type does not match property Type.", "value"); } if (pi != null) { try { pi.SetValue(ctrl, ((pi.PropertyType.FullName == "System.String") ? value.ToString() : value), args); } catch { throw; } } else { throw new ArgumentException("Specified control does not expose a '" + propertyName + "' property.", "propertyName"); } } }
public bool AddField <TMember>(int hash, SetPropertyDelegate <T, TMember> set, GetPropertyDelegate <T, TMember> get) { if (set == null) { throw new ArgumentNullException(nameof(set)); } if (get == null) { throw new ArgumentNullException(nameof(get)); } var memberType = typeof(TMember); if (Serializer.TryGetDataSerializer(memberType, out var methods, false) == false) { _serializer.Logger.Error($"{typeof(T)} custom serializer, cannot get serialization methods for field of type {memberType}"); return(false); } var toAdd = new MemberSerializerStruct(hash, new PropertyWriter <T, TMember>(methods, set, get)); Add(toAdd); return(true); }
static SecuredReflectionMethods() { if (!SecuredReflection.HasReflectionPermission) { if (!ProfilerInterceptor.IsProfilerAttached) { ProfilerInterceptor.ThrowElevatedMockingException(); } ProfilerInterceptor.CreateDelegateFromBridge("ReflectionInvoke", out Invoke); ProfilerInterceptor.CreateDelegateFromBridge("ReflectionGetProperty", out GetProperty); ProfilerInterceptor.CreateDelegateFromBridge("ReflectionSetProperty", out SetProperty); ProfilerInterceptor.CreateDelegateFromBridge("ReflectionGetField", out GetField); ProfilerInterceptor.CreateDelegateFromBridge("ReflectionSetField", out SetField); } else { Invoke = (method, instance, args) => method.Invoke(instance, args); GetProperty = (prop, instance, indexArgs) => prop.GetValue(instance, indexArgs); SetProperty = (prop, instance, value, indexArgs) => prop.SetValue(instance, value, indexArgs); GetField = (field, instance) => field.GetValue(instance); SetField = (field, instance, value) => field.SetValue(instance, value); } }
public PropertySerializerEntry(SetPropertyDelegate propertySetFn, ParseStringDelegate propertyParseStringFn) { PropertySetFn = propertySetFn; PropertyParseStringFn = propertyParseStringFn; }
public PropertySerializerEntry(SetPropertyDelegate propertySetFn, ParseStringDelegate propertyParseStringFn) { PropertySetFn = propertySetFn; PropertyParseStringFn = propertyParseStringFn; }
static void OverridePropertyHandlers(GType gtype, GetPropertyDelegate get_cb, SetPropertyDelegate set_cb) { IntPtr class_ptr = gtype.ClassPtr; GObjectClass klass = (GObjectClass) Marshal.PtrToStructure (class_ptr, typeof (GObjectClass)); klass.get_prop_cb = get_cb; klass.set_prop_cb = set_cb; Marshal.StructureToPtr (klass, class_ptr, false); }
static public TEntity GenEntityProxy <TEntity>(GetPropertyDelegate getPropertyValue, SetPropertyDelegate setPropertyValue) { RealProxy <TEntity> realProxy = new RealProxy <TEntity>(); realProxy.GetPropertyEvent += getPropertyValue; realProxy.SetPropertyEvent += setPropertyValue; return(realProxy.Entity); }
static extern void gtksharp_override_property_handlers (IntPtr type, GetPropertyDelegate get_cb, SetPropertyDelegate set_cb);
/// <summary> /// Sets a specified property value on a given control. An exception will be thrown if the control does not expose a property with the specified name. /// </summary> /// <param name="ctrl">The System.Windows.Forms.Control object whose property will be set.</param> /// <param name="propertyName">The name of the Property member that will receive the new value.</param> /// <param name="value">The value to assign to the property. This object must be properly typed to match the property's signature.</param> /// <param name="args">Any optional arguments that must be provided to access this property. Array objects must be properly typed to match property's signature.</param> public static void SetPropertyValue(Control ctrl, string propertyName, object value, params object[] args) { if (ctrl.InvokeRequired) { SetPropertyDelegate del = new SetPropertyDelegate(CrossThreadUI.SetPropertyValue); if (CrossThreadUI.ExecSync) ctrl.Invoke(del, ctrl, propertyName, value, args); else ctrl.BeginInvoke(del, ctrl, propertyName, value, args); } else { Type ctrlType = ctrl.GetType(); PropertyInfo pi = ctrlType.GetProperty(propertyName, Binding); if (pi != null) { // Make sure the object "Value" matches the property's Type. if (!value.GetType().IsAssignableFrom(pi.PropertyType)) throw new ArgumentException("Value Type does not match property Type.", "value"); try { pi.SetValue(ctrl, ((pi.PropertyType.FullName == "System.String") ? value.ToString() : value), args); } catch { throw; } } else throw new ArgumentException("Specified control does not expose a '" + propertyName + "' property.", "propertyName"); } }
static SecuredReflectionMethods() { if (!SecuredReflection.HasReflectionPermission) { if (!ProfilerInterceptor.IsProfilerAttached) ProfilerInterceptor.ThrowElevatedMockingException(); ProfilerInterceptor.CreateDelegateFromBridge("ReflectionInvoke", out Invoke); ProfilerInterceptor.CreateDelegateFromBridge("ReflectionGetProperty", out GetProperty); ProfilerInterceptor.CreateDelegateFromBridge("ReflectionSetProperty", out SetProperty); ProfilerInterceptor.CreateDelegateFromBridge("ReflectionGetField", out GetField); ProfilerInterceptor.CreateDelegateFromBridge("ReflectionSetField", out SetField); } else { Invoke = (method, instance, args) => method.Invoke(instance, args); GetProperty = (prop, instance, indexArgs) => prop.GetValue(instance, indexArgs); SetProperty = (prop, instance, value, indexArgs) => prop.SetValue(instance, value, indexArgs); GetField = (field, instance) => field.GetValue(instance); SetField = (field, instance, value) => field.SetValue(instance, value); } }
static extern void gtksharp_override_property_handlers(IntPtr type, GetPropertyDelegate get_cb, SetPropertyDelegate set_cb);
public PropertyWriter(DataSerializer dataSerializer, SetPropertyDelegate <T, TMember> set, GetPropertyDelegate <T, TMember> get) { _set = set; _get = get; _dataSerializer = dataSerializer; }