VB.NET: Extension method for pages that uses GetLocalResourceObject

一个人想着一个人 提交于 2019-12-12 04:23:06

问题


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:


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?



来源:https://stackoverflow.com/questions/4973771/vb-net-extension-method-for-pages-that-uses-getlocalresourceobject

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!