c# api 动态创建的方法

c# api methods created dynamically

正如标题所说,是否可以动态创建一个api?我做了一些研究,但找不到任何方法来做到这一点。我有一种方法可以将列拉到 table 中。我想从这些信息中创建所有的 CRUD 操作。然后可以通过 API 调用这些操作。如果我不清楚或需要 post 其他任何事情,请告诉我。

这是使用 T4 模板生成代码的一种可能解决方案。 Visual Studio 开箱即用 T4 模板。有关详细信息,请查看 documentation 关于使用 T4 文本模板生成代码。

对于示例,我创建了一个包含文件 Common.t4,它具有检索模型信息的功能。如果您已经有这样做的库,则可以直接在模板中导入程序集,也不需要通用代码文件。为了简单起见,我 return 函数中只有静态数据,然后您必须通过调用获取数据的方法来实现它。

示例通用文件:

<#@ import namespace="System" #>
<#+
public string[] GetEntities()
{
    // TODO: implement logic to get entitities
    return new string[] { "Entity01", "Entity02", "Entity03" };
}
public class FieldDefinition
{
    public string Name { get; set;}
    public Type Type { get; set; }
}
public FieldDefinition[] GetEntityFields(string entityName)
{
    // TODO: Implement retrieval of Entity fields
    return new FieldDefinition[] 
    { 
        new FieldDefinition() { Name = "Id", Type = typeof(int) },
        new FieldDefinition() { Name = "Name", Type = typeof(string) }
    };
}
#>

有了这个通用功能后,您可以为模型创建一个 T4 模板文件,为控制器创建一个。

这是一个示例模型模板:

<#@ template hostspecific="false" language="C#" #>
<#@ output extension=".cs" #>
<#@ include file="Common.t4" #>


using System;
using System.Collections.Generic;
using System.Linq;

namespace WebApplication3.Models
{
<#
foreach (string entity in GetEntities())
{
#>
    public class <#=entity#>Model
    {
<#
    foreach (FieldDefinition fieldDefinition in GetEntityFields(entity))
    {
#>
        public <#= fieldDefinition.Type.FullName#> <#= fieldDefinition.Name#> { get; set; }
<#
    }
#>
    }

<#
}
#>
}

最后是控制器模板:

<#@ template hostspecific="false" language="C#" #>
<#@ output extension=".cs" #>
<#@ include file="Common.t4" #>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using WebApplication3.Models;

namespace WebApplication3.Controllers
{
<#
foreach (string entity in GetEntities())
{
#>
    [Produces("application/json")]
    [Route("api/<#=entity#>")]
    public class <#=entity#>Controller : Controller
    {
        // GET: api/<#=entity#>
        [HttpGet]
        public IEnumerable<<#=entity#>Model> Get()
        {
            return new <#=entity#>Model[] { new <#=entity#>Model(), new <#=entity#>Model() };
        }

        // GET: api/<#=entity#>/5
        [HttpGet("{id}", Name = "Get")]
        public <#=entity#>Model Get(int id)
        {
            return new <#=entity#>Model();
        }

        // POST: api/<#=entity#>
        [HttpPost]
        public void Post([FromBody]<#=entity#>Model value)
        {
        }

        // PUT: api/<#=entity#>/5
        [HttpPut("{id}")]
        public void Put(int id, [FromBody]<#=entity#>Model value)
        {
        }

        // DELETE: api/<#=entity#>/5
        [HttpDelete("{id}")]
        public void Delete(int id)
        {
        }
    }

<#
}
#>
}

还有许多其他商业和免费代码生成工具可用。检查此 link:Comparison of code generation tools。如果您不喜欢它或缺少某些功能,它还提供了 T4 的替代品。

最后我还提到了可用于在运行时生成完全动态代码的技术。对于这种情况,您可以使用 Roslyn compiler, CodeDOM or Reflection.Emit.