C# 获取属性信息
我的UI是用大量可翻译的用户控件构建的。usercontrols中的一些控件不应该被翻译,我想用[DoNotTranslate]`自定义属性标记它们 在我的userControl.designer.cs文件中C# 获取属性信息,c#,reflection,C#,Reflection,我的UI是用大量可翻译的用户控件构建的。usercontrols中的一些控件不应该被翻译,我想用[DoNotTranslate]`自定义属性标记它们 在我的userControl.designer.cs文件中 [DoNotTranslate] private DevExpress.XtraEditors.LabelControl maxLabel; [DoNotTranslate] private DevExpress.XtraEditors.LabelCon
[DoNotTranslate]
private DevExpress.XtraEditors.LabelControl maxLabel;
[DoNotTranslate]
private DevExpress.XtraEditors.LabelControl valueLabel;
//all other controls
翻译功能需要一个(用户)控件,然后遍历所有子控件。控件,以确保翻译所有控件,而无需对每个控件调用翻译功能
是否可以确定控件是否设置了自定义属性?问题是,当我遍历所有控件时,我不知道如何在translation函数中获取属性信息
非常感谢您的任何建议
谢谢您可以使用方法来确定该属性是否已应用。比如说,
static readonly Type _DoNotTranslateAttribute = typeof(DoNotTranslate);
.... // other code
var t = control.GetType();
if (t.GetCustomAttributes(_DoNotTranslateAttribute, false).length > 0)
{
// do not translate
}
(免责声明:未测试/未编译的代码-只是为了让您了解如何使用该函数)您可以使用方法来确定是否应用了该属性。比如说,
static readonly Type _DoNotTranslateAttribute = typeof(DoNotTranslate);
.... // other code
var t = control.GetType();
if (t.GetCustomAttributes(_DoNotTranslateAttribute, false).length > 0)
{
// do not translate
}
(免责声明:未经测试/未编译的代码-只是为了了解如何使用该函数)编辑:我现在从您发布的代码中看到,属性位于控件的属性上,而不是定义控件本身的类上。您可以这样尝试:
public IEnumerable<PropertyInfo> GetNonTranslatableProperties(WebControl control)
{
foreach (PropertyInfo property in control.GetType().GetProperties())
{
if(
property
.GetCustomAttributes(true)
.Count(item => item is DoNotTranslateAttribute) > 0)
yield return property;
}
}
public IEnumerable<Control> GetNonTranslatableChildren(Control control)
{
foreach(Control c in control.Controls)
{
if(
c.GetType()
.GetCustomAttributes(true)
.Count(item => item is DoNotTranslateAttribute) > 0)
yield return c;
}
}
===================
对于每个控件,您可以执行以下操作:
myCustomControl.GetType()
.GetCustomAttributes(true)
.Where(item => item is DoNotTranslateAttribute);
例如,您可以像下面这样枚举所有“不可翻译”控件:
public IEnumerable<PropertyInfo> GetNonTranslatableProperties(WebControl control)
{
foreach (PropertyInfo property in control.GetType().GetProperties())
{
if(
property
.GetCustomAttributes(true)
.Count(item => item is DoNotTranslateAttribute) > 0)
yield return property;
}
}
public IEnumerable<Control> GetNonTranslatableChildren(Control control)
{
foreach(Control c in control.Controls)
{
if(
c.GetType()
.GetCustomAttributes(true)
.Count(item => item is DoNotTranslateAttribute) > 0)
yield return c;
}
}
public IEnumerable GetNonTranslatableChildren(控件)
{
foreach(Control.Controls中的控件c)
{
如果(
c、 GetType()
.GetCustomAttributes(true)
.Count(项=>项为DoNotTranslateAttribute)>0)
收益率c;
}
}
编辑:我现在从您发布的代码中看到,属性位于控件的属性上,而不是定义控件本身的类上。您可以这样尝试:
public IEnumerable<PropertyInfo> GetNonTranslatableProperties(WebControl control)
{
foreach (PropertyInfo property in control.GetType().GetProperties())
{
if(
property
.GetCustomAttributes(true)
.Count(item => item is DoNotTranslateAttribute) > 0)
yield return property;
}
}
public IEnumerable<Control> GetNonTranslatableChildren(Control control)
{
foreach(Control c in control.Controls)
{
if(
c.GetType()
.GetCustomAttributes(true)
.Count(item => item is DoNotTranslateAttribute) > 0)
yield return c;
}
}
===================
对于每个控件,您可以执行以下操作:
myCustomControl.GetType()
.GetCustomAttributes(true)
.Where(item => item is DoNotTranslateAttribute);
例如,您可以像下面这样枚举所有“不可翻译”控件:
public IEnumerable<PropertyInfo> GetNonTranslatableProperties(WebControl control)
{
foreach (PropertyInfo property in control.GetType().GetProperties())
{
if(
property
.GetCustomAttributes(true)
.Count(item => item is DoNotTranslateAttribute) > 0)
yield return property;
}
}
public IEnumerable<Control> GetNonTranslatableChildren(Control control)
{
foreach(Control c in control.Controls)
{
if(
c.GetType()
.GetCustomAttributes(true)
.Count(item => item is DoNotTranslateAttribute) > 0)
yield return c;
}
}
public IEnumerable GetNonTranslatableChildren(控件)
{
foreach(Control.Controls中的控件c)
{
如果(
c、 GetType()
.GetCustomAttributes(true)
.Count(项=>项为DoNotTranslateAttribute)>0)
收益率c;
}
}
您是否可以添加到目前为止编写的代码(自定义属性的声明以及您尝试在翻译函数中读取信息的方式)?您是否可以添加到目前为止编写的代码(自定义属性的声明以及您尝试在翻译函数中读取信息的方式)?thx用于响应,但对我不起作用。在代码中,我是否检查该特定类(control.GetType())是否应用了自定义属性(该属性在类上而不是在成员上)?在我的例子中,我的usercontrol有两个我不想翻译的标签。我不想翻译的是usercontrol的这两个成员标签,而不是标签类型本身。@Tarscher,您是对的,但您的原始帖子没有给出任何您希望在属性级别应用属性的线索。不管怎样,@paolo已经编辑了他的答案,给出了正确的解决方案——IMO,这两种方法中的任何一种都会起作用。thx用于回复,但它不适用于我。在代码中,我是否检查该特定类(control.GetType())是否应用了自定义属性(该属性在类上而不是在成员上)?在我的例子中,我的usercontrol有两个我不想翻译的标签。我不想翻译的是usercontrol的这两个成员标签,而不是标签类型本身。@Tarscher,您是对的,但您的原始帖子没有给出任何您希望在属性级别应用属性的线索。不管怎样,@paolo已经编辑了他的答案,给出了正确的解决方案——IMO,这两种方法都可以。