gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 22:17:30 24 4
gpt4 key购买 nike

<分区>

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;
}
}
}

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com