// snippet shows how interfaces and coclasses can adorn the guid attribute.
// running the regasm will 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 system.guid can be constructed
// and how guid 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 be used to register this coclass in the registry.
// tlb file will be 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 an interface/coclass.
// value property 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 is 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 be 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
}
}
}
