T4 FieldName 采用驼峰命名法,不带下划线?

发布于 2024-09-04 08:18:51 字数 372 浏览 11 评论 0原文

我正在使用 T4 生成一些类定义,并发现我的字段名称前面有一个下划线。

我设置

code.CamelCaseFields = true;

只是为了安全(尽管我知道这是默认设置),但最终仍然是 _myField 而不是 myField。

如何生成不带“_”字符的字段名称?

另外,T4的文档在哪里?我找到了大量资源,例如

代码生成和文本模板以及大量博客,但我还没有找到逐个类、逐个属性的文档。

I'm using T4 to generate some class definitions and find that I'm getting an underscore in front of my field names.

I have set

code.CamelCaseFields = true;

just to be safe (even though I understand that's the default) but still end up with _myField rather than myField.

How can I generate a field name without the '_' character?

Also, where is the documentation for T4? I'm finding plenty of resources such as

Code Generation and Text Templates and numerous blogs, but I have not found the class-by-class, property-by-property documentation.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

过潦 2024-09-11 08:18:51

您可能正在谈论 EF4 自跟踪实体。 CodeGenerationTools 类通过 <#@ include file="EF.Utility.CS.ttinclude"#> 指令包含,您可以在“[VSInstallDir”中找到该指令] \ Common7 \ IDE \ Extensions \ Microsoft \ Entity Framework Tools \ Templates \ Includes \ EF.Utility.CS.ttinclude”。

FieldName 函数的定义如下:

private string FieldName(string name)
{
  if (CamelCaseFields)
  {
    return "_" + CamelCase(name);
  }
  else
  {
    return "_" + name;
  }
}

“_”在函数中被硬编码。自己编写代码应该不难。请注意,CodeGenerationTools 类特定于此 ttinclude 文件,并不是在 T4 中生成代码的通用嵌入式方式。

You're probably talking about EF4 Self Tracking Entities. The CodeGenerationTools class is included via the <#@ include file="EF.Utility.CS.ttinclude"#> directive, which you can find at "[VSInstallDir]\Common7\IDE\Extensions\Microsoft\Entity Framework Tools\Templates\Includes\EF.Utility.CS.ttinclude".

The FieldName function is defined as such:

private string FieldName(string name)
{
  if (CamelCaseFields)
  {
    return "_" + CamelCase(name);
  }
  else
  {
    return "_" + name;
  }
}

The "_" is hardcoded in the function. Coding your own shouldn't be difficult. Note that the CodeGenerationTools class is specific to this ttinclude file and isn't a generic and embedded way to generate code in T4.

我不咬妳我踢妳 2024-09-11 08:18:51

我编写了以下方法来使第一个字符大写,删除空格/下划线并使下一个字符大写。请参阅下面的示例。请随意使用。

private string CodeName(string name)
{
    name = name.ToLowerInvariant();

    string result = name;
    bool upperCase = false;

    result = string.Empty;
    for (int i = 0; i < name.Length; i++)
    {
        if (name[i] == ' ' || name[i] == '_')
        {
            upperCase = true;
        }
        else
        {
            if (i == 0 || upperCase)
            {
                result += name[i].ToString().ToUpperInvariant();
                upperCase = false;
            }
            else
            {
                result += name[i];
            }
        }
    }

    return result;
}

输入/输出样本:
名字 = 名字,
id = id,
状态消息 = StatusMessage

I've written the following method to make first character upper case, remove spaces/underscores and make next character upper case. See samples below. Feel free to use.

private string CodeName(string name)
{
    name = name.ToLowerInvariant();

    string result = name;
    bool upperCase = false;

    result = string.Empty;
    for (int i = 0; i < name.Length; i++)
    {
        if (name[i] == ' ' || name[i] == '_')
        {
            upperCase = true;
        }
        else
        {
            if (i == 0 || upperCase)
            {
                result += name[i].ToString().ToUpperInvariant();
                upperCase = false;
            }
            else
            {
                result += name[i];
            }
        }
    }

    return result;
}

input/output samples:
first_name = FirstName,
id = Id,
status message = StatusMessage

暖阳 2024-09-11 08:18:51

这是一个很好的建议,但它并不能帮助您了解放置此类函数的正确位置在哪里...

是否有关于分解 EF .tt 文件或逐步生成输出以查看它如何构建输出的指导?

通过将上面的函数插入一个名为的函数中,我能够成功地使用它
(Ef4.3)

public string Property(EdmProperty edmProperty)

似乎用于输出诸如“public int fieldname { get; set; }”之类的行,

并将第三个(索引 {2})参数更改为要包装的格式修改名称的函数,如下所示:

_typeMapper.GetTypeName(edmProperty.TypeUsage), //unchanged
UnderScoreToPascalCase(_code.Escape(edmProperty)), //wrapped "name"
_code.SpaceAfter(Accessibility.ForGetter(edmProperty)), // unchanged

这并不完美,例如:它不会保留现有的“Ucasing”并且不关心这样的事情:
客户IP
输出:客户
在我看来,它的可读性不是很好……

但它比我所看到的要好,这是一场噩梦,因为数据库混杂着驼峰命名法、帕斯卡命名法和下划线分隔符,非常可怕。

无论如何希望这对某人有帮助......

This is good advice however it doesn't help you in knowing WHERE the right place to put such a function is...

Is there any guidance on DECOMPOSING the EF .tt files or stepping through the output generation to see how it builds the output?

I was able to use the above function successfully by plugging it into a function called
(Ef4.3)

public string Property(EdmProperty edmProperty)

Which appears to be used to output the lines like "public int fieldname { get; set; }"

and changed the 3rd (index {2}) param to the formating to wrap with the function to modify the name, like this:

_typeMapper.GetTypeName(edmProperty.TypeUsage), //unchanged
UnderScoreToPascalCase(_code.Escape(edmProperty)), //wrapped "name"
_code.SpaceAfter(Accessibility.ForGetter(edmProperty)), // unchanged

This is not perfect, eg: it doesn't keep existing "Ucasing" and doesn't care about things like this:
customerIP
outputs: Customerip
which IMO is not very readable...

but its better than what I WAS looking at which was a nightmare because the database was intermingled mess of camelCase, PascalCase and underscore separation, so pretty horrific.

anyway hope this helps someone...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文