/// <summary> /// NOTE: Code-generation-invoked method, method name and parameter order matters /// </summary> /// <param name="descriptor">descriptor</param> /// <param name="underlying">underlying</param> /// <param name="ex">exception</param> /// <returns>exception</returns> public static PropertyAccessException HandleException( DynamicPropertyDescriptorByMethod descriptor, object underlying, Exception ex) { if (ex is InvalidCastException invalidCastException) { throw PropertyUtility.GetMismatchException(descriptor.Method, underlying, invalidCastException); } if (ex is TargetException targetException) { throw PropertyUtility.GetTargetException(descriptor.Method, targetException); } if (ex is TargetInvocationException targetInvocationException) { throw PropertyUtility.GetTargetException(descriptor.Method, targetInvocationException); } if (ex is ArgumentException argumentException) { throw PropertyUtility.GetArgumentException(descriptor.Method, argumentException); } if (ex is MemberAccessException memberAccessException) { throw PropertyUtility.GetMemberAccessException(descriptor.Method, memberAccessException); } throw PropertyUtility.GetGeneralException(descriptor.Method, ex); }
/// <summary> /// NOTE: Code-generation-invoked method, method name and parameter order matters /// </summary> /// <param name="descriptor">descriptor</param> /// <param name="underlying">target</param> /// <param name="key">key</param> /// <returns>value</returns> public static bool DynamicMappedPropertyExists( DynamicPropertyDescriptorByMethod descriptor, object underlying, string key) { try { if (descriptor.HasParameters) { return(true); } var result = descriptor.Method.Invoke(underlying, null); return(result != null && GetMapKeyExistsChecked(result, key)); } catch (InvalidCastException e) { throw PropertyUtility.GetMismatchException(descriptor.Method, underlying, e); } catch (TargetInvocationException e) { throw PropertyUtility.GetTargetException(descriptor.Method, e); } catch (TargetException e) { throw PropertyUtility.GetTargetException(descriptor.Method, e); } catch (ArgumentException e) { throw PropertyUtility.GetArgumentException(descriptor.Method, e); } catch (MemberAccessException e) { throw PropertyUtility.GetMemberAccessException(descriptor.Method, e); } }
/// <summary> /// NOTE: Code-generation-invoked method, method name and parameter order matters /// </summary> /// <param name="descriptor">descriptor</param> /// <param name="underlying">target</param> /// <param name="parameters">params</param> /// <returns>value</returns> public static object DynamicMappedPropertyGet( DynamicPropertyDescriptorByMethod descriptor, object underlying, object[] parameters) { try { if (descriptor.HasParameters) { return(descriptor.Method.Invoke(underlying, parameters)); } var result = descriptor.Method.Invoke(underlying, null); if (result == null) { return(null); } return(GetMapValueChecked(result, parameters[0])); } catch (InvalidCastException e) { throw PropertyUtility.GetMismatchException(descriptor.Method, underlying, e); } catch (TargetInvocationException e) { throw PropertyUtility.GetTargetException(descriptor.Method, e); } catch (TargetException e) { throw PropertyUtility.GetTargetException(descriptor.Method, e); } catch (ArgumentException e) { throw PropertyUtility.GetArgumentException(descriptor.Method, e); } catch (MemberAccessException e) { throw PropertyUtility.GetMemberAccessException(descriptor.Method, e); } }
/// <summary> /// NOTE: Code-generation-invoked method, method name and parameter order matters /// </summary> /// <param name="descriptor">desc</param> /// <param name="underlying">underlying</param> /// <returns>value</returns> public static object DynamicSimplePropertyCall( DynamicPropertyDescriptorByMethod descriptor, object underlying) { try { return(descriptor.Method.Invoke(underlying, null)); } catch (Exception ex) { throw HandleException(descriptor, underlying, ex); } }
/// <summary> /// NOTE: Code-generation-invoked method, method name and parameter order matters /// </summary> /// <param name="descriptor">descriptor</param> /// <param name="underlying">target</param> /// <param name="parameters">params</param> /// <param name="index">idx</param> /// <returns>null or method</returns> public static object DynamicIndexedPropertyGet( DynamicPropertyDescriptorByMethod descriptor, object underlying, object[] parameters, int index) { try { if (descriptor.HasParameters) { return(descriptor.Method.Invoke(underlying, parameters)); } var result = descriptor.Method.Invoke(underlying, null); if (result == null) { return(null); } if (result is Array array) { return(array.Length > index?array.GetValue(index) : null); } if (result.GetType().IsGenericList()) { var list = result.AsObjectList(MagicMarker.SingletonInstance); return(list.Count > index ? list[index] : null); } return(null); } catch (InvalidCastException e) { throw PropertyUtility.GetMismatchException(descriptor.Method, underlying, e); } catch (TargetInvocationException e) { throw PropertyUtility.GetTargetException(descriptor.Method, e); } catch (TargetException e) { throw PropertyUtility.GetTargetException(descriptor.Method, e); } catch (ArgumentException e) { throw PropertyUtility.GetArgumentException(descriptor.Method, e); } catch (MemberAccessException e) { throw PropertyUtility.GetMemberAccessException(descriptor.Method, e); } }
/// <summary> /// NOTE: Code-generation-invoked method, method name and parameter order matters /// </summary> /// <param name="clazz">class</param> /// <param name="method">method</param> /// <param name="cache">cache</param> /// <returns>descriptor</returns> public static DynamicPropertyDescriptorByMethod DynamicPropertyCacheAdd( Type clazz, MethodInfo method, CopyOnWriteList <DynamicPropertyDescriptorByMethod> cache) { DynamicPropertyDescriptorByMethod propertyDescriptor; if (method == null) { propertyDescriptor = new DynamicPropertyDescriptorByMethod(clazz, null, false); } else { propertyDescriptor = new DynamicPropertyDescriptorByMethod(clazz, method, method.GetParameters().Length > 0); } cache.Add(propertyDescriptor); return(propertyDescriptor); }
/// <summary> /// Call the getter to obtains the return result object, or null if no such method exists. /// </summary> /// <param name="descriptor">provides method information for the class</param> /// <param name="underlying">is the underlying object to ask for the property value</param> /// <returns>underlying</returns> protected abstract object Call( DynamicPropertyDescriptorByMethod descriptor, object underlying);
protected override object Call( DynamicPropertyDescriptorByMethod descriptor, object underlying) { return(DynamicMappedPropertyGet(descriptor, underlying, _parameters)); }
protected override object Call( DynamicPropertyDescriptorByMethod descriptor, object underlying) { return(DynamicSimplePropertyCall(descriptor, underlying)); }