/// <summary> /// Get the specialized property value. /// </summary> //6.0 TODO: Merge into IAccessOptimizer. internal static object GetSpecializedPropertyValue(this IAccessOptimizer optimizer, object target) { if (optimizer is AccessOptimizer accessOptimizer) { return(accessOptimizer.GetSpecializedPropertyValue(target)); } throw new InvalidOperationException($"{optimizer.GetType()} does not support {nameof(GetPropertyValue)} method."); }
/// <summary> /// Set the property value on the given index. /// </summary> //6.0 TODO: Merge into IAccessOptimizer. public static void SetPropertyValue(this IAccessOptimizer optimizer, object target, int i, object value) { if (optimizer is AccessOptimizer accessOptimizer) { accessOptimizer.SetPropertyValue(target, i, value); return; } throw new InvalidOperationException($"{optimizer.GetType()} does not support {nameof(SetPropertyValue)} method."); }
/// <summary> /// Class constructor. /// </summary> public ReflectionOptimizer(System.Type mappedType, IGetter[] getters, ISetter[] setters) { // save off references this.mappedType = mappedType; typeOfThis = mappedType.IsValueType ? mappedType.MakeByRefType() : mappedType; //this.getters = getters; //this.setters = setters; GetPropertyValuesInvoker getInvoker = GenerateGetPropertyValuesMethod(getters); SetPropertyValuesInvoker setInvoker = GenerateSetPropertyValuesMethod(getters, setters); accessOptimizer = new AccessOptimizer(getInvoker, setInvoker, getters, setters); createInstanceMethod = CreateCreateInstanceMethod(mappedType); }
public void SetUp() { _accessor = PropertyAccessorFactory.GetPropertyAccessor(AccessorType); _getters = new IGetter[PropertyNames.Count]; _setters = new ISetter[PropertyNames.Count]; var type = typeof(T); for (var i = 0; i < PropertyNames.Count; i++) { _getters[i] = _accessor.GetGetter(type, PropertyNames[i]); _setters[i] = _accessor.GetSetter(type, PropertyNames[i]); } _optimizer = new ReflectionOptimizer(type, _getters, _setters, null, null).AccessOptimizer; }
public void TestGetterTypeMismatch() { var obj = new GetterTypeMismatchClass(); const string property = "Property"; NoSetterAccessor accessor = new NoSetterAccessor(new CamelCaseUnderscoreStrategy()); Assert.IsTrue(accessor.CanAccessThroughReflectionOptimizer); ReflectionOptimizer reflectionOptimizer = new ReflectionOptimizer( obj.GetType(), new[] { accessor.GetGetter(obj.GetType(), property) }, new[] { accessor.GetSetter(obj.GetType(), property) }); IAccessOptimizer accessOptimizer = reflectionOptimizer.AccessOptimizer; accessOptimizer.SetPropertyValues(obj, new object[] { 10 }); object[] values = accessOptimizer.GetPropertyValues(obj); Assert.AreEqual("str", values[0]); }
/// <summary> /// Class constructor. /// </summary> public ReflectionOptimizer( System.Type mappedType, IGetter[] getters, ISetter[] setters, IGetter specializedGetter, ISetter specializedSetter) { // save off references this.mappedType = mappedType; typeOfThis = mappedType.IsValueType ? mappedType.MakeByRefType() : mappedType; //this.getters = getters; //this.setters = setters; GetPropertyValuesInvoker getInvoker = GenerateGetPropertyValuesMethod(getters); SetPropertyValuesInvoker setInvoker = GenerateSetPropertyValuesMethod(setters); var getMethods = new GetPropertyValueInvoker[getters.Length]; for (var i = 0; i < getters.Length; i++) { getMethods[i] = GenerateGetPropertyValueMethod(getters[i]); } var setMethods = new SetPropertyValueInvoker[setters.Length]; for (var i = 0; i < setters.Length; i++) { setMethods[i] = GenerateSetPropertyValueMethod(setters[i]); } accessOptimizer = new AccessOptimizer( getInvoker, setInvoker, getMethods, setMethods, GenerateGetPropertyValueMethod(specializedGetter), GenerateSetPropertyValueMethod(specializedSetter) ); createInstanceMethod = CreateCreateInstanceMethod(mappedType); }