Esempio n. 1
0
		public static IInCord InCordByMethodFactory(InMethodDefenition methodInfo, object Contract)
		{
            var parameters = methodInfo.MethodDefenition.GetParameters().Select(p => p.ParameterType).ToArray();
            var returnType = methodInfo.MethodDefenition.ReturnType;

			if (parameters.Length == 1) { //Usual monoparameter cord
				if (returnType == typeof(void)) {
                    var ccord = JustInCordFactory(parameters, methodInfo.Attribute);
                    ccord.OnReceive += (sender, msg) => methodInfo.MethodDefenition.Invoke(Contract, new object[] { msg });
					return ccord;
				} else {
                    var acord = AnswerCordFactory(parameters, returnType, methodInfo.Attribute);
					acord.OnAsk += (sender, id, msg) => {
                        var res = methodInfo.MethodDefenition.Invoke(Contract, new object[] { msg });
						acord.SendAnswer (res, id);
					};
					return acord;
				}
			} else { //Sequence deserialization
				if (returnType == typeof(void)) {// no-answer cord
                    var icord = new InCord<object[]>(methodInfo.Attribute.CordId, new SequenceDeserializer(parameters));
                    icord.OnReceiveT += (sender, msg) => methodInfo.MethodDefenition.Invoke(Contract, msg);
					return icord;
				} else {// answering cord
					var ser = SerializersFactory.Create (returnType);
                    var acord = new AnsweringCord(methodInfo.Attribute.CordId, new SequenceDeserializer(parameters), ser);
					acord.OnAsk += (sender, id, msg) => {
                        var res = methodInfo.MethodDefenition.Invoke(Contract, msg as object[]);
						acord.SendAnswer (res, id);
                        };
                    
					return acord;
				}
			}
		}
Esempio n. 2
0
		public static IInCord InCordByEventFactory(InEventDefenition raiseEventInfo, object Contract)
		{
            var meth = raiseEventInfo.RaiseField.FieldType.GetMethod("Invoke");

			var parameters = meth.GetParameters ().Select(p=>p.ParameterType).ToArray();
			var returnType = meth.ReturnType;

			if (parameters.Length == 1) { //Usual monoparameter cord
				if (returnType == typeof(void)) {
                    var ccord = JustInCordFactory(parameters, raiseEventInfo.Attribute);
					ccord.OnReceive += (sender, msg) => 
					{
                        var eventDelegate = raiseEventInfo.RaiseField.GetValue(Contract) as MulticastDelegate;

						if (eventDelegate != null)
							foreach (var handler in eventDelegate.GetInvocationList()) 
								handler.Method.Invoke (handler.Target, new object[] { msg });
					};
					return ccord;
				} else {
					var acord = AnswerCordFactory (parameters, returnType, raiseEventInfo.Attribute);
					acord.OnAsk += (sender, id, msg) => {
                        var eventDelegate = raiseEventInfo.RaiseField.GetValue(Contract) as MulticastDelegate;
						object ans = null;
						if (eventDelegate != null)
						{
							foreach (var handler in eventDelegate.GetInvocationList()) 
								ans = handler.Method.Invoke (handler.Target, new object[] { msg });
							acord.SendAnswer (ans, id);
						}
					};
					return acord;
				}
			} else { //Sequence deserialization
				if (returnType == typeof(void)) {// no-answer cord
                    var icord = new InCord<object[]>(raiseEventInfo.Attribute.CordId, new SequenceDeserializer(parameters));
					icord.OnReceiveT += (sender, msg) => {
                        var eventDelegate = raiseEventInfo.RaiseField.GetValue(Contract) as MulticastDelegate;
						if (eventDelegate != null)
							foreach (var handler in eventDelegate.GetInvocationList()) 
								handler.Method.Invoke (handler.Target, msg);
					};
					return icord;
				} else {// answering cord
					var ser = SerializersFactory.Create (returnType);
                    var acord = new AnsweringCord(raiseEventInfo.Attribute.CordId, new SequenceDeserializer(parameters), ser);
					acord.OnAsk += (sender, id, msg) => {
						object ans = null;
                        var eventDelegate = raiseEventInfo.RaiseField.GetValue(Contract) as MulticastDelegate;
						if (eventDelegate != null)
						{
							foreach (var handler in eventDelegate.GetInvocationList()) 
								ans = handler.Method.Invoke (handler.Target, msg as object[]);
							acord.SendAnswer(ans, id);
						}
					};
					return acord;
				}
			}
		}