如何在使用Visual Studio的C#项目模板中将参数值替换为小写

huangapple go评论72阅读模式
英文:

How to replace a parameter value with lowercase in a C# project template using Visual Studio

问题

我试图创建一个C#项目模板,并在模板配置中有一个名为"MyClass"的输入参数。我希望在模板中的"MyClass"被替换后,能够将其实例更改为小写,这是我想生成所有类的示例类模板:

    //This is myClass <- 将此更改为小写 (myClass)
    public class MyClass <- 保持不变 (MyClass)
    {
    }

如果我从模板生成一个名为"School"的类,我希望输出如下:

    //This is school <- 此替换已更改为全小写
    public class School <- 此替换未更改
    {
    }

以下是我的模板的当前配置:

{
  "name": "Test",
  "shortName": "test",
  "preferNameDirectory": true,
  "tags": {
    "language": "C#",
    "type": "item"
  },
  "symbols": {
    "MyClass": {
      "type": "parameter",
      "datatype": "text",
      "description": "Renames files containing 'MyClass'",
      "fileRename": "MyClass",
      "replaces": "MyClass",
      "isRequired": true,
      "defaultValue": "MyClass"
    }
  }
}

注意:模板文件的构建操作已设置为内容,无法进行编译,因此无法执行ToLower()等操作。

英文:

I am trying to create a project template for C# and I have an input parameter named "MyClass" as shown in the template configuration below. I want to be able to change the instances of "MyClass" to lowercase if I want after it has been replaced in the template.

This is a sample class template that I want to generate all classes from:

//This is myClass &lt;- Change this to lowercase (myClass)
public class MyClass &lt;- Leave this as it is (MyClass)
{
}

If I generate a class named "School" from the template, I want the output to be like this:

//This is school &lt;- This replacement was changed to all lowercase
public class School &lt;- This replacement was left unchanged
{
}

Here's the current configuration of my template:

{
  &quot;name&quot;: &quot;Test&quot;,
  &quot;shortName&quot;: &quot;test&quot;,
  &quot;preferNameDirectory&quot;: true,
  &quot;tags&quot;: {
    &quot;language&quot;: &quot;C#&quot;,
    &quot;type&quot;: &quot;item&quot;
  },
  &quot;symbols&quot;: {
    &quot;MyClass&quot;: {
      &quot;type&quot;: &quot;parameter&quot;,
      &quot;datatype&quot;: &quot;text&quot;,
      &quot;description&quot;: &quot;Renames files containing &#39;MyClass&#39;&quot;,
      &quot;fileRename&quot;: &quot;MyClass&quot;,
      &quot;replaces&quot;: &quot;MyClass&quot;,
      &quot;isRequired&quot;: true,
      &quot;defaultValue&quot;: &quot;MyClass&quot;
    }
  }
}

NOTE: The build action of the template file has been set to content and cannot be compiled so I cannot do things like ToLower().

答案1

得分: 1

I don't understand your Note:, do you mean you can't modify this template? So how can you get what you need.

You should modify to add valueTransform attribute, this should solve your problem.

"valueTransform": "firtstLowerCase"

英文:

I don't understand your Note:, do you mean you can't modify this template? So how can you get what you need.

You should modify to add valueTransform attribute, this should solve your problem.

"valueTransform": "firtstLowerCase"

huangapple
  • 本文由 发表于 2023年4月6日 20:05:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75949328.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定