using System; using System.Runtime.InteropServices; namespace Example { class Program { static void Main(string[] args) { Guid guid = Marshal.GenerateGuidForType(typeof(Program)); Console.WriteLine(guid); } } }
using System; using System.Runtime.InteropServices; namespace Example { [Guid("C0697E41-6DC5-4DFC-AFA1-B2D9DC1686B8")] class MyClass { static void Main(string[] args) { Guid guid = Marshal.GenerateGuidForType(typeof(MyClass)); Console.WriteLine(guid); } } }This example generates a GUID for the MyClass class using Marshal.GenerateGuidForType and outputs it to the console. The class also has a predefined GUID using the Guid attribute. The Marshal.GenerateGuidForType method is part of the System.Runtime.InteropServices namespace.