编译每个project有效localize的方法

本文介绍了一个用于设置UI文化属性的.NET类NewCulture,并通过一个CultureSetter特性来标记可被设置为当前文化的属性。此外,还提供了一个方法用于遍历指定类型的静态公共属性并根据CultureSetter特性设置CultureInfo。

    public sealed class NewCulture
    {
        private static CultureInfo uiCultureInfo;

        private NewCulture() { }

        [CultureSetter]
        public static CultureInfo UICultureInfo
        {
            get
            {
                return uiCultureInfo;
            }
            set
            {
                if (uiCultureInfo != value)
                {
                    uiCultureInfo = value;
                    StringResource.StringRes.Culture = value;
                }
            }
        }
    }

_______________________________________

    [AttributeUsage(AttributeTargets.Property)]
    public class CultureSetterAttribute : Attribute
    {
    }

_______________________________________

        private void SetCultureInfo(Type type, CultureAttribute attri)
        {
            if (type != null )
            {
                BindingFlags flags = BindingFlags.Static | BindingFlags.Public;
                PropertyInfo[] propertyInfos = type.GetProperties(flags);
                if (propertyInfos != null)
                {
                    foreach (PropertyInfo property in propertyInfos)
                    {
                        if (property.CanWrite &&
                            property.PropertyType.Equals(typeof(CultureInfo)))
                        {
                            object[] attributes = property.GetCustomAttributes(typeof(CultureSetterAttribute), false);
                            if (attributes != null && attributes.Length == 1)
                            {
                                property.SetValue(null, NewUICulture.UICulture, null);
                            }
                        }
                    }
                }
            }
        }

 


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值