コード例 #1
0
ファイル: ServiceExtensions.cs プロジェクト: jarlef/Bifrost
        public static Container WithServiceMethodsFrom(this FunctionBody functionBody, Type type)
        {
            var objectMethods = typeof(object).GetMethods();
            var methods = type.GetMethods(BindingFlags.Public|BindingFlags.Instance).Where(m => objectMethods.Any(om=>om.DeclaringType != m.DeclaringType));

            foreach (var method in methods)
            {
                var functionName = method.Name.ToCamelCase();

                var selfScopeCall = new Scope("self");
                
                var parameters = method.GetParameters().Select(p => p.Name.ToCamelCase()).ToArray();
                var objectLiteral = new ObjectLiteral();
                foreach (var parameter in parameters) 
                    objectLiteral.Assign(parameter).WithLiteral(parameter);


                if (method.ReturnType == typeof(void))
                    selfScopeCall.FunctionCall(f => f.WithName("callWithoutReturnValue").WithParameters(new Literal("\"" + method.Name + "\""), objectLiteral));
                else if (method.ReturnType.IsDictionary() || !method.ReturnType.IsEnumerable())
                    selfScopeCall.FunctionCall(f => f.WithName("callWithObjectAsReturn").WithParameters(new Literal("\"" + method.Name + "\""), objectLiteral));
                else if (method.ReturnType.IsEnumerable())
                    selfScopeCall.FunctionCall(f => f.WithName("callWithArrayAsReturn").WithParameters(new Literal("\"" + method.Name + "\""), objectLiteral));

                functionBody.Property(functionName, p =>
                {
                    p.WithFunction(function =>
                        function
                            .WithParameters(parameters)
                                .Body
                                    .Return(selfScopeCall)
                    );
                });
            }

            return functionBody;
        }
コード例 #2
0
ファイル: FunctionExtensions.cs プロジェクト: JoB70/Bifrost
 /// <summary>
 /// Add a scope - such as "self", typically used together with an <see cref="Assignment"/>
 /// </summary>
 /// <param name="functionBody"><see cref="FunctionBody"/> to add to</param>
 /// <param name="name">Name of the scope, e.g. "self"</param>
 /// <param name="callback"><see cref="Action{Scope}"/> that gets called for working with the <see cref="Scope"/></param>
 /// <returns>Chained <see cref="FunctionBody"/> to keep building on</returns>
 public static FunctionBody Scope(this FunctionBody functionBody, string name, Action<Scope> callback)
 {
     var scope = new Scope(name);
     functionBody.AddChild(scope);
     callback(scope);
     return functionBody;
 }