private static void BuildClass(JsonRpcServiceClassBuilder builder, Type type)
        {
            //
            // Build via attributes.
            //

            object[] attributes = type.GetCustomAttributes(typeof(IServiceClassReflector), true);
            foreach (IServiceClassReflector reflector in attributes)
                reflector.Build(builder, type);
            
            //
            // Fault in the type name if still without name.
            //

            if (builder.Name.Length == 0)
                builder.Name = type.Name;

            //
            // Get all the public instance methods on the type and create a
            // filtered table of those to expose from the service.
            //
            
            MethodInfo[] publicMethods = type.GetMethods(BindingFlags.Public | BindingFlags.Instance);

            foreach (MethodInfo method in publicMethods)
            {
                if (ShouldBuild(method))
                    BuildMethod(builder.DefineMethod(), method);
            }
        }
        internal JsonRpcServiceClass(JsonRpcServiceClassBuilder classBuilder)
        {
            Debug.Assert(classBuilder != null);
            
            _serviceName = classBuilder.Name;
            _description = classBuilder.Description;

            JsonRpcMethodBuilder[] methodBuilders = classBuilder.GetMethods();
            
            _methods = new JsonRpcMethod[methodBuilders.Length];
            _methodByName = new Hashtable(methodBuilders.Length);
            int methodIndex = 0;

            foreach (JsonRpcMethodBuilder methodBuilder in methodBuilders)
            {
                JsonRpcMethod method = new JsonRpcMethod(methodBuilder, this);

                //
                // Check for duplicates.
                //

                if (_methodByName.ContainsKey(method.Name))
                    throw new DuplicateMethodException(string.Format("The method '{0}' cannot be exported as '{1}' because this name has already been used by another method on the '{2}' service.", method.Name, method.InternalName, _serviceName));

                //
                // Add the method to the class and index it by its name.
                //

                _methods[methodIndex++] = method;
                _methodByName.Add(method.Name, method);
            }
        }
 public static JsonRpcServiceClass FromType(Type type)
 {
     if (type == null)
         throw new ArgumentNullException("type");
     
     JsonRpcServiceClassBuilder builder = new JsonRpcServiceClassBuilder();
     BuildClass(builder, type);
     return builder.CreateClass();
 }
        internal JsonRpcServiceClass(JsonRpcServiceClassBuilder classBuilder)
        {
            Debug.Assert(classBuilder != null);
            
            _serviceName = classBuilder.Name;
            _description = classBuilder.Description;

            //
            // Set up methods and their names.
            //

            ICollection methodBuilders = classBuilder.Methods;
            _methods = new JsonRpcMethod[methodBuilders.Count];
            _methodNames = new string[methodBuilders.Count];
            int methodIndex = 0;

            foreach (JsonRpcMethodBuilder methodBuilder in methodBuilders)
            {
                JsonRpcMethod method = new JsonRpcMethod(methodBuilder, this);

                //
                // Check for duplicates.
                //

                if (Array.IndexOf(_methodNames, method.Name) >= 0)
                    throw new DuplicateMethodException(string.Format("The method '{0}' cannot be exported as '{1}' because this name has already been used by another method on the '{2}' service.", method.Name, method.InternalName, _serviceName));

                //
                // Add the method to the class and index it by its name.
                //

                _methods[methodIndex] = method;
                _methodNames[methodIndex++] = method.Name;
            }

            //
            // Keep a sorted list of parameters and their names so we can
            // do fast look ups using binary search.
            //
            
            _sortedMethods = (JsonRpcMethod[]) _methods.Clone();
            Array.Sort(_methodNames, _sortedMethods, Comparer.DefaultInvariant);
        }
 void IServiceClassReflector.Build(JsonRpcServiceClassBuilder builder, Type type)
 {
     builder.Name = Name;
 }
 public void Init()
 {
     _classBuilder = new JsonRpcServiceClassBuilder();
     _builder = _classBuilder.DefineMethod();
 }
 internal JsonRpcMethodBuilder(JsonRpcServiceClassBuilder serviceClass)
 {
     Debug.Assert(serviceClass != null);
     _serviceClass = serviceClass;
 }
 public void Init()
 {
     _builder = new JsonRpcServiceClassBuilder();
 }
 void IServiceClassReflector.Build(JsonRpcServiceClassBuilder builder, Type type)
 {
     builder.Description = Text;
 }