如何移植使用VB.NET回调的C#代码?

时间:2010-03-25 07:40:30

标签: c# vb.net

我需要将以下内容从ASP.NET MVC 2 sourcecode从C#移植到VB.NET。它来自 AuthorizeAttribute.cs ,从第86行开始:

HttpCachePolicyBase cachePolicy = filterContext.HttpContext.Response.Cache;
cachePolicy.SetProxyMaxAge(new TimeSpan(0));
cachePolicy.AddValidationCallback(CacheValidateHandler, null /* data */);

其中 CacheValidateHandler 是:

private void CacheValidateHandler(HttpContext context, object data, 
                                  ref HttpValidationStatus validationStatus) {
     validationStatus = OnCacheAuthorization(new HttpContextWrapper(context));
}

来自http://converter.telerik.com的VB.NET端口对此行不起作用:

cachePolicy.AddValidationCallback(CacheValidateHandler, Nothing) ' Error

其中 CacheValidateHandler 是:

Private Sub CacheValidateHandler(ByVal context As HttpContext, ByVal data As Object, _
                                 ByRef validationStatus As HttpValidationStatus)
    validationStatus = OnCacheAuthorization(New HttpContextWrapper(context))
End Sub

VS2008抱怨 CacheValidateHandler 没有为上下文数据 validationStatus 指定其参数。

如何移植此代码?

2 个答案:

答案 0 :(得分:3)

我认为你需要使用AddressOf

cachePolicy.AddValidationCallback(AddressOf CacheValidateHandler, Nothing)

答案 1 :(得分:3)

要在VB.NET中将函数作为参数传递,必须使用AddressOf关键字:

cachePolicy.AddValidationCallback(AddressOf CacheValidateHandler, Nothing)