[英]Is it possible to use the conditional attribute to create similliar debugger and run-time method?
is it possible to create 2 methods that have the same method-name, passed in values and returns using the conditional attribute and some anti conditional for example 是否有可能创建2个具有相同方法名称的方法,传入值并使用条件属性返回并返回一些反条件
[Conditional("Debug")]
private string StringGenerator()
{
Guid g = Guid.NewGuid();
string guidString = Convert.ToBase64String(g.ToByteArray());
guidString = guidString.Replace("=", "");
guidString = guidString.Replace("+", "");
return guidString;
}
[!Conditional("Debug")]// I know using '!' doesn't really work
private string StringGenerator()
{
Guid g = Guid.NewGuid();
string guidString = Convert.ToBase64String(g.ToByteArray());
return guidString;
}
so that you could just call a method and based on whether "Debug" is defined the compiler will choose which method? 这样你就可以调用一个方法并根据是否定义了“Debug”,编译器会选择哪种方法?
Thanks 谢谢
No this is not possible. 不,这是不可能的。 The
Conditional
attribute doesn't control whether or not a method is defined in code, it just controls the conditions under which the method call is included in the calling code. Conditional
属性不控制是否在代码中定义了方法,它只控制方法调用包含在调用代码中的条件。
Additionally it's possible for the Conditional
attribute to be applied multiple times to a method. 此外,可以将
Conditional
属性多次应用于方法。 Hence there isn't a simply on / off
decision to be made here. 因此,这里没有简单的
on / off
决定。 Consider 考虑
[Conditional("DEBUG")]
[Conditional("TRACE")]
void Target() { ... }
There are 4 combinations here to consider, not just 2. 这里有4种组合需要考虑,而不仅仅是2种。
You can try using the #if DEBUG
syntax like so: 您可以尝试使用
#if DEBUG
语法,如下所示:
#if DEBUG
private string StringGenerator()
{
Guid g = Guid.NewGuid();
string guidString = Convert.ToBase64String(g.ToByteArray());
guidString = guidString.Replace("=", "");
guidString = guidString.Replace("+", "");
return guidString;
}
#else
private string StringGenerator()
{
Guid g = Guid.NewGuid();
string guidString = Convert.ToBase64String(g.ToByteArray());
return guidString;
}
#endif
However, there are some differences with using this. 但是,使用它有一些不同之处。 See this question for more details.
有关详细信息,请参阅此问题 。
You can't have multiple functions/methods with the same name and the same signature. 您不能拥有具有相同名称和相同签名的多个函数/方法。 You can overload a method by changing its signature, though.
但是,您可以通过更改其签名来重载方法。 For example:
例如:
Private Function myMethod() As String
Return "D"
End Function
Private Function myMethod(ByVal myString As String) As String
Return "D"
End Function
Instead trying to create the same exact function, that does two different things, it might be better to pass in a variable and, depending of the value, do two different things. 相反,尝试创建相同的功能,执行两个不同的操作,传递变量可能更好,并且根据值,执行两个不同的操作。
Private Function myFunction(ByVal test As String) As String
if(test.toUpper()= "QA") then
'do one thing
elseif(test.toUpper() = "LOCAL"
'do another
else
'must be Prod
end if
end Sub
Normally what I do is have a key in my web.config file that denotes what Environment i'm using (Local, QA, Prod) and depending on that value, i'd pass a different parameter into my function/method. 通常我所做的是在我的web.config文件中有一个键,表示我正在使用的环境(Local,QA,Prod),并且根据该值,我会将不同的参数传递给我的函数/方法。 Then, when I publish, i change the web config key
然后,当我发布时,我更改了Web配置密钥
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.