I have a ConfigElement with decorated property indicating if it is required or a key. I would like to make this a key, but how do I generate a key so I don’t have to depend on the user to come up a unique key? I tried putting Guid.NewGuid(), but it gave an error saying the default value has to be a constant:
An attribute argument must be a constant expression,
typeof expression or array creation expression of an attribute parameter type
Here is what my class looks like:
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;
}
}
How about setting it in your constructor: