Tag: attributes

如何获取枚举的属性

可能重复: 获得Enum价值的属性 这是我的class级: [AttributeUsage(AttributeTargets.Field)] public sealed class LabelAttribute : Attribute { public LabelAttribute(String labelName) { Name = labelName; } public String Name { get; set; } } 我想得到属性的字段: public enum ECategory { [Label(“Safe”)] Safe, [Label(“LetterDepositBox”)] LetterDepositBox, [Label(“SavingsBookBox”)] SavingsBookBox, }

在C#中使用reflection获取字段的属性

我写了一个从对象中提取字段的方法,如下所示: private static string GetHTMLStatic(ref Object objectX, ref List ExludeFields) { Type objectType = objectX.GetType(); FieldInfo[] fieldInfo = objectType.GetFields(); foreach (FieldInfo field in fieldInfo) { if(!ExludeFields.Contains(field.Name)) { DisplayOutput += GetHTMLAttributes(field); } } return DisplayOutput; } 我的类中的每个字段也都有自己的属性,在这种情况下,我的属性称为HTMLAttributes。 在foreach循环中,我试图获取每个字段的属性及其各自的值。 它目前看起来像这样: private static string GetHTMLAttributes(FieldInfo field) { string AttributeOutput = string.Empty; HTMLAttributes[] htmlAttributes = field.GetCustomAttributes(typeof(HTMLAttributes), false); foreach (HTMLAttributes […]

C#Xml序列化列表后代与Xml属性

早上好, 我有一个从List下降并拥有公共属性的集合。 Xml序列化程序没有取得我的优势。 列表项序列化很好。 我尝试过XmlAttribute属性无济于事。 你们有解决方案吗? public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void button1_Click(object sender, RoutedEventArgs e) { var people = new PersonCollection { new Person { FirstName=”Sue”, Age=17 }, new Person { FirstName=”Joe”, Age=21 } }; people.FavoritePerson = “Sue”; var x = new XmlSerializer(people.GetType()); var b = […]

DefaultValue属性不适用于我的自动属性

我有以下自动属性 [DefaultValue(true)] public bool RetrieveAllInfo { get; set; } 当我尝试在代码中使用它时我发现默认的false为false我认为这是bool变量的默认值,有没有人知道什么是错的!?

我如何GetCustomAttributes?

我使用2.0框架尝试了以下代码,然后我得到一个属性,但是当我在紧凑框架上尝试这个时,它总是返回一个空数组。 MSDN文档说它支持,我做错了什么? Test x = new Test(); FieldInfo field_info = x.GetType().GetField(“ArrayShorts”); object[] custom_attributes = field_info.GetCustomAttributes(typeof(MarshalAsAttribute), false); [StructLayout(LayoutKind.Sequential)] public struct Test { [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)] public ushort[] ArrayShorts; }