How to Use reflection technology to implement ASP. NET internationalized sites

Source: Internet
Author: User
The basic idea is to write a configuration file corresponding to a language, and then use reflection to dynamically set the attribute values of each control.
Configuration File example:
<ClassName=" Article_projectdetail_aspx">

  <Field Name=" Title"
Value="Title"/> <Field Name=" Labtitle. Text"
Value=" Project information"/> </ Class >

Then retrieve attributes or fields through reflection and set the values. If you need to match different languages, you can specify them in the configuration information, for example:
< Language Name =" Zh-CN " >
</ Language>
Set the language according to the language types supported by the client. The advantage of using this method is that it is simpler than the method provided by Microsoft, and easier to maintain. In terms of performance
Common applications, and support for new language types can be added at any time without modification. Code . The following code sets attributes or fields for reflection.
/// <Summary>
/// Set attributes or fields
/// </Summary>
/// <Param name = "instance"> instance to be processed </param>
/// <Param name = "key"> attribute or field name </param>
/// <Param name = "value"> attribute or field value </param>
Private Static void parsepropertyorfield (object instance, string key, string value)
{
// Determine whether the instance is valid
If (null = instance)
{
// Return directly if the instance is empty
Return;
}

// Determine whether the key is a multi-level embedded attribute
If (-1 = key. indexof ('.'))
{
// Obtain member information
System. reflection. memberinfo [] members = instance. GetType (). getmember (key,
System. reflection. membertypes. FIELD |
System. reflection. membertypes. property,
System. reflection. bindingflags. ignorecase |
System. reflection. bindingflags. instance |
System. reflection. bindingflags. nonpublic |
System. reflection. bindingflags. Public );

// Determine whether the object exists
If (0 = members. length)
{
// Directly return if no one exists
Return;
}

// Determine whether it is an attribute
If (system. reflection. membertypes. Property = members [0]. membertype)
{
// For Attribute setting attribute values
(System. reflection. propertyinfo) members [0]). setvalue (instance, value, null );
}
Else
{
// Determine whether it is a field
(System. reflection. fieldinfo) members [0]). setvalue (instance, value );
}

}
Else
{
// The serial number of the attribute
Int fieldindex = key. indexof ('.');

// Obtain the attribute or field name
String fieldname = key. substring (0, fieldindex );

// Obtain member information
System. reflection. memberinfo [] members = instance. GetType (). getmember (fieldname,
System. reflection. membertypes. FIELD |
System. reflection. membertypes. property,
System. reflection. bindingflags. ignorecase |
System. reflection. bindingflags. instance |
System. reflection. bindingflags. nonpublic |
System. reflection. bindingflags. Public );

// Determine whether the object exists
If (0 = members. length)
{
// Directly return if no one exists
Return;
}

// Used to save the attribute or field value
Object fieldobj = NULL;

// Determine whether it is an attribute
If (system. reflection. membertypes. Property = members [0]. membertype)
{
// Obtain the attribute value for an attribute.
Fieldobj = (system. reflection. propertyinfo) members [0]). getvalue (instance, null );
}
Else
{
// Determine whether it is a field
// Obtain the value of a field if it is a field
Fieldobj = (system. reflection. fieldinfo) members [0]). getvalue (instance );
}

// Determine whether an attribute or field exists
If (null = fieldobj)
{
// If it does not exist, return directly
Return;
}

// Obtain the embedded attribute name
String innername = key. substring (fieldindex + 1 );

// If an embedded attribute is contained, perform a recursive call.
parsepropertyorfield (fieldobj, innername, value );
}< BR >}< br>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.