Пример #1
0
        private void InitMethodsFromType(Type type, bool isAttributedMethodsOnly)
        {
            foreach (var methodInfo in type.GetMethods())
            {
                RpcMethodAttribute methodAttr = GetMethodAttribute(methodInfo);
                if (methodAttr != null || !isAttributedMethodsOnly)
                {
                    RpcMethodDescriptor method = new RpcMethodDescriptor(methodInfo, methodAttr, mName);
                    if (mMethods.ContainsKey(method.Name))
                    {
                        mMethods[method.Name].Merge(method);
                    }
                    else
                    {
                        mMethods.Add(method.Name, method);
                    }
                }
            }

            //for classes checking the public methods is enough, but for an interface we must check the other
            //interfaces implemented too
            if (type.IsInterface)
            {
                foreach (var interfaceType in type.GetInterfaces())
                {
                    InitMethodsFromType(interfaceType, isAttributedMethodsOnly);
                }
            }
        }
Пример #2
0
        public RpcMethodDescriptor(MethodInfo methodInfo, RpcMethodAttribute attr, string serviceName)
        {
            mServiceName = serviceName;

            Type[] methodParamTypes = methodInfo.GetParameters().Select(p => p.ParameterType).ToArray();

            if (methodInfo.Name.StartsWith("Begin") && methodParamTypes.Length >= 2 &&
                methodInfo.ReturnType == typeof(IAsyncResult) &&
                methodParamTypes[methodParamTypes.Length - 2] == typeof(AsyncCallback) &&
                methodParamTypes[methodParamTypes.Length - 1] == typeof(object))
            {
                mAsyncBeginMethodInfo = methodInfo;
                mParameterTypes       = methodParamTypes.Take(methodParamTypes.Length - 2).ToArray();
                if (attr != null && attr.Name != null)
                {
                    mName = attr.Name;
                }
                else
                {
                    mName = methodInfo.Name.Substring(5);
                }
            }
            else if (methodInfo.Name.StartsWith("End") && methodParamTypes.Length == 1 &&
                     methodParamTypes[0] == typeof(IAsyncResult))
            {
                mAsyncEndMethodInfo = methodInfo;
                mReturnType         = methodInfo.ReturnType;
                if (attr != null && attr.Name != null)
                {
                    mName = attr.Name;
                }
                else
                {
                    mName = methodInfo.Name.Substring(3);
                }
            }
            else
            {
                mSyncMethodInfo = methodInfo;
                mParameterTypes = methodParamTypes;
                mReturnType     = methodInfo.ReturnType;
                if (attr != null && attr.Name != null)
                {
                    mName = attr.Name;
                }
                else
                {
                    mName = methodInfo.Name;
                }
            }

            //verify parameters
            if (mParameterTypes != null)
            {
                foreach (Type paramType in mParameterTypes)
                {
                    if (!ParameterConverter.IsSupportedType(paramType))
                    {
                        throw new ArgumentException(
                                  String.Format(
                                      "Parameter of type '{0}' on method '{1}' in service '{2}' is not a supported type.",
                                      paramType, methodInfo.Name, serviceName));
                    }
                }
            }

            //verify return type
            if (mReturnType != null && mReturnType != typeof(void))
            {
                if (!ParameterConverter.IsSupportedType(mReturnType))
                {
                    throw new ArgumentException(
                              String.Format("Return type '{0}' of method '{1}' in service '{2}' is not a supported type",
                                            mReturnType, mName, serviceName));
                }
            }
        }