简体   繁体   English

从另一个类获取静态值

[英]Get static value from another class

I have a static string in some class: 我在某些班级有一个静态的字符串:

public static class SomeClass
{
    public static string MyString = "Hello World";
}

In T4 template file I want to invoke that static variable for conditional purposes in order to generate SomeClass2. 在T4模板文件中,我想出于条件目的调用该静态变量,以便生成SomeClass2。 I thought it would be something like this: 我以为会是这样的:

<# SomeClass.MyString #>

I tried multiple approaches, but nothing seemed to work. 我尝试了多种方法,但似乎没有任何效果。 I am getting error SomeClass.MyString does not exists. 我收到错误SomeClass.MyString不存在。 How can that be achieved? 如何实现?

As Daniel spotted, you are probably trying to self reference from the T4 template the same project you never built, so SomeClass doesn't exist in the (non existent?) assembly (if you reference it) 正如Daniel所发现的,您可能试图从T4模板自我引用从未构建过的项目,因此SomeClass在(不存在?)程序集中不存在(如果您引用它)

You could comment out you T4 code Then build the project , then you can reference it 您可以注释掉您的T4代码,然后构建项目,然后可以引用它

Or alternatively put your "Helpers" in a different project/assembly that you can reference from from the T4-Project using 'T4 Assembly Directive' 或者,也可以将“帮助程序”放在其他项目/程序集中,您可以使用“ T4程序集指令”从T4-Project引用该项目/程序集

Or Put your helpers inside the T4 Template 或将您的助手放入T4模板中

public static class FooOrBar
{
public static string What { get { return  " <#= MyHelpers.GetString(true) #> "; } }
}
<#+
public static class MyHelpers {
    public static string GetString(bool what)
    {
        return what ? "foo" : "bar";
    }
}
#>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM