You may have noticed that every C# type has a "GUID" property of type Guid. If you don't explicitly specify, then this GUID is determined for you automatically. However, you can explicitly specify this GUID at compile time. We can thank COM interoperability for this ability, as setting an Interface's ID is essential, and so we can get this for our basic types as well. I personally like to use this ability for plugin object registration.
A couple "Using" statements for our example:
using System.Runtime.InteropServices;
using System.Diagnostics;
A dummy type with an explicit Guid using System.Runtime.InteropServices.GuidAttribute:
[Guid("7CB9C1E2-9BA4-4647-9202-FCFEF063552A")]
public class ExplicitTypeID
{
public void CheckTypeID()
{
Debug.Assert
(
this.GetType().GUID ==
new Guid("7CB9C1E2-9BA4-4647-9202-FCFEF063552A")
);
}
}
0 comments:
Post a Comment