Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 属性参数必须是常量值-XmlRoot命名空间_C#_.net_Attributes - Fatal编程技术网

C# 属性参数必须是常量值-XmlRoot命名空间

C# 属性参数必须是常量值-XmlRoot命名空间,c#,.net,attributes,C#,.net,Attributes,我有下面的代码,它使用一个struct声明一个const值作为XmlRoot属性的namespacce,因为我们都知道属性只能有const值 public struct Declarations { public const string SchemaVersion = "http://localhost:4304/XMLSchemas/Request.xsd"; } [XmlRoot(ElementName = "Header", Namespace = Declarations.Sc

我有下面的代码,它使用一个struct声明一个const值作为XmlRoot属性的namespacce,因为我们都知道属性只能有const值

public struct Declarations
{
    public const string SchemaVersion = "http://localhost:4304/XMLSchemas/Request.xsd";
}
[XmlRoot(ElementName = "Header", Namespace = Declarations.SchemaVersion, IsNullable = false), Serializable]
public class RequestHeader
{
    ...
这显然会引发错误“属性参数必须是常量值”。我的问题是,是否有任何方法可以使用web.config中指定的值,从而使命名空间对于我拥有的所有不同环境(DEV、STE、UAT等)都不同


感谢高级版。

否,常量值必须在编译时出现。这意味着配置文件值永远不能是代码中常量的有效候选值

您可以结合
DEV
STE
UAT
符号执行类似的操作(丑陋,是的,但它可以工作):


不,常量值必须在编译时出现。这意味着配置文件值永远不能是代码中常量的有效候选值

您可以结合
DEV
STE
UAT
符号执行类似的操作(丑陋,是的,但它可以工作):


谢谢安德鲁。您可能的解决方案是有意义的,尽管我不确定#if语句,但我以前从未使用过类似的语句,它们到底是什么,以及如何将其与环境联系起来?我的意思是,我在哪里把开发人员映射到我的开发环境?谢谢Andrew。您可能的解决方案是有意义的,尽管我不确定#if语句,但我以前从未使用过类似的语句,它们到底是什么,以及如何将其与环境联系起来?我的意思是,我应该在哪里将DEV映射到我的开发环境?
public struct Declarations
{
    public const string SchemaVersion_DEV
        = "http://localhost:4304/XMLSchemas/Request.xsd";
    public const string SchemaVersion_STE
        = "http://someotherserver/XMLSchemas/Request.xsd";
    public const string SchemaVersion_UAT
        = "http://anotherserver/XMLSchemas/Request.xsd";
}

#if DEV
[XmlRoot(ElementName = "Header", Namespace = Declarations.SchemaVersion_DEV, IsNullable = false), Serializable]
#elif STE
[XmlRoot(ElementName = "Header", Namespace = Declarations.SchemaVersion_STE, IsNullable = false), Serializable]
#elif UAT
[XmlRoot(ElementName = "Header", Namespace = Declarations.SchemaVersion_UAT, IsNullable = false), Serializable]
#endif
public class RequestHeader { }