gpt4 book ai didi

c# - 在 t4 模板中检测类继承

转载 作者:太空宇宙 更新时间:2023-11-03 12:15:22 24 4
gpt4 key购买 nike

你能在 t4 模板中检测类的继承属性吗?

我有两个这样的类:

public class User : ManagedRecord
{
public int UserId { get; set;}
public string UserName { get; set;}
}

public abstract class ManagedRecord
{
public int Deleted { get; set;}
public DateTime CreatedDate { get; set;}
}

当 t4 模板创建基于 User 的字段时,它将首先放置 ManagedRecord 字段,然后是 User 的字段。

有没有办法检测继承的类字段,以便我可以以不同的方式处理它们?

t4 模板用于 ASP.NET MVC 5 编辑页面模板,它根据使用的类创建表单字段。

t4 代码如下所示:

<#@ template language="C#" HostSpecific="True" #>
<#@ output extension=".cshtml" #>
<#@ include file="Imports.include.t4" #>
@model <#= ViewDataTypeName #>
<#
string boolType = "System.Boolean";
int tabIndex = 1;
bool autofocus = true;
Version requiredMvcVersion = new Version("5.1.0.0");
bool isControlHtmlAttributesSupported = MvcVersion >= requiredMvcVersion;


foreach (PropertyMetadata property in ModelMetadata.Properties) {
if (property.Scaffold && !property.IsAssociation) {
#>
@Html.EditorFor(model => model.<#= property.PropertyName #>)
<#
}
#>

最佳答案

我找到了适合我的场景的解决方案。您可以修饰属性以表明您不想搭建列的脚手架。

using System.ComponentModel.DataAnnotations;
namespace YourNameSpace
{
public abstract class ManagedRecord
{
[ScaffoldColumn(false)]
public DateTime CreatedDate{ get; set; }

[ScaffoldColumn(false)]
public bool Deleted { get; set; }
}
}

关于c# - 在 t4 模板中检测类继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50067171/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com