In this example, the new prefix is "Externalresource". The required syntax for this new expression is shown below.
<%$ Externalresource: [assemblyname]| [ResourceType], [ResourceKey]%>
This expression extracts resources from a specific assembly using the same GlobalExternalResourceProvider described earlier. To support this new expression, we will create a custom type ExternalResourceExpressionBuilder. Table 2 summarizes the functionality provided by the ExpressionBuilder method for each replacement.
Table 2 summarizes the functionality provided by each substitution method
| Method |
Description |
| EvaluateExpression |
Returns the resource value of a Externalresource expression in an precompiled page. |
| GetCodeExpression |
Returns the code generated for the Externalresource expression. This code invokes the custom resource provider GlobalExternalResourceProvider. |
| ParseExpression |
Validates an externalresource expression by attempting to access an expression resource. If a resource cannot be found, page analysis will fail. |
| Supportsevaluate |
property indicates whether page judgments are not compiled or unsupported. In this implementation, returns TRUE. |
Using ExternalResourceExpressionBuilder, you can declare a custom localized expression as shown below.
<asp:Label ID="labExternalResource" runat="server" Text="<%$ ExternalResources:CommonResources|CommonTerms, Hello %>" meta:localize="false" ></asp:Label>
Keep in mind that expressions are analyzed at design time and before compilation. ParseExpression is invoked during page analysis to verify that the resource expression is accurate and that the requested resource is actually present. The following code illustrates this implementation.
public override Object ParseExpression (string expression, Type PropertyType, ExpressionBuilderContext context
{
if (string). IsNullOrEmpty (expression))
{
throw new ArgumentException (String.Format ( Thread.currentthread.currentuiculture,properties.resources.expression_toofewparameters, Expression));
}
Externalresourceexpressionfields fields = null;
string classkey = null;
string resourceKey = null;
String[] expparams = expression. Split (new char[] {', '});
if (Expparams.length > 2)
{
throw new ArgumentException (String.Format (Thread.CurrentThread.CurrentUICu Lture, Properties.Resources.Expression_TooManyParameters, Expression));
}
if (expparams.length = = 1)
{
throw new ArgumentException (String.Format (Thread.CurrentThread.Curre Ntuiculture, Properties.Resources.Expression_TooFewParameters, Expression));
}
Else
{
ClassKey = expparams[0]. Trim ();
ResourceKey = expparams[1]. Trim ();
}
Fields = new Externalresourceexpressionfields (ClassKey, ResourceKey);
Externalresourceexpressionbuilder.ensureresourceproviderfactory ();
IResourceProvider rp = ExternalResourceExpressionBuilder.
S_resourceproviderfactory.createglobalresourceprovider (fields. ClassKey);
Object res = Rp. GetObject (fields. ResourceKey, CultureInfo.InvariantCulture);
if (res = = null)
{
throw new ArgumentException (String.Format (Thread.CurrentThread.CurrentUICulture, Prope Rties. Resources.rm_resourcenotfound, fields. ResourceKey));
}
Return fields
}