2012-08-08 124 views
0

我有一个装饰属性ConfigElement指示是否需要或一个关键。我想将其作为关键,但是如何生成密钥以便我不必依靠用户来创建唯一密钥?我试图把Guid.NewGuid(),但它给了一个错误说的默认值必须是一个常数:为ConfigurationProperty密钥生成ID?

An attribute argument must be a constant expression, 
typeof expression or array creation expression of an attribute parameter type 

这里是我的课是什么样子:

public class MyEntryElement : ConfigElement 
{ 
    #region Constructor 

    public MyEntryElement(ConfigElement parent) 
     : base(parent) 
    { 
    } 

    #endregion 

    #region Properties 

    [ConfigurationProperty("id", DefaultValue = Guid.NewGuid(), IsRequired = true, IsKey = true)] 
    [ObjectInfo(Title = "ID", Description = "Defines the id of the entry.")] 
    public string ID 
    { 
     get 
     { 
      return (string)this["id"]; 
     } 
     set 
     { 
      this["id"] = value; 
     } 
    } 

    [ConfigurationProperty("note", DefaultValue = "", IsRequired = true)] 
    [ObjectInfo(Title = "Note", Description = "Defines the note of the entry.")] 
    public string Note 
    { 
     get 
     { 
      return (string)this["note"]; 
     } 
     set 
     { 
      this["note"] = value; 
     } 
    } 

回答

0

如何在构造函数设置它:

public MyEntryElement(ConfigElement parent) 
     : base(parent) 
    { 
     if (String.IsNullOrEmpty(ID)) 
     { 
      ID = Guid.NewGuid().ToString(); 
     } 
    } 
0

尝试分配ID在班级的课程中。

而且我会让ID只读(没有设置)。通常不希望允许修改密钥(仅创建)。