Program of Foreigner (v.)--How to use global identifiers (GUIDs)

Source: Internet
Author: User
Tags constructor
Guid| Program//snippet shows how interfaces and coclasses can adorn the Guid attribute.
Running the regasm would generate. reg and. tlb files. Reg file can be
Used to register the interface and coclass with the registry. TLB file
are used to do interop. Also illustrates how to System.Guid can be constructed
And how GUIDs objects can be compared.

Namespace Guidsnippet
{
<Snippet1>
Using System;
Using System.Runtime.InteropServices;

Guid for the interface IFoo.
[Guid ("F9168c5e-ceb2-4faa-b6bf-329bf39fa1e4")]
Interface IFoo
{
void Foo ();
}

Guid for the coclass Cfoo.
[Guid ("936da01f-9abd-4d9d-80c7-02af85c822a8")]
public class Cfoo:ifoo
{
Run RegAsm on this assembly to create. reg and. tlb files.
Reg file can is used to register this coclass in the registry.
TLB file is used to do interop.

public void Foo () {}

public static void Main (string []args)
{
Snippet addresses the following in System.Runtime.InterOpServices.GuidAttribute.
How to specify the attribute on Interface/coclass.
Retrieve the GuidAttribute from Interface/coclass.
Value on GuidAttribute class.

Snippet addresses the following in System.Guid.
Constructor Guid (String).
Constructor Guid (ByteArray).
Equals.
Operator = =.
CompareTo.

Attribute Ifooattribute = Attribute.GetCustomAttribute (typeof (IFoo), typeof (GuidAttribute));

The Value property of GuidAttribute returns a string.
System.Console.WriteLine ("IFoo Attribute:" + (GuidAttribute) ifooattribute). Value);

Using the string to create a GUID.
GUID guidFoo1 = new GUID ((GuidAttribute) ifooattribute). Value);
Using a byte array to create a GUID.
GUID GuidFoo2 = new GUID (Guidfoo1.tobytearray ());

Equals is overriden and so value comparison are done though references are different.
if (Guidfoo1.equals (GUIDFOO2))
System.Console.WriteLine ("GuidFoo1 equals GuidFoo2");
Else
System.Console.WriteLine ("GuidFoo1 not equals GuidFoo2");

Equality operator can also is used to determine if two GUIDs have same value.
if (guidFoo1 = = GuidFoo2)
System.Console.WriteLine ("guidFoo1 = = GuidFoo2");
Else
System.Console.WriteLine ("GuidFoo1!= GuidFoo2");

CompareTo returns 0 if the GUIDs have same value.
if (Guidfoo1.compareto (guidFoo2) = = 0)
System.Console.WriteLine ("GuidFoo1 compares to GuidFoo2");
Else
System.Console.WriteLine ("GuidFoo1 does not compare to GuidFoo2");

System.Console.ReadLine ();

Output.
IFoo attribute:f9168c5e-ceb2-4faa-b6bf-329bf39fa1e4
GuidFoo1 equals GuidFoo2
GuidFoo1 = = GuidFoo2
GuidFoo1 compares to GuidFoo2
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.