打字机从IActionResult返回的ReturnType

3
我已经开始尝试使用Typewriter,看它是否适合生成模型和API层的要求。
目前为止,它可以用于生成模型,并且我已经成功生成了某种类型的API层。但是,在使用示例Angular Web API Service中的$ReturnType时遇到了问题。在示例代码中;
${
    using Typewriter.Extensions.WebApi;

    string ReturnType(Method m) => m.Type.Name == "IHttpActionResult" ? "void" : m.Type.Name;
    string ServiceName(Class c) => c.Name.Replace("Controller", "Service");
}
module App { $Classes(:ApiController)[

    export class $Name {

        constructor(private $http: ng.IHttpService) { 
        } $Methods[

        public $name = ($Parameters[$name: $Type][, ]) : ng.IHttpPromise<$ReturnType> => {

            return this.$http<$ReturnType>({
                url: `$Url`, 
                method: "$HttpMethod", 
                data: $RequestData
            });
        };]
    }

    angular.module("App").service("$ServiceName", ["$http", $Name]);]
}

这里使用了$ReturnType,但是当你调用一个 .net WebApi 控制器的方法时;

public async Task<IActionResult> DoSomething(){
    return Ok(MyModel);
}

“$ReturnType”是“IActionResult”,对我来说类型不够强大。我希望它是“MyModel”类型。
有什么方法可以在“Ok”中返回指定类型吗?我能否使用Type修饰该方法,以便Typewriter可以读取并使用?
2个回答

4
好的,因为这个问题没有得到答案,所以我将用我的当前解决方案回答它,希望它能对其他人有所帮助。
我创建了一个新属性;
public class ReturnTypeAttribute : Attribute
{     
    public ReturnTypeAttribute(Type t)
    {            
    }
}

然后我用它来装饰我的 API 方法;
[ReturnType(typeof(MyModel))]
public async Task<IActionResult> DoSomething(){
    return Ok(MyModel);
}

现在,在我的TypeWriter文件中,我有以下代码;

string ReturnType(Method m) {

    if (m.Type.Name == "IActionResult"){

        foreach (var a in m.Attributes){

            // Checks to see if there is an attribute to match returnType
            if (a.name == "returnType"){

                // a.Value will be in the basic format "typeof(Project.Namespace.Object)" or "typeof(System.Collections.Generic.List<Project.Namespace.Object>)
                // so we need to strip out all the unwanted info to get Object type
                string type = string.Empty;

                // check to see if it is an list, so we can append "[]" later
                bool isArray = a.Value.Contains("<");
                string formattedType = a.Value.Replace("<", "").Replace(">", "").Replace("typeof(", "").Replace(")", "");
                string[] ar;

                ar = formattedType.Split('.');
                type = ar[ar.Length - 1];

                if (isArray){
                    type += "[]";
                }

                // mismatch on bool vs boolean
                if (type == "bool"){
                    type = "boolean";
                }

                return type;
            }
        }

        return "void";
    }

    return m.Type.Name;
}

所以,正如你所看到的,我正在检查名为“returnType”的属性,然后获取该属性的值(它是一个字符串),删除一些格式以获取原始对象名称,然后返回该名称。到目前为止,对于我的需求来说,这个方案运行良好。如果有人能想出更好的解决方案,请告诉我!
这不是我所希望的最佳解决方案,因为如果你想在.ts文件中获得正确的类型,你需要确保有ReturnType(typeof(Object))

你能使用强类型的 public async Task<ActionResult<MyModel>> DoSomething() {} 吗?Typewriter 能否与 ActionResult 一起使用,而不是 IActionResult? - joedotnot

1
在我的情况下,我从方法的参数中获取返回类型:
// Turn IActionResult into void
string ReturnType(Method objMethod) 
{
    if(objMethod.Type.Name == "IActionResult")
    {
            if((objMethod.Parameters.Where(x => !x.Type.IsPrimitive).FirstOrDefault() != null))
            {
                return objMethod.Parameters.Where(x => !x.Type.IsPrimitive).FirstOrDefault().Name;
            }
            else
            {
                return "void";
            }
    } 
    else
    {
            return objMethod.Type.Name;
    }
}

请参见:

使用打字机加速您的Angular 4+开发


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接