Attribute In Member Field
可绑定到成员变量的Attribute。
测试上面的属性:
测试:
也可以用表态的:
可绑定到成员变量上的Attribute
1using System;
2using System.Reflection;
3using System.ComponentModel;
4
5namespace Relaction.Attribute_in_member_field
6{
7 public interface IFieldShow
8 {
9 string Show(object o);
10 }
11 /**//// <summary>
12 /// 可用于字段或属性的自定义属性。
13 /// </summary>
14 ///
15 [AttributeUsage(AttributeTargets.Field|AttributeTargets.Property,Inherited = false,AllowMultiple=false)]
16 public class FieldShowAttribute:Attribute
17 {
18 private bool _show = false;
19 private Type _showclass = null;
20 public bool Show
21 {
22 get
23 {
24 return _show;
25 }
26 }
27 public Type ShowClass
28 {
29 get
30 {
31 return _showclass;
32 }
33 }
34 public FieldShowAttribute(bool show,Type showclass)
35 {
36 _show = show;
37 _showclass = showclass;
38 if(_showclass == null)
39 {
40 throw(new Exception("出错!"));
41 }
42 }
43 public IFieldShow CreateShowInstance()
44 {
45 object o ;
46 ConstructorInfo cinfo = _showclass.GetConstructor(new Type[]{});
47 o = cinfo.Invoke(null);
48 return (IFieldShow)o;
49 }
50 }
51}
1using System;
2using System.Reflection;
3using System.ComponentModel;
4
5namespace Relaction.Attribute_in_member_field
6{
7 public interface IFieldShow
8 {
9 string Show(object o);
10 }
11 /**//// <summary>
12 /// 可用于字段或属性的自定义属性。
13 /// </summary>
14 ///
15 [AttributeUsage(AttributeTargets.Field|AttributeTargets.Property,Inherited = false,AllowMultiple=false)]
16 public class FieldShowAttribute:Attribute
17 {
18 private bool _show = false;
19 private Type _showclass = null;
20 public bool Show
21 {
22 get
23 {
24 return _show;
25 }
26 }
27 public Type ShowClass
28 {
29 get
30 {
31 return _showclass;
32 }
33 }
34 public FieldShowAttribute(bool show,Type showclass)
35 {
36 _show = show;
37 _showclass = showclass;
38 if(_showclass == null)
39 {
40 throw(new Exception("出错!"));
41 }
42 }
43 public IFieldShow CreateShowInstance()
44 {
45 object o ;
46 ConstructorInfo cinfo = _showclass.GetConstructor(new Type[]{});
47 o = cinfo.Invoke(null);
48 return (IFieldShow)o;
49 }
50 }
51}
测试上面的属性:
测试上面的属性
1using System;
2
3namespace Relaction.Attribute_in_member_field
4{
5 /**//// <summary>
6 /// 测试字段和属性的自定义属性晕,
7 /// </summary>
8 public class StringFieldShow:IFieldShow
9 {
10 public StringFieldShow()
11 {
12 }
13 IFieldShow 成员#region IFieldShow 成员
14
15 public string Show(object o)
16 {
17 return o.ToString();
18 }
19
20 #endregion
21 }
22 public class AttrFieldTest
23 {
24 [FieldShow(true,typeof(StringFieldShow))]
25 public int _num = 0;
26
27 public AttrFieldTest()
28 {
29 }
30 }
31}
1using System;
2
3namespace Relaction.Attribute_in_member_field
4{
5 /**//// <summary>
6 /// 测试字段和属性的自定义属性晕,
7 /// </summary>
8 public class StringFieldShow:IFieldShow
9 {
10 public StringFieldShow()
11 {
12 }
13 IFieldShow 成员#region IFieldShow 成员
14
15 public string Show(object o)
16 {
17 return o.ToString();
18 }
19
20 #endregion
21 }
22 public class AttrFieldTest
23 {
24 [FieldShow(true,typeof(StringFieldShow))]
25 public int _num = 0;
26
27 public AttrFieldTest()
28 {
29 }
30 }
31}
测试:
测试
1 private void button3_Click(object sender, System.EventArgs e)
2 {
3 AttrFieldTest[] o = new AttrFieldTest[]{new AttrFieldTest(),new AttrFieldTest()};
4 o[0]._num = 15;
5 o[1]._num = 30;
6 foreach(AttrFieldTest item in o)
7 {
8 foreach(FieldInfo field in item.GetType().GetFields())
9 {
10 FieldShowAttribute[] fattr = (FieldShowAttribute[])field.GetCustomAttributes(typeof(FieldShowAttribute),false);
11 if(fattr.Length != 0)
12 {
13 label1.Text += field.Name+fattr[0].CreateShowInstance().Show(field.GetValue(item));
14 }
15 }
16 }
17 }
不是很理解。呵呵 1 private void button3_Click(object sender, System.EventArgs e)
2 {
3 AttrFieldTest[] o = new AttrFieldTest[]{new AttrFieldTest(),new AttrFieldTest()};
4 o[0]._num = 15;
5 o[1]._num = 30;
6 foreach(AttrFieldTest item in o)
7 {
8 foreach(FieldInfo field in item.GetType().GetFields())
9 {
10 FieldShowAttribute[] fattr = (FieldShowAttribute[])field.GetCustomAttributes(typeof(FieldShowAttribute),false);
11 if(fattr.Length != 0)
12 {
13 label1.Text += field.Name+fattr[0].CreateShowInstance().Show(field.GetValue(item));
14 }
15 }
16 }
17 }
也可以用表态的:
修改后的FieldShowAttribute
1using System;
2using System.Reflection;
3using System.ComponentModel;
4
5namespace Relaction.Attribute_in_member_field
6{
7 public interface IFieldShow
8 {
9 string Show(object o);
10 }
11 /**//// <summary>
12 /// 可用于字段或属性的自定义属性。
13 /// </summary>
14 ///
15 [AttributeUsage(AttributeTargets.Field|AttributeTargets.Property,Inherited = false,AllowMultiple=false)]
16 public class FieldShowAttribute:Attribute
17 {
18 public FieldShowAttribute(bool show,Type showclass)
19 {
20 _show = show;
21 _showclass = showclass;
22 if(_showclass == null)
23 {
24 throw(new Exception("出错!"));
25 }
26 }
27
28
29 public bool Show
30 {
31 get
32 {
33 return _show;
34 }
35 }private bool _show = false;
36
37 public Type ShowClass
38 {
39 get
40 {
41 return _showclass;
42 }
43 }private Type _showclass = null;
44
45 public IFieldShow CreateShowInstance()
46 {
47 object o ;
48 ConstructorInfo cinfo = _showclass.GetConstructor(new Type[]{});
49 o = cinfo.Invoke(null);
50 return (IFieldShow)o;
51 }
52 public static string ShowValue(FieldShowAttribute attr,object o,FieldInfo valueobject)
53 {
54 MethodInfo mi = attr.ShowClass.GetMethod("Show",BindingFlags.Static|BindingFlags.Public);
55 return (string)mi.Invoke(null,new object[]{valueobject.GetValue(o)});
56 }
57 }
58}
59
1using System;
2using System.Reflection;
3using System.ComponentModel;
4
5namespace Relaction.Attribute_in_member_field
6{
7 public interface IFieldShow
8 {
9 string Show(object o);
10 }
11 /**//// <summary>
12 /// 可用于字段或属性的自定义属性。
13 /// </summary>
14 ///
15 [AttributeUsage(AttributeTargets.Field|AttributeTargets.Property,Inherited = false,AllowMultiple=false)]
16 public class FieldShowAttribute:Attribute
17 {
18 public FieldShowAttribute(bool show,Type showclass)
19 {
20 _show = show;
21 _showclass = showclass;
22 if(_showclass == null)
23 {
24 throw(new Exception("出错!"));
25 }
26 }
27
28
29 public bool Show
30 {
31 get
32 {
33 return _show;
34 }
35 }private bool _show = false;
36
37 public Type ShowClass
38 {
39 get
40 {
41 return _showclass;
42 }
43 }private Type _showclass = null;
44
45 public IFieldShow CreateShowInstance()
46 {
47 object o ;
48 ConstructorInfo cinfo = _showclass.GetConstructor(new Type[]{});
49 o = cinfo.Invoke(null);
50 return (IFieldShow)o;
51 }
52 public static string ShowValue(FieldShowAttribute attr,object o,FieldInfo valueobject)
53 {
54 MethodInfo mi = attr.ShowClass.GetMethod("Show",BindingFlags.Static|BindingFlags.Public);
55 return (string)mi.Invoke(null,new object[]{valueobject.GetValue(o)});
56 }
57 }
58}
59
修改后贴上FieldShowAttribute类
1using System;
2
3namespace Relaction.Attribute_in_member_field
4{
5 /**//// <summary>
6 /// 测试字段和属性的自定义属性
7 /// </summary>
8 public class StringFieldShow:IFieldShow
9 {
10 public StringFieldShow()
11 {
12 }
13 IFieldShow 成员#region IFieldShow 成员
14
15 public string Show(object o)
16 {
17 return o.ToString();
18 }
19
20 #endregion
21 }
22 public class StringFieldShow2
23 {
24 public static string Show(object o)
25 {
26 return o.ToString();
27 }
28 }
29 public class AttrFieldTest
30 {
31 [FieldShow(true,typeof(StringFieldShow2))]
32 public int _num = 0;
33
34 public AttrFieldTest()
35 {
36 }
37 }
38}
39
1using System;
2
3namespace Relaction.Attribute_in_member_field
4{
5 /**//// <summary>
6 /// 测试字段和属性的自定义属性
7 /// </summary>
8 public class StringFieldShow:IFieldShow
9 {
10 public StringFieldShow()
11 {
12 }
13 IFieldShow 成员#region IFieldShow 成员
14
15 public string Show(object o)
16 {
17 return o.ToString();
18 }
19
20 #endregion
21 }
22 public class StringFieldShow2
23 {
24 public static string Show(object o)
25 {
26 return o.ToString();
27 }
28 }
29 public class AttrFieldTest
30 {
31 [FieldShow(true,typeof(StringFieldShow2))]
32 public int _num = 0;
33
34 public AttrFieldTest()
35 {
36 }
37 }
38}
39
客户调用
1 private void button4_Click(object sender, System.EventArgs e)
2 {
3 AttrFieldTest[] o = new AttrFieldTest[]{new AttrFieldTest(),new AttrFieldTest()};
4 o[0]._num = 15;
5 o[1]._num = 30;
6 foreach(AttrFieldTest item in o)
7 {
8 foreach(FieldInfo field in item.GetType().GetFields())
9 {
10 FieldShowAttribute[] fattr = (FieldShowAttribute[])field.GetCustomAttributes(typeof(FieldShowAttribute),false);
11 if(fattr.Length != 0)
12 {
13 label1.Text += FieldShowAttribute.ShowValue(fattr[0],item,field);
14 }
15 }
16 }
17 }
1 private void button4_Click(object sender, System.EventArgs e)
2 {
3 AttrFieldTest[] o = new AttrFieldTest[]{new AttrFieldTest(),new AttrFieldTest()};
4 o[0]._num = 15;
5 o[1]._num = 30;
6 foreach(AttrFieldTest item in o)
7 {
8 foreach(FieldInfo field in item.GetType().GetFields())
9 {
10 FieldShowAttribute[] fattr = (FieldShowAttribute[])field.GetCustomAttributes(typeof(FieldShowAttribute),false);
11 if(fattr.Length != 0)
12 {
13 label1.Text += FieldShowAttribute.ShowValue(fattr[0],item,field);
14 }
15 }
16 }
17 }