예제 #1
0
 public void RegisterHandlers(IEnumerable <Type> InstanceTypes, IEnumerable <Type> MessageTypes, string methodnameprefix = "")
 {
     foreach (var InstanceType in InstanceTypes)
     {
         var methodnames = InstanceType.GetMethods().Where(x =>
                                                           x.ReturnType == typeof(void) &&
                                                           x.Name.StartsWith(methodnameprefix) &&
                                                           (x.GetBaseDefinition() == null || x.GetBaseDefinition().DeclaringType != typeof(Scritchy.Domain.AR))
                                                           ).Select(x => x.Name).ToArray();
         foreach (var methodname in methodnames)
         {
             var messagetype = MessageTypes.Where(x => x.Name == methodname.Substring(methodnameprefix.Length)).FirstOrDefault();
             if (messagetype == null)
             {
                 continue;
             }
             var localmn                      = methodname;
             var localinstancetype            = InstanceType;
             var props                        = localinstancetype.GetMethod(localmn).GetParameters().ToDictionary(x => x.Name, x => x.ParameterType);
             Action <object, object[]> invoke = (instance, parameters) => localinstancetype.GetMethod(localmn).Invoke(instance, parameters);
             Action <object, object, IParameterResolver> invoker = (instance, message, pr) => {
                 var pars = pr.ResolveParameters(props, message).ToArray();
                 invoke(instance, pars);
             };
             this.Register(InstanceType, messagetype, invoker);
         }
     }
 }
예제 #2
0
        public override QsValue ExclamationOperator(QsValue key)
        {
            if (key.ToString().Equals("TypeName", StringComparison.OrdinalIgnoreCase))
            {
                return(new QsText(InstanceType.Name));
            }

            if (key.ToString().Equals("Properties", StringComparison.OrdinalIgnoreCase))
            {
                QsFlowingTuple f   = new QsFlowingTuple();
                var            mms = InstanceType.GetProperties(BindingFlags.Public | BindingFlags.Instance);

                QsTupleValue[] fefe = new QsTupleValue[mms.Length];

                for (int ix = 0; ix < mms.Length; ix++)
                {
                    fefe[ix].Name = (string)mms[ix].Name;

                    fefe[ix].Value = new QsText(mms[ix].PropertyType.Name);
                }
                if (fefe.Length == 0)
                {
                    return(new QsFlowingTuple());
                }
                return(new QsFlowingTuple(fefe));
            }

            if (key.ToString().Equals("Methods", StringComparison.OrdinalIgnoreCase))
            {
                QsFlowingTuple f   = new QsFlowingTuple();
                var            mms = from m in InstanceType.GetMethods(BindingFlags.Public | BindingFlags.Instance)
                                     where m.IsSpecialName == false
                                     select m;

                QsTupleValue[] fefe = new QsTupleValue[mms.Count()];

                for (int ix = 0; ix < mms.Count(); ix++)
                {
                    fefe[ix].Name = (string)mms.ElementAt(ix).Name;

                    fefe[ix].Value = new QsText(mms.ElementAt(ix).ReturnType.Name);
                }
                if (fefe.Length == 0)
                {
                    return(new QsFlowingTuple());
                }
                return(new QsFlowingTuple(fefe));
            }

            if (key.ToString().Equals("Type", StringComparison.OrdinalIgnoreCase))
            {
                return(new QsObject(InstanceType));
            }
            return(new QsText("Unknown Command"));
        }