为什么Enum.ToString不能作为属性参数字符串类型的const字符串?

时间:2016-12-03 12:52:23

标签: c# string enums const

[TL; DR] 为什么在C#中不允许const string SomeConstString = SomeEnum.OneEnumValue.ToString();,即使它可以在编译时处理?

以下属性定义来自Azure WebJobs SDK;

[AttributeUsage(AttributeTargets.Parameter)]
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public sealed class ServiceBusTriggerAttribute : Attribute
{
    private readonly string _queueName;
    private readonly string _topicName;
    private readonly string _subscriptionName;

    /// <summary>
    /// Initializes a new instance of the <see cref="ServiceBusTriggerAttribute"/> class.
    /// </summary>
    /// <param name="queueName">The name of the queue to which to bind.</param>
    public ServiceBusTriggerAttribute(string queueName)
    {
        ...
    }

代码的用法如下;

public static void GetBapulActivityFromTopic(
        [ServiceBusTrigger("SomeConstString")] BrokeredMessage message,
        TextWriter log)
    {
        ...
    }

在上面的用法中,我理解"SomeConstString"应该是const string类型,因为值必须是编译时常量。

但为什么SomeEnum.OneEnumValue.ToString()不能是编译时常量?

0 个答案:

没有答案