扩展方法作为静态类方法
可能的重复:
您可以添加像静态一样调用的扩展方法吗方法?
我想在 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;
}
}
}
但我不能让它工作,它只适用于实例变量,知道如何将其作为静态方法添加到扩展类中吗?
Possible Duplicate:
Can you add extension methods that you call like static methods?
I would like to add NewSequentialGuid
function on the Guid
system type, so I can use like following:
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;
}
}
}
But I cannot get this to work, it only works with instance variables, any idea how to add this to extended class as a static method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你不能。
它们被创建为看起来像实例方法,并且不能用作类(静态)方法。
来自 MSDN:
和:
You can't.
They were created to look like instance methods and can't be make to work as class (static) methods.
From MSDN:
And:
您可以添加像静态一样调用的扩展方法吗方法?
Can you add extension methods that you call like static methods?