Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 如何从用户特定的资源文件中提取标题?_Asp.net_Vb.net - Fatal编程技术网

Asp.net 如何从用户特定的资源文件中提取标题?

Asp.net 如何从用户特定的资源文件中提取标题?,asp.net,vb.net,Asp.net,Vb.net,我的网站项目中的app\u GlobalResources文件夹下有两个资源文件(CaptionsA.resx和CaptionsB.resx),分别用于CustomerA和CustomerB 例如,在CaptionsA.resx中,我有: MyButtonText ------> Click me! MyButtonText ------> Click Here 在CaptionsB.resx中,我有: MyButtonText ------> Click me!

我的网站项目中的
app\u GlobalResources
文件夹下有两个资源文件(
CaptionsA.resx
CaptionsB.resx
),分别用于
CustomerA
CustomerB

例如,在
CaptionsA.resx
中,我有:

MyButtonText ------>  Click me!
MyButtonText ------>  Click Here
CaptionsB.resx
中,我有:

MyButtonText ------>  Click me!
MyButtonText ------>  Click Here
我必须在我的网站的多个页面上使用标题。但是,当
CustomerA
使用网站时,
CaptionsA.resx
中的所有字幕都应可见,当
CustomerB
使用网站时,
CaptionsB.resx
中的所有字幕都应可见。请记住,两位客户都使用英语作为网站语言,因此我不能使用文化/语言本地化

我想问的是:

  • 如何以编程方式告诉我的网站何时使用哪个资源文件
  • 在我的VB.net代码中写什么
  • 如何在我的代码中访问资源文件

    If CustomerType = CustomerA   
     //RETRIEVE DATA FROM CaptionsA.resx (How to do this?)
    
    else If CustomerType = CustomerB 
    //RETRIEVE DATA FROM CaptionsB.resx (How to do this?)
    
我应该在
aspx
源文件中写什么

<asp:Label ID="LblButtonText" runat="server" Text="<%$ Resources:**WHAT-TO-WRITE-HERE?**,MyButtonText %>"></asp:Label>


我已经搜索了很多,并试图在无数的论坛上找到答案,但与这个主题相关的帖子大多没有答案或没有帮助

以下是您的操作方法

Dim resourceFileBaseName As String = "WebApplicationNamespace.app_GlobalResources.CaptionsA"
Dim isCustomerB As Boolean = True

If isCustomerB Then
    resourceFileBaseName = "WebApplicationNamespace.app_GlobalResources.CaptionsB"
End If

Dim customerBasedResourceManager = New System.Resources.ResourceManager(resourceFileBaseName, GetType(CaptionsA).Assembly)

Dim resourceManagerField = GetType(CaptionsA).GetField("resourceMan", BindingFlags.[Static] Or BindingFlags.NonPublic)

resourceManagerField.SetValue(Nothing, customerBasedResourceManager)
所有ResX文件生成一个等效类(例如CaptionsA),该类具有一个底层ResourceManager,该ResourceManager指向包含所有字符串的CaptionsA资源。根据客户类型,我们可以使此资源管理器指向正确的底层resx文件。但是这个资源管理器是类内部的,因此我们需要反映并设置值。另外,标题A和标题B彼此没有关系,否则我们可以利用一些模式/铸造来访问它们的成员

我们在上述代码中所做的是:

  • 根据客户类型设置正确的资源文件基本名称。(确保为类使用正确的命名空间路径)
  • 创建一个指向实际resx文件的定制resourcemanager
  • 通过反射将CaptionsA类的resourcemanager设置为自定义的
  • 现在,无论何时尝试访问资源,它都会基于基础的resx访问captionsA.resx或CaptionsB.resx

    您会注意到,您也将通过CaptionsA类访问CaptionsB.resx的资源。这是不可避免的,并且是最接近于我们可以通过基于非文化的不同资源获得的基于文化的无缝资源访问

    为了好玩,这里还有C代码

    string resourceFileBaseName = "WebApplicationNamespace.app_GlobalResources.CaptionsA";
    bool isCustomerB = true;
    
    if (isCustomerB)
    {
        resourceFileBaseName = "WebApplicationNamespace.app_GlobalResources.CaptionsB";
    }
    
    var customerBasedResourceManager = new System.Resources.ResourceManager(resourceFileBaseName, 
        typeof(CaptionsA).Assembly);
    
    var resourceManagerField = typeof(CaptionsA).GetField("resourceMan", BindingFlags.Static | BindingFlags.NonPublic);
    resourceManagerField.SetValue(null, customerBasedResourceManager);
    
    CaptionsA.MyButtonText将根据客户类型的resx文件指向该值