Attribute In Member Field
可绑定到成员变量的Attribute。

可绑定到成员变量上的Attribute
1
using System;
2
using System.Reflection;
3
using System.ComponentModel;
4
5
namespace 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
}
测试上面的属性:

测试上面的属性
1
using System;
2
3
namespace 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
}
不是很理解。呵呵
也可以用表态的:

修改后的FieldShowAttribute
1
using System;
2
using System.Reflection;
3
using System.ComponentModel;
4
5
namespace 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类
1
using System;
2
3
namespace 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
using System;2
using System.Reflection;3
using System.ComponentModel;4

5
namespace Relaction.Attribute_in_member_field6


{7
public interface IFieldShow8

{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:Attribute17

{18
private bool _show = false;19
private Type _showclass = null;20
public bool Show21

{22
get23

{24
return _show;25
}26
}27
public Type ShowClass28

{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
}测试上面的属性:
1
using System;2

3
namespace Relaction.Attribute_in_member_field4


{5

/**//// <summary>6
/// 测试字段和属性的自定义属性晕,7
/// </summary>8
public class StringFieldShow:IFieldShow9

{10
public StringFieldShow()11

{12
}13

IFieldShow 成员#region IFieldShow 成员14

15
public string Show(object o)16

{17
return o.ToString();18
}19

20
#endregion21
}22
public class AttrFieldTest23

{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
using System;2
using System.Reflection;3
using System.ComponentModel;4

5
namespace Relaction.Attribute_in_member_field6


{7
public interface IFieldShow8

{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:Attribute17

{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 Show30

{31
get32

{33
return _show;34
}35
}private bool _show = false;36

37
public Type ShowClass38

{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

1
using System;2

3
namespace Relaction.Attribute_in_member_field4


{5

/**//// <summary>6
/// 测试字段和属性的自定义属性7
/// </summary>8
public class StringFieldShow:IFieldShow9

{10
public StringFieldShow()11

{12
}13

IFieldShow 成员#region IFieldShow 成员14

15
public string Show(object o)16

{17
return o.ToString();18
}19

20
#endregion21
}22
public class StringFieldShow223

{24
public static string Show(object o)25

{26
return o.ToString();27
}28
}29
public class AttrFieldTest30

{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
}

浙公网安备 33010602011771号