给控件的Description添加多语言支持
1、在工程中新建resx资源文件,如:String.resx,添加string项
2、驱动模块
驱动模块
LogicControlSR#region LogicControlSR
internal sealed class LogicControlSR
{
private static object s_InternalSyncObject;
private static LogicControlSR loader;
private ResourceManager resources;
internal LogicControlSR()
{
this.resources = new ResourceManager(assemblyName+"."+ResxName, base.GetType().Assembly);
}
private static CultureInfo Culture
{
get
{
return new System.Globalization.CultureInfo(1031);
}
}
private static object InternalSyncObject
{
get
{
if (s_InternalSyncObject == null)
{
object obj2 = new object();
Interlocked.CompareExchange(ref s_InternalSyncObject, obj2, null);
}
return s_InternalSyncObject;
}
}
public static ResourceManager Resources
{
get
{
return GetLoader().resources;
}
}
private static LogicControlSR GetLoader()
{
if (loader == null)
{
lock (InternalSyncObject)
{
if (loader == null)
{
loader = new LogicControlSR();
}
}
}
return loader;
}
public static object GetObject(string name)
{
LogicControlSR loader = GetLoader();
if (loader == null)
{
return null;
}
return loader.resources.GetObject(name, Culture);
}
public static string GetString(string name)
{
LogicControlSR loader = GetLoader();
if (loader == null)
{
return null;
}
return loader.resources.GetString(name, Culture);
}
public static string GetString(string name, params object[] args)
{
LogicControlSR loader = GetLoader();
if (loader == null)
{
return null;
}
string format = loader.resources.GetString(name, Culture);
if ((args == null) || (args.Length <= 0))
{
return format;
}
for (int i = 0; i < args.Length; i++)
{
string str2 = args[i] as string;
if ((str2 != null) && (str2.Length > 0x400))
{
args[i] = str2.Substring(0, 0x3fd) + "";
}
}
return string.Format(CultureInfo.CurrentCulture, format, args);
}
}
#endregion
其中assemblyName+"."+ResxName不带.resxLogicControlSR#region LogicControlSR
internal sealed class LogicControlSR
{
private static object s_InternalSyncObject;
private static LogicControlSR loader;
private ResourceManager resources;
internal LogicControlSR()
{
this.resources = new ResourceManager(assemblyName+"."+ResxName, base.GetType().Assembly);
}
private static CultureInfo Culture
{
get
{
return new System.Globalization.CultureInfo(1031);
}
}
private static object InternalSyncObject
{
get
{
if (s_InternalSyncObject == null)
{
object obj2 = new object();
Interlocked.CompareExchange(ref s_InternalSyncObject, obj2, null);
}
return s_InternalSyncObject;
}
}
public static ResourceManager Resources
{
get
{
return GetLoader().resources;
}
}
private static LogicControlSR GetLoader()
{
if (loader == null)
{
lock (InternalSyncObject)
{
if (loader == null)
{
loader = new LogicControlSR();
}
}
}
return loader;
}
public static object GetObject(string name)
{
LogicControlSR loader = GetLoader();
if (loader == null)
{
return null;
}
return loader.resources.GetObject(name, Culture);
}
public static string GetString(string name)
{
LogicControlSR loader = GetLoader();
if (loader == null)
{
return null;
}
return loader.resources.GetString(name, Culture);
}
public static string GetString(string name, params object[] args)
{
LogicControlSR loader = GetLoader();
if (loader == null)
{
return null;
}
string format = loader.resources.GetString(name, Culture);
if ((args == null) || (args.Length <= 0))
{
return format;
}
for (int i = 0; i < args.Length; i++)
{
string str2 = args[i] as string;
if ((str2 != null) && (str2.Length > 0x400))
{
args[i] = str2.Substring(0, 0x3fd) + "";
}
}
return string.Format(CultureInfo.CurrentCulture, format, args);
}
}
#endregion
例如:DataFlow.CommonData.Strings
3、定义description Attribute
LogicControlDescriptionAttribute
internal class LogicControlDescriptionAttribute : System.ComponentModel.DescriptionAttribute
{
private bool replaced;
internal LogicControlDescriptionAttribute(string description)
: base(description)
{
}
public override string Description
{
get
{
if (!this.replaced)
{
this.replaced = true;
base.DescriptionValue = LogicControlSR.GetString(base.Description);
}
return base.Description;
}
}
public override object TypeId
{
get
{
return typeof(System.ComponentModel.DescriptionAttribute);
}
}
}
internal class LogicControlDescriptionAttribute : System.ComponentModel.DescriptionAttribute
{
private bool replaced;
internal LogicControlDescriptionAttribute(string description)
: base(description)
{
}
public override string Description
{
get
{
if (!this.replaced)
{
this.replaced = true;
base.DescriptionValue = LogicControlSR.GetString(base.Description);
}
return base.Description;
}
}
public override object TypeId
{
get
{
return typeof(System.ComponentModel.DescriptionAttribute);
}
}
}
4、给控件相关属性添加Attribute
Text
[LogicControlDescriptionAttribute("LogicControl_Text")]
public string Text
{
get { return _Text; }
set
{
_Text = value;
}
}
[LogicControlDescriptionAttribute("LogicControl_Text")]
public string Text
{
get { return _Text; }
set
{
_Text = value;
}
}