2008-11-28 79 views
6

我需要這樣的一些屬性類一個GUID屬性:如何將GUID作爲屬性參數?

public class SomeAttribute : Attribute { 
    private Guid foreignIdentificator; 
    public Guid ForeignIdentificator { 
     get { return this.foreignIdentificator; } 
     set { this.foreignIdentificator = value; } 
    } 
} 

但是在屬性定義我只能使用基本類型,這是常數(我明白爲什麼,它是讓我感覺)。解決方法:可以定義「ForeignIdentificator」字符串,並創建一個GUID運行:

public class SomeAttribute : Attribute { 
    private string foreignIdentificator; 
    public string ForeignIdentificator { 
     get { return this.foreignIdentificator; } 
     set { this.foreignIdentificator = value; } 
    } 
    public Guid ForeignIdentificatorGuid { 
     get { return new Guid(ForeignIdentificator); } 
    } 
} 

Unahppily我鬆散檢查類型安全。 「ForeignIdentificator」屬性可以包含任何字符串值,在創建期間Guid將在運行時拋出異常,而不是在編譯時。

我知道編譯器爲「Guid兼容性」檢查「System.Runtime.InteropServices.GuidAttribute」的字符串值。這個檢查正是我需要的,但是我不知道這個檢查是在編譯器中硬編碼還是我可以明確定義(以及如何)。

你是否知道某種方式,如何確保「Guid兼容性」檢查屬性?或者以另一種方式,如何在屬性中實現類型安全的Guid定義? 謝謝。

+0

什麼是「標識符」? – 2008-11-28 13:31:03

+0

「ForeignIdentificator」是在另一個程序集或數據庫或其他任何地方定義的GUID值,其中應該將屬性的所有者添加到其中。 – TcKs 2008-11-28 13:33:14

+0

這聽起來像是布什主義。 – xr280xr 2018-01-26 19:48:24

回答

6

我已經遇到過去的確切問題。我們只需要它們傳遞GUID作爲字符串... VS GUID生成器工具給我們的默認方式(* F9168C5E-CEB2-4faa-B6BF-329BF39FA1E4)。我們基本上做了你所做的。我們在插件體系結構中使用它,所以我們的客戶已經使用該接口。如果你看看當微軟需要做同樣的事情時他們做了什麼,他們會這麼做。

這樣做沒有問題。不是一次,我們是否認爲這是該領域的一個問題。

儘管如此,您可能想要命名該字符串字段的GUID,以免混淆消費者。添加一些文檔,以防他們不知道需要什麼格式。

當我看着這個時,我有同樣的反應......但後來我只是繼續前進,因爲它似乎沒有類型 - 安全的方案。

4

屬性參數必須是常量。如果我違反規則,我的C#編譯器會發出此錯誤:

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

由於在C#中沒有GUID文字,你有一個字符串編碼的GUID以另一種格式,例如。然而,你並不完全處於海上:你可以讓你的屬性擁有一個採用你想要的格式的ctor。這裏有一個與System.Guid相同的例子:

[AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)] 
sealed class MyGuidAttribute : Attribute 
{ 
    public Guid Guid { get; private set; } 

    // 
    // Summary: 
    //  Initializes a new instance of the System.Guid class using the specified array 
    //  of bytes. 
    // 
    // Parameters: 
    // b: 
    //  A 16 element byte array containing values with which to initialize the GUID. 
    // 
    // Exceptions: 
    // System.ArgumentNullException: 
    //  b is null. 
    // 
    // System.ArgumentException: 
    //  b is not 16 bytes long. 
    public MyGuidAttribute(byte[] b) 
    { 
     this.Guid = new Guid(b); 
    } 
    // 
    // Summary: 
    //  Initializes a new instance of the System.Guid class using the value represented 
    //  by the specified string. 
    // 
    // Parameters: 
    // g: 
    //  A System.String that contains a GUID in one of the following formats ('d' 
    //  represents a hexadecimal digit whose case is ignored): 32 contiguous digits: 
    //  dddddddddddddddddddddddddddddddd -or- Groups of 8, 4, 4, 4, and 12 digits 
    //  with hyphens between the groups. The entire GUID can optionally be enclosed 
    //  in matching braces or parentheses: dddddddd-dddd-dddd-dddd-dddddddddddd -or- 
    //  {dddddddd-dddd-dddd-dddd-dddddddddddd} -or- (dddddddd-dddd-dddd-dddd-dddddddddddd) 
    //  -or- Groups of 8, 4, and 4 digits, and a subset of eight groups of 2 digits, 
    //  with each group prefixed by "0x" or "0X", and separated by commas. The entire 
    //  GUID, as well as the subset, is enclosed in matching braces: {0xdddddddd, 
    //  0xdddd, 0xdddd,{0xdd,0xdd,0xdd,0xdd,0xdd,0xdd,0xdd,0xdd}} All braces, commas, 
    //  and "0x" prefixes are required. All embedded spaces are ignored. All leading 
    //  zeroes in a group are ignored. The digits shown in a group are the maximum 
    //  number of meaningful digits that can appear in that group. You can specify 
    //  from 1 to the number of digits shown for a group. The specified digits are 
    //  assumed to be the low order digits of the group. 
    // 
    // Exceptions: 
    // System.ArgumentNullException: 
    //  g is null. 
    // 
    // System.FormatException: 
    //  The format of g is invalid. 
    // 
    // System.OverflowException: 
    //  The format of g is invalid. 
    public MyGuidAttribute(string g) 
    { 
     this.Guid = new Guid(g); 
    } 
    // 
    // Summary: 
    //  Initializes a new instance of the System.Guid class using the specified integers 
    //  and byte array. 
    // 
    // Parameters: 
    // a: 
    //  The first 4 bytes of the GUID. 
    // 
    // b: 
    //  The next 2 bytes of the GUID. 
    // 
    // c: 
    //  The next 2 bytes of the GUID. 
    // 
    // d: 
    //  The remaining 8 bytes of the GUID. 
    // 
    // Exceptions: 
    // System.ArgumentNullException: 
    //  d is null. 
    // 
    // System.ArgumentException: 
    //  d is not 8 bytes long. 
    public MyGuidAttribute(int a, short b, short c, byte[] d) 
    { 
     this.Guid = new Guid(a, b, c, d); 
    } 
    // 
    // Summary: 
    //  Initializes a new instance of the System.Guid class using the specified integers 
    //  and bytes. 
    // 
    // Parameters: 
    // a: 
    //  The first 4 bytes of the GUID. 
    // 
    // b: 
    //  The next 2 bytes of the GUID. 
    // 
    // c: 
    //  The next 2 bytes of the GUID. 
    // 
    // d: 
    //  The next byte of the GUID. 
    // 
    // e: 
    //  The next byte of the GUID. 
    // 
    // f: 
    //  The next byte of the GUID. 
    // 
    // g: 
    //  The next byte of the GUID. 
    // 
    // h: 
    //  The next byte of the GUID. 
    // 
    // i: 
    //  The next byte of the GUID. 
    // 
    // j: 
    //  The next byte of the GUID. 
    // 
    // k: 
    //  The next byte of the GUID. 
    public MyGuidAttribute(int a, short b, short c, byte d, byte e, byte f, byte g, byte h, byte i, byte j, byte k) 
    { 
     this.Guid = new Guid(a, b, c, d, e, f, g, h, i, j, k); 
    } 
    // 
    // Summary: 
    //  Initializes a new instance of the System.Guid class using the specified unsigned 
    //  integers and bytes. 
    // 
    // Parameters: 
    // a: 
    //  The first 4 bytes of the GUID. 
    // 
    // b: 
    //  The next 2 bytes of the GUID. 
    // 
    // c: 
    //  The next 2 bytes of the GUID. 
    // 
    // d: 
    //  The next byte of the GUID. 
    // 
    // e: 
    //  The next byte of the GUID. 
    // 
    // f: 
    //  The next byte of the GUID. 
    // 
    // g: 
    //  The next byte of the GUID. 
    // 
    // h: 
    //  The next byte of the GUID. 
    // 
    // i: 
    //  The next byte of the GUID. 
    // 
    // j: 
    //  The next byte of the GUID. 
    // 
    // k: 
    //  The next byte of the GUID. 
    [CLSCompliant(false)] 
    public MyGuidAttribute(uint a, ushort b, ushort c, byte d, byte e, byte f, byte g, byte h, byte i, byte j, byte k) 
    { 
     this.Guid = new Guid(a, b, c, d, e, f, g, h, i, j, k); 
    } 
}