2008-11-28 9 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 मैं ढीला प्रकार की सुरक्षा के लिए जाँच करें। "विदेशी पहचानकर्ता" संपत्ति में कोई स्ट्रिंग मान हो सकता है और ग्रिड बनाने के दौरान रनटाइम पर अपवाद फेंक दिया जाएगा, संकलन समय पर नहीं।

मुझे "ग्रिड संगतता" के लिए "System.Runtime.InteropServices.GuidAttribute" के लिए कंपाइलर चेक स्ट्रिंग मान पता है। यह चेक बिल्कुल वही है जो मुझे चाहिए, लेकिन मुझे नहीं पता कि यह जांच मैंने संकलक में हार्डकोड किया है या क्या मैं स्पष्ट रूप से परिभाषित (और कैसे) कर सकता हूं।

क्या आप कुछ तरीकों से जानते हैं, "Guid compatibilty" गुणों को कैसे सुरक्षित करें? या कुछ और तरीका, गुणों में सुरक्षित ग्वि परिभाषा प्रकार कैसे पहुंचे? धन्यवाद।

+0

"पहचानकर्ता" क्या है, के साथ एक ही उदाहरण के साथ एक उदाहरण है। –

+0

"विदेशी पहचानकर्ता" GUID मान है जिसे किसी अन्य असेंबली या डेटाबेस या अन्य किसी भी रूप में परिभाषित किया गया है, जहां विशेषता के स्वामी को जोड़ा जाना चाहिए। – TcKs

+0

यह बुशवाद की तरह लगता है। – xr280xr

उत्तर

6

मैंने अतीत में आपकी सटीक समस्या में भाग लिया है। हमें बस उन्हें एक स्ट्रिंग के रूप में GUID में पास करने की आवश्यकता थी ... डिफ़ॉल्ट तरीका है कि वीएस GUID जेनरेटर टूल हमें यह देता है (* F9168C5E-CEB2-4faa-B6BF-329BF39FA1E4)। हमने अनिवार्य रूप से किया जो आपने किया था। हम इसे प्लग-इन आर्किटेक्चर में उपयोग करते हैं, इसलिए हमारे ग्राहक इंटरफ़ेस का उपयोग करने वाले हैं। यदि आप देखते हैं कि माइक्रोसॉफ्ट क्या करता है जब उन्हें एक ही काम करने की ज़रूरत होती है, तो वे ऐसा करते हैं।

इस तरह से ऐसा करने में कोई समस्या नहीं हुई है। एक बार नहीं, क्या हमने इसे मैदान से एक मुद्दा के रूप में देखा है।

हालांकि, आप अपने उपभोक्ताओं को भ्रमित नहीं करने के लिए उस स्ट्रिंग फ़ील्ड GUID को नाम देना चाहेंगे। कुछ दस्तावेज जोड़ें यदि उन्हें पता नहीं है कि उन्हें किस प्रारूप में होना चाहिए।

जब मैंने इसे देखा तो मुझे वही प्रतिक्रिया मिली ... लेकिन फिर मैं बस आगे बढ़ गया, ऐसा लगता है कि ऐसा कोई प्रकार नहीं है- सुरक्षित समाधान

+0

नया ग्रिड लिखना मेरे लिए समस्या नहीं है (मेरे पास इसके लिए एक महत्वपूर्ण शॉर्टकट है), लेकिन मुझे अन्य लोगों के बारे में डर है। लेकिन यदि कोई रास्ता नहीं है, तो संकलन समय पर सही प्रारूप को सुरक्षित करने के लिए कैसे करें (और मुझे डर है कि वहां नहीं है), मुझे उपयोगिता बनाना होगा, जो संकलित असेंबली में सभी विशेषता परिभाषाओं का परीक्षण करेगा। – TcKs

+0

मैं आपकी चिंता को समझता हूं। हमने इसे समझने की कोशिश करने में कुछ समय बिताया। माइक्रोसॉफ्ट के कार्यान्वयन में, वे वही काम करते हैं, इसलिए हमने छोड़ दिया। फिर, हमें डर था कि यह क्षेत्र में एक समस्या होगी, लेकिन यह एक बार समस्या के रूप में नहीं आया है। बस अपने अंत में अपवाद पकड़ने के लिए सुनिश्चित करें। –

+0

आप मेरा अनुमान लगाते हैं। मैं और समाधान के लिए और अधिक खोज नहीं करूंगा। धन्यवाद। – TcKs

4

विशेषता पैरामीटर स्थिर होना चाहिए। अगर मैं नियमों को तोड़ने, मेरे सी # संकलक इस त्रुटि देता है:

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

चूंकि सी # में कोई GUID शाब्दिक हैं, तो आप एक स्ट्रिंग किसी अन्य स्वरूप में GUID एन्कोड करने के लिए, उदाहरण के लिए की है। हालांकि, आप पूरी तरह समुद्र में नहीं हैं: आप अपनी विशेषता को एक सीटीआर बना सकते हैं जो आपके इच्छित प्रारूप को लेता है। यहां 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); 
    } 
} 
+0

आप सही हैं। मैं विशेषता के लिए एक guid पास करने के लिए स्ट्रिंग का उपयोग करें। – IlPADlI

संबंधित मुद्दे

 संबंधित मुद्दे