protected static void PushParameter <T>(ref InvocationContext ctx, InvocationTypes type, T val) { switch (type) { case InvocationTypes.Integer: ctx.PushInteger(val); break; case InvocationTypes.Long: ctx.PushLong(val); break; case InvocationTypes.Float: ctx.PushFloat(val); break; case InvocationTypes.Double: ctx.PushDouble(val); break; default: ctx.PushObject(val); break; } }
internal void PushParameter <T>(InvocationTypes type, T val) { switch (type) { case InvocationTypes.Integer: PushInteger(val); break; case InvocationTypes.Long: PushLong(val); break; case InvocationTypes.Float: PushFloat(val); break; case InvocationTypes.Double: PushDouble(val); break; case InvocationTypes.Enum: PushObject(val, false); break; case InvocationTypes.ValueType: PushValueType(ref val); break; default: PushObject(val); break; } }
/// <summary> /// Initializes a new AdapterInvocationInformation instance. /// </summary> /// <param name="targetMethod">Target method.</param> /// <param name="adapterMethodParameterTypes">Adapter method parameter types.</param> /// <param name="targetMethodParameterTypes">Target method parameter types.</param> /// <param name="targetInvocationType">Target invocation type.</param> /// <param name="invocationHelper">Generic method instance of AdapterInterceptor's generic method definition for invoking methods returning generic tasks.</param> public AdapterInvocationInformation(MethodInfo targetMethod, Type[] adapterMethodParameterTypes, Type[] targetMethodParameterTypes, InvocationTypes targetInvocationType, MethodInfo?invocationHelper = null) { TargetMethod = targetMethod; AdapterMethodParameterTypes = adapterMethodParameterTypes; TargetMethodParameterTypes = targetMethodParameterTypes; TargetInvocationType = targetInvocationType; InvocationHelper = invocationHelper; }
private void LoadSquareBracketGroupingProperties(XmlNode node) { XmlElement element = node as XmlElement; if (element.HasAttribute(Constants.XmlRuleElementConstants.MEMBER_OF_COLLECTION)) { _memberOfCollectionString = element.Attributes[Constants.XmlRuleElementConstants.MEMBER_OF_COLLECTION].Value; } if (element.HasAttribute(Constants.XmlRuleElementConstants.INVOCATION_TYPE)) { _invocationType = TranslationHelper.InvocationTypesTranslation(element.Attributes[Constants.XmlRuleElementConstants.INVOCATION_TYPE].Value); } }
private InvocationTypes GetInvocationType(XmlNode node) { //this is the default InvocationTypes invocationType = InvocationTypes.AsProperty; //unless it is specified XmlElement element = node as XmlElement; if (element.HasAttribute(Translation.Constants.XmlRuleElementConstants.INVOCATION_TYPE)) { invocationType = TranslationHelper.InvocationTypesTranslation(element.GetAttribute(Translation.Constants.XmlRuleElementConstants.INVOCATION_TYPE)); } return(invocationType); }
protected virtual AdapterInvocationInformation PrepareAdapterInvocationInformation(MethodInfo adapterMethod, MethodInfo targetMethod, Type[] adapterMethodParameterTypes, Type[] targetMethodParmeterTypes) { Type targetMethodReturnType = targetMethod.ReturnType; // Task<> if (AdapterHelper.IsGenericTask(targetMethodReturnType)) { MethodInfo genericInvokeTargetGenericTaskAsyncMethod = _invokeTargetGenericTaskAsync_Method.MakeGenericMethod(adapterMethod.ReturnType.GenericTypeArguments[0]); InvocationTypes targetInvocationType = InvocationTypes.GenericTask; AssertReturnTypeCombination(adapterMethod, targetMethod, targetInvocationType); return(new AdapterInvocationInformation(targetMethod, adapterMethodParameterTypes, targetMethodParmeterTypes, targetInvocationType, genericInvokeTargetGenericTaskAsyncMethod)); } // Task else if (AdapterHelper.IsTask(targetMethodReturnType)) { InvocationTypes targetInvocationType = InvocationTypes.Task; AssertReturnTypeCombination(adapterMethod, targetMethod, targetInvocationType); return(new AdapterInvocationInformation(targetMethod, adapterMethodParameterTypes, targetMethodParmeterTypes, targetInvocationType)); } // ValueTask<> else if (AdapterHelper.IsGenericValueTask(targetMethodReturnType)) { MethodInfo genericInvokeTargetGenericValueTaskAsyncMethod = _invokeTargetGenericValueTaskAsync_Method.MakeGenericMethod(adapterMethod.ReturnType.GenericTypeArguments[0]); InvocationTypes targetInvocationType = InvocationTypes.GenericValueTask; AssertReturnTypeCombination(adapterMethod, targetMethod, targetInvocationType); return(new AdapterInvocationInformation(targetMethod, adapterMethodParameterTypes, targetMethodParmeterTypes, targetInvocationType, genericInvokeTargetGenericValueTaskAsyncMethod)); } // ValueTask else if (AdapterHelper.IsValueTask(targetMethodReturnType)) { InvocationTypes targetInvocationType = InvocationTypes.ValueTask; AssertReturnTypeCombination(adapterMethod, targetMethod, targetInvocationType); return(new AdapterInvocationInformation(targetMethod, adapterMethodParameterTypes, targetMethodParmeterTypes, targetInvocationType)); } else // Likely synchronous { InvocationTypes targetInvocationType = InvocationTypes.Sync; AssertReturnTypeCombination(adapterMethod, targetMethod, targetInvocationType); return(new AdapterInvocationInformation(targetMethod, adapterMethodParameterTypes, targetMethodParmeterTypes, targetInvocationType)); } }
protected static T ReadResult <T>(ref InvocationContext ctx, InvocationTypes type) { switch (type) { case InvocationTypes.Integer: return(ctx.ReadInteger <T>()); case InvocationTypes.Long: return(ctx.ReadLong <T>()); case InvocationTypes.Float: return(ctx.ReadFloat <T>()); case InvocationTypes.Double: return(ctx.ReadDouble <T>()); default: return(ctx.ReadObject <T>()); } }
internal T ReadResult <T>(InvocationTypes type) { switch (type) { case InvocationTypes.Integer: return(ReadInteger <T>()); case InvocationTypes.Long: return(ReadLong <T>()); case InvocationTypes.Float: return(ReadFloat <T>()); case InvocationTypes.Double: return(ReadDouble <T>()); default: return(ReadObject <T>()); } }
static MethodDelegateAdapter() { pType = InvocationContext.GetInvocationType <T1>(); }
/// <summary> /// Registers the rule with the current <see cref="ModelContext"/>. /// </summary> public void Register(ModelType rootType = null) { // Determine if the rule is be registered for a specific root model type if (rootType != null) { if (this.rootType != null) { throw new InvalidOperationException("Rules cannot be explicitly registered for more than one model type."); } this.rootType = rootType; } // Raise the Initialization event the first time the rule is registered if (Initialize != null) { Initialize(this, EventArgs.Empty); Initialize = null; } // Default the invocation type to PropertyChanged if none were assigned if ((int)InvocationTypes == 1) { InvocationTypes = RuleInvocationType.PropertyChanged; } // Automatically detect predicates if none were specified if (Predicates == null && ((InvocationTypes & (RuleInvocationType.PropertyChanged | RuleInvocationType.PropertyGet)) > 0)) { SetPredicates(GetPredicates()); } // Track and validate uniqueness of condition types HashSet <ConditionType> conditionTypes = RootType.GetExtension <HashSet <ConditionType> >(); foreach (var conditionType in ConditionTypes) { if (conditionTypes.Contains(conditionType)) { throw new InvalidOperationException("Registered condition types must be unique for each model type: " + conditionType.Code); } conditionTypes.Add(conditionType); } // Track the rule registration for the root model type List <Rule> rules = RootType.GetExtension <List <Rule> >(); rules.Add(this); // Do not perform model type event registration if the rule is not supposed to execute on the server if ((ExecutionLocation & RuleExecutionLocation.Server) == 0) { return; } // Init Invocation if ((InvocationTypes & RuleInvocationType.InitExisting) == RuleInvocationType.InitExisting || (InvocationTypes & RuleInvocationType.InitNew) == RuleInvocationType.InitNew) { RootType.Init += (sender, e) => { if (((InvocationTypes & RuleInvocationType.InitExisting) == RuleInvocationType.InitExisting && !e.Instance.IsNew) || ((InvocationTypes & RuleInvocationType.InitNew) == RuleInvocationType.InitNew && e.Instance.IsNew)) { Invoke(e.Instance, e); } }; } // Property Get Invocation if ((InvocationTypes & RuleInvocationType.PropertyGet) == RuleInvocationType.PropertyGet) { // Subscribe to property get notifications for all return values RootType.PropertyGet += (sender, e) => { // Determine if the rule is responsible for calculating the property being accessed if (ReturnValues.Contains(e.Property.Name)) { // Get the rule manager for the current instance var manager = e.Instance.GetExtension <RuleManager>(); // Determine if the rule needs to be run if ((e.IsFirstAccess && (!e.Property.IsPersisted || e.Instance.IsNew || manager.IsPendingInvocation(this))) || manager.IsPendingInvocation(this)) { // Invoke the rule Invoke(e.Instance, e); // Mark the rule state as no longer requiring invocation manager.SetPendingInvocation(this, false); } } }; if (!InvocationTypes.HasFlag(RuleInvocationType.SuppressPropertyChanged)) { // Subscribe to property change notifications for all rule predicates foreach (string predicate in Predicates) { RootType.GetPath(predicate).Change += (sender, e) => { // Only invoke the rule if the instance is of the same type as the rule root type if (RootType.IsInstanceOfType(e.Instance)) { // Get the rule manager for the current instance var manager = e.Instance.GetExtension <RuleManager>(); // Mark the rule state as requiring invocation if (manager.SetPendingInvocation(this, true)) { // Raise property change notifications foreach (var property in ReturnValues) { e.Instance.Type.Properties[property].NotifyPathChange(e.Instance); } } } }; } } // Mark the properties as calculated to denote that they are return values of a rule foreach (var property in ReturnValues) { var prop = RootType.Properties[property]; if (prop != null) { prop.IsCalculated = true; } } } // Property Change Invocation if ((InvocationTypes & RuleInvocationType.PropertyChanged) == RuleInvocationType.PropertyChanged) { // Subscribe to property change notifications for all rule predicates foreach (string predicate in Predicates) { RootType.GetPath(predicate).Change += (sender, e) => { // Only invoke the rule if the instance is of the same type as the rule root type if (RootType.IsInstanceOfType(e.Instance)) { // Get the rule manager for the current instance var manager = e.Instance.GetExtension <RuleManager>(); // Register the rule to run if it is not already registered if (manager.SetPendingInvocation(this, true)) { // Invoke the rule when the last model event scope exits ModelEventScope.OnExit(() => { // Mark the rule state as no longer requiring invocation manager.SetPendingInvocation(this, false); // Invoke the rule Invoke(e.Instance, e); }); } } }; } } // Allow subclasses to perform additional registration logic OnRegister(); }
static MethodDelegateAdapter() { pType = GetInvocationType <T1>(); }
public MachineWorkStep(string stepName, InvocationTypes invocationType) : this(stepName) { this.InvocationType = invocationType; }
public LogStep(InvocationTypes type, string stepName = "Log Step") : base(stepName, type) { }
protected virtual void AssertReturnTypeCombination(MethodInfo adapterMethod, MethodInfo targetMethod, InvocationTypes targetInvocationType) { Type targetMethodReturnType = targetMethod.ReturnType; switch (targetInvocationType) { case InvocationTypes.Sync: if (AdapterHelper.IsVoid(adapterMethod.ReturnType) && adapterMethod.ReturnType != targetMethodReturnType) { throw new AdapterInterceptorException($"Adapter and target method return types should match if either is void.{Environment.NewLine}Adapter method: {adapterMethod.ToLoggerString()}{Environment.NewLine}Target method: {targetMethod.ToLoggerString()}"); } break; case InvocationTypes.GenericTask: if (!AdapterHelper.IsGenericTask(adapterMethod.ReturnType)) { throw new AdapterInterceptorException($"Adapter and target method return types should both be a generic Task.{Environment.NewLine}Adapter method: {adapterMethod.ToLoggerString()}{Environment.NewLine}Target method: {targetMethod.ToLoggerString()}"); } break; case InvocationTypes.Task: if (adapterMethod.ReturnType != targetMethodReturnType) { throw new AdapterInterceptorException($"Adapter and target method return types should match if target return type is Task.{Environment.NewLine}Adapter method: {adapterMethod.ToLoggerString()}{Environment.NewLine}Target method: {targetMethod.ToLoggerString()}"); } break; case InvocationTypes.GenericValueTask: if (!AdapterHelper.IsGenericValueTask(adapterMethod.ReturnType)) { throw new AdapterInterceptorException($"Adapter and target method return types should both be a generic ValueTask.{Environment.NewLine}Adapter method: {adapterMethod.ToLoggerString()}{Environment.NewLine}Target method: {targetMethod.ToLoggerString()}"); } break; case InvocationTypes.ValueTask: if (!AdapterHelper.IsValueTask(adapterMethod.ReturnType)) { throw new AdapterInterceptorException($"Adapter and target method return types should match if target return type is ValueTask.{Environment.NewLine}Adapter method: {adapterMethod.ToLoggerString()}{Environment.NewLine}Target method: {targetMethod.ToLoggerString()}"); } break; default: throw new NotImplementedException(); } }
public void AssertReturnTypeCombination_InvalidReturnTypesCombination_ThrowsException(InvocationTypes invocationType, Type adapterReturnType, Type targetReturnType, string expectedErrorMessage) { var adapterMethodStub = new Mock <MethodInfo>(); adapterMethodStub.Setup(instance => instance.ReturnType).Returns(adapterReturnType); var targetMethodStub = new Mock <MethodInfo>(); targetMethodStub.Setup(instance => instance.ReturnType).Returns(targetReturnType); var ex = Assert.Throws <AdapterInterceptorException>(() => AssertReturnTypeCombination(adapterMethodStub.Object, targetMethodStub.Object, invocationType)); TestContext.WriteLine(ex); Assert.AreEqual(expectedErrorMessage, ex.Message); }