Skip to main content
 首页 » 编程设计

c#之扩展方法作为静态类方法

2024年02月23日19kenshinobiy

<分区>

Possible Duplicate:
Can you add extension methods that you call like static methods?

我想在 Guid 系统类型上添加 NewSequentialGuid 函数,这样我就可以像下面这样使用:

Id = Guid.NewSequentialGuid()

namespace MyExtensions 
{ 
    public static class GuidExtensions 
    { 
        [DllImport("rpcrt4.dll", SetLastError = true)] 
        static extern int UuidCreateSequential(out Guid guid); 
 
        public static Guid NewSequentialGuid(this Guid guid) 
        { 
            const int RPC_S_OK = 0; 
            Guid g; 
            int hr = UuidCreateSequential(out g); 
            if (hr != RPC_S_OK) 
                throw new ApplicationException 
                  ("UuidCreateSequential failed: " + hr); 
            return g; 
        } 
    } 
} 

但我无法让它工作,它只适用于实例变量,知道如何将它作为静态方法添加到扩展类吗?