2012-08-08 2 views
0

필자는 필수 요소인지 또는 키인지 나타내는 데코 레이팅 된 속성을 가진 ConfigElement를 가지고 있습니다. 이 키를 만들고 싶습니다. 그러나 사용자가 고유 한 키를 사용하도록 의존 할 필요가 없도록 키를 생성하는 방법은 무엇입니까? 나는 Guid.NewGuid()을 퍼팅 시도하지만, 기본 값이 일정하다고가 없다는 오류를 준 :ConfigurationProperty 키의 ID를 생성 하시겠습니까?

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를 할당 해보십시오 :

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

이처럼 내 클래스는 모습입니다 클래스의 ctor에서.

그리고 ID 만 읽으면됩니다 (설정 안 함). 일반적으로 키를 수정 (생성 만 허용)하고 싶지는 않습니다.

관련 문제