VB.NET: Extension method for pages that uses GetLo

2019-08-18 06:57发布

In our .aspx pages, we've got lots of this code:

<%= CType(GetLocalResourceObject("key"), String)) %>

I'd like to add an extension method that I can use in our .aspx views that allows me to do this:

<%= GetLocalResourceString("key") %>

The code isn't working, though:

Imports System.Runtime.CompilerServices
Imports System.Web.UI

Module Extensions

    <Extension()> 
    Public Function GetLocalResourceString(ByVal control as TemplateControl, 
        ByVal resourceKey as String) as String
        Return CType(control.GetLocalResourceObject(resourceKey)), String)
    End Sub

End Module

According to Intellisense, the problem is that GetLocalResourceObject doesn't exist as a method of System.Web.UI.TemplateControl objects.

However, when I look at this page on MSDN, it's there.

What am I doing wrong? Should the extension method be on a different object? I've tried others and have the same Intellisense/build error.

1条回答
兄弟一词,经得起流年.
2楼-- · 2019-08-18 07:24

GetLocalResourceObject is protected so it can only be called from inside the page.

I posted a similar question to see if anyone knew how to call outside the page.

I tried creating a class that inherited the Page object and then exposing a method that called GetLocalResourceObject internally. I couldnt get it to work because when you pass ME/This you are not referencing a Page object.

Here is my similar Question: Is there a way to move a call to ASP GetLocalResourceObject to a external static/shared method?

查看更多
登录 后发表回答