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

 可绑定到成员变量上的Attribute
可绑定到成员变量上的Attribute
1 using System;
using System;
2 using System.Reflection;
using System.Reflection;
3 using System.ComponentModel;
using System.ComponentModel;
4
5 namespace Relaction.Attribute_in_member_field
namespace Relaction.Attribute_in_member_field
6

 {
{
7 public interface IFieldShow
    public interface IFieldShow
8
 
     {
{
9 string Show(object o);
        string Show(object o);
10 }
    }
11
 /**//// <summary>
    /**//// <summary>
12 /// 可用于字段或属性的自定义属性。
    /// 可用于字段或属性的自定义属性。
13 /// </summary>
    /// </summary>
14 ///
    /// 
15 [AttributeUsage(AttributeTargets.Field|AttributeTargets.Property,Inherited = false,AllowMultiple=false)]
    [AttributeUsage(AttributeTargets.Field|AttributeTargets.Property,Inherited = false,AllowMultiple=false)]
16 public class FieldShowAttribute:Attribute
    public class FieldShowAttribute:Attribute
17
 
     {
{
18 private bool _show = false;
        private bool _show = false;
19 private Type _showclass = null;
        private Type _showclass = null;
20 public bool Show
        public bool Show
21
 
         {
{
22 get
            get
23
 
             {
{
24 return _show;
                return _show;
25 }
            }
26 }
        }
27 public Type ShowClass
        public Type ShowClass
28
 
         {
{
29 get
            get 
30
 
             {
{
31 return _showclass;
                return _showclass;
32 }
            }
33 }
        }
34 public FieldShowAttribute(bool show,Type showclass)
        public FieldShowAttribute(bool show,Type showclass)
35
 
         {
{
36 _show = show;
            _show = show;
37 _showclass = showclass;
            _showclass = showclass;
38 if(_showclass == null)
            if(_showclass == null)
39
 
             {
{
40 throw(new Exception("出错!"));
                throw(new Exception("出错!"));
41 }
            }
42 }
        }
43 public IFieldShow CreateShowInstance()
        public IFieldShow CreateShowInstance()
44
 
         {
{
45 object o ;
            object o ;
46
 ConstructorInfo cinfo = _showclass.GetConstructor(new Type[]
            ConstructorInfo cinfo = _showclass.GetConstructor(new Type[] {});
{});
47 o = cinfo.Invoke(null);
            o = cinfo.Invoke(null);
48 return (IFieldShow)o;
            return (IFieldShow)o;
49 }
        }
50 }
    }
51 }
}
测试上面的属性:

 测试上面的属性
测试上面的属性
1 using System;
using System;
2
3 namespace Relaction.Attribute_in_member_field
namespace Relaction.Attribute_in_member_field
4

 {
{
5
 /**//// <summary>
    /**//// <summary>
6 /// 测试字段和属性的自定义属性晕,
    /// 测试字段和属性的自定义属性晕,
7 /// </summary>
    /// </summary>
8 public class StringFieldShow:IFieldShow
    public class StringFieldShow:IFieldShow
9
 
     {
{
10 public StringFieldShow()
        public StringFieldShow()
11
 
         {
{
12 }
        }
13
 IFieldShow 成员#region IFieldShow 成员
        IFieldShow 成员#region IFieldShow 成员
14
15 public string Show(object o)
        public string Show(object o)
16
 
         {
{
17 return o.ToString();
            return o.ToString();
18 }
        }
19
20 #endregion
        #endregion
21 }
    }
22 public class AttrFieldTest
    public class AttrFieldTest
23
 
     {
{
24 [FieldShow(true,typeof(StringFieldShow))]
        [FieldShow(true,typeof(StringFieldShow))]
25 public int _num = 0;
        public int _num = 0;
26
27 public AttrFieldTest()
        public AttrFieldTest()
28
 
         {
{
29 }
        }
30 }
    }
31 }
}
测试:

 测试
测试
1 private void button3_Click(object sender, System.EventArgs e)
    private void button3_Click(object sender, System.EventArgs e)
2
 
         {
{
3
 AttrFieldTest[] o = new AttrFieldTest[]
            AttrFieldTest[] o = new AttrFieldTest[] {new AttrFieldTest(),new AttrFieldTest()};
{new AttrFieldTest(),new AttrFieldTest()};
4 o[0]._num = 15;
            o[0]._num = 15;
5 o[1]._num = 30;
            o[1]._num = 30;
6 foreach(AttrFieldTest item in o)
            foreach(AttrFieldTest item in o)
7
 
             {
{
8 foreach(FieldInfo field in item.GetType().GetFields())
                foreach(FieldInfo field in item.GetType().GetFields())
9
 
                 {
{
10 FieldShowAttribute[] fattr = (FieldShowAttribute[])field.GetCustomAttributes(typeof(FieldShowAttribute),false);
                  FieldShowAttribute[] fattr = (FieldShowAttribute[])field.GetCustomAttributes(typeof(FieldShowAttribute),false);
11 if(fattr.Length != 0)
                    if(fattr.Length != 0)
12
 
                     {
{
13 label1.Text += field.Name+fattr[0].CreateShowInstance().Show(field.GetValue(item));
                        label1.Text += field.Name+fattr[0].CreateShowInstance().Show(field.GetValue(item));
14 }
                    }
15 }
                }
16 }
            }
17 }
不是很理解。呵呵
        }
不是很理解。呵呵 
也可以用表态的:

 修改后的FieldShowAttribute
修改后的FieldShowAttribute
1 using System;
using System;
2 using System.Reflection;
using System.Reflection;
3 using System.ComponentModel;
using System.ComponentModel;
4
5 namespace Relaction.Attribute_in_member_field
namespace Relaction.Attribute_in_member_field
6

 {
{
7 public interface IFieldShow
    public interface IFieldShow
8
 
     {
{
9 string Show(object o);
        string Show(object o);
10 }
    }
11
 /**//// <summary>
    /**//// <summary>
12 /// 可用于字段或属性的自定义属性。
    /// 可用于字段或属性的自定义属性。
13 /// </summary>
    /// </summary>
14 ///
    /// 
15 [AttributeUsage(AttributeTargets.Field|AttributeTargets.Property,Inherited = false,AllowMultiple=false)]
    [AttributeUsage(AttributeTargets.Field|AttributeTargets.Property,Inherited = false,AllowMultiple=false)]
16 public class FieldShowAttribute:Attribute
    public class FieldShowAttribute:Attribute
17
 
     {
{
18 public FieldShowAttribute(bool show,Type showclass)
        public FieldShowAttribute(bool show,Type showclass)
19
 
         {
{
20 _show = show;
            _show = show;
21 _showclass = showclass;
            _showclass = showclass;
22 if(_showclass == null)
            if(_showclass == null)
23
 
             {
{
24 throw(new Exception("出错!"));
                throw(new Exception("出错!"));
25 }
            }
26 }
        }
27
28
29 public bool Show
        public bool Show
30
 
         {
{
31 get
            get
32
 
             {
{
33 return _show;
                return _show;
34 }
            }
35 }private bool _show = false;
        }private bool _show = false;
36
37 public Type ShowClass
        public Type ShowClass
38
 
         {
{
39 get
            get 
40
 
             {
{
41 return _showclass;
                return _showclass;
42 }
            }
43 }private Type _showclass = null;
        }private Type _showclass = null;
44 
    
45 public IFieldShow CreateShowInstance()
        public IFieldShow CreateShowInstance()
46
 
         {
{
47 object o ;
            object o ;
48
 ConstructorInfo cinfo = _showclass.GetConstructor(new Type[]
            ConstructorInfo cinfo = _showclass.GetConstructor(new Type[] {});
{});
49 o = cinfo.Invoke(null);
            o = cinfo.Invoke(null);
50 return (IFieldShow)o;
            return (IFieldShow)o;
51 }
        }
52 public static string ShowValue(FieldShowAttribute attr,object o,FieldInfo valueobject)
        public static string ShowValue(FieldShowAttribute attr,object o,FieldInfo valueobject)
53
 
         {
{
54 MethodInfo mi = attr.ShowClass.GetMethod("Show",BindingFlags.Static|BindingFlags.Public);
            MethodInfo mi = attr.ShowClass.GetMethod("Show",BindingFlags.Static|BindingFlags.Public);
55
 return (string)mi.Invoke(null,new object[]
            return (string)mi.Invoke(null,new object[] {valueobject.GetValue(o)});
{valueobject.GetValue(o)});
56 }
        }
57 }
    }
58 }
}
59 

 修改后贴上FieldShowAttribute类
修改后贴上FieldShowAttribute类
1 using System;
using System;
2
3 namespace Relaction.Attribute_in_member_field
namespace Relaction.Attribute_in_member_field
4

 {
{
5
 /**//// <summary>
    /**//// <summary>
6 /// 测试字段和属性的自定义属性
    /// 测试字段和属性的自定义属性
7 /// </summary>
    /// </summary>
8 public class StringFieldShow:IFieldShow
    public class StringFieldShow:IFieldShow
9
 
     {
{
10 public StringFieldShow()
        public StringFieldShow()
11
 
         {
{
12 }
        }
13
 IFieldShow 成员#region IFieldShow 成员
        IFieldShow 成员#region IFieldShow 成员
14
15 public string Show(object o)
        public string Show(object o)
16
 
         {
{
17 return o.ToString();
            return o.ToString();
18 }
        }
19
20 #endregion
        #endregion
21 }
    }
22 public class StringFieldShow2
    public class StringFieldShow2
23
 
     {
{
24 public static string Show(object o)
        public static string Show(object o)
25
 
         {
{
26 return o.ToString();
            return o.ToString();
27 }
        }
28 }
    }
29 public class AttrFieldTest
    public class AttrFieldTest
30
 
     {
{
31 [FieldShow(true,typeof(StringFieldShow2))]
        [FieldShow(true,typeof(StringFieldShow2))]
32 public int _num = 0;
        public int _num = 0;
33
34 public AttrFieldTest()
        public AttrFieldTest()
35
 
         {
{
36 }
        }
37 }
    }
38 }
}
39 

 客户调用
客户调用
1 private void button4_Click(object sender, System.EventArgs e)
    private void button4_Click(object sender, System.EventArgs e)
2
 
         {
{
3
 AttrFieldTest[] o = new AttrFieldTest[]
            AttrFieldTest[] o = new AttrFieldTest[] {new AttrFieldTest(),new AttrFieldTest()};
{new AttrFieldTest(),new AttrFieldTest()};
4 o[0]._num = 15;
            o[0]._num = 15;
5 o[1]._num = 30;
            o[1]._num = 30;
6 foreach(AttrFieldTest item in o)
            foreach(AttrFieldTest item in o)
7
 
             {
{
8 foreach(FieldInfo field in item.GetType().GetFields())
                foreach(FieldInfo field in item.GetType().GetFields())
9
 
                 {
{
10 FieldShowAttribute[] fattr = (FieldShowAttribute[])field.GetCustomAttributes(typeof(FieldShowAttribute),false);
                    FieldShowAttribute[] fattr = (FieldShowAttribute[])field.GetCustomAttributes(typeof(FieldShowAttribute),false);
11 if(fattr.Length != 0)
                    if(fattr.Length != 0)
12
 
                     {
{
13 label1.Text += FieldShowAttribute.ShowValue(fattr[0],item,field);
                        label1.Text += FieldShowAttribute.ShowValue(fattr[0],item,field);
14 }
                    }
15 }
                }
16 }
            }
17 }
        }

 可绑定到成员变量上的Attribute
可绑定到成员变量上的Attribute1
 using System;
using System;2
 using System.Reflection;
using System.Reflection;3
 using System.ComponentModel;
using System.ComponentModel;4

5
 namespace Relaction.Attribute_in_member_field
namespace Relaction.Attribute_in_member_field6


 {
{7
 public interface IFieldShow
    public interface IFieldShow8

 
     {
{9
 string Show(object o);
        string Show(object o);10
 }
    }11

 /**//// <summary>
    /**//// <summary>12
 /// 可用于字段或属性的自定义属性。
    /// 可用于字段或属性的自定义属性。13
 /// </summary>
    /// </summary>14
 ///
    /// 15
 [AttributeUsage(AttributeTargets.Field|AttributeTargets.Property,Inherited = false,AllowMultiple=false)]
    [AttributeUsage(AttributeTargets.Field|AttributeTargets.Property,Inherited = false,AllowMultiple=false)]16
 public class FieldShowAttribute:Attribute
    public class FieldShowAttribute:Attribute17

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

 
         {
{22
 get
            get23

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

 
         {
{29
 get
            get 30

 
             {
{31
 return _showclass;
                return _showclass;32
 }
            }33
 }
        }34
 public FieldShowAttribute(bool show,Type showclass)
        public FieldShowAttribute(bool show,Type showclass)35

 
         {
{36
 _show = show;
            _show = show;37
 _showclass = showclass;
            _showclass = showclass;38
 if(_showclass == null)
            if(_showclass == null)39

 
             {
{40
 throw(new Exception("出错!"));
                throw(new Exception("出错!"));41
 }
            }42
 }
        }43
 public IFieldShow CreateShowInstance()
        public IFieldShow CreateShowInstance()44

 
         {
{45
 object o ;
            object o ;46

 ConstructorInfo cinfo = _showclass.GetConstructor(new Type[]
            ConstructorInfo cinfo = _showclass.GetConstructor(new Type[] {});
{});47
 o = cinfo.Invoke(null);
            o = cinfo.Invoke(null);48
 return (IFieldShow)o;
            return (IFieldShow)o;49
 }
        }50
 }
    }51
 }
}测试上面的属性:

 测试上面的属性
测试上面的属性1
 using System;
using System;2

3
 namespace Relaction.Attribute_in_member_field
namespace Relaction.Attribute_in_member_field4


 {
{5

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

 
     {
{10
 public StringFieldShow()
        public StringFieldShow()11

 
         {
{12
 }
        }13

 IFieldShow 成员#region IFieldShow 成员
        IFieldShow 成员#region IFieldShow 成员14

15
 public string Show(object o)
        public string Show(object o)16

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

20
 #endregion
        #endregion21
 }
    }22
 public class AttrFieldTest
    public class AttrFieldTest23

 
     {
{24
 [FieldShow(true,typeof(StringFieldShow))]
        [FieldShow(true,typeof(StringFieldShow))]25
 public int _num = 0;
        public int _num = 0;26

27
 public AttrFieldTest()
        public AttrFieldTest()28

 
         {
{29
 }
        }30
 }
    }31
 }
}测试:

 测试
测试1
 private void button3_Click(object sender, System.EventArgs e)
    private void button3_Click(object sender, System.EventArgs e)2

 
         {
{3

 AttrFieldTest[] o = new AttrFieldTest[]
            AttrFieldTest[] o = new AttrFieldTest[] {new AttrFieldTest(),new AttrFieldTest()};
{new AttrFieldTest(),new AttrFieldTest()};4
 o[0]._num = 15;
            o[0]._num = 15;5
 o[1]._num = 30;
            o[1]._num = 30;6
 foreach(AttrFieldTest item in o)
            foreach(AttrFieldTest item in o)7

 
             {
{8
 foreach(FieldInfo field in item.GetType().GetFields())
                foreach(FieldInfo field in item.GetType().GetFields())9

 
                 {
{10
 FieldShowAttribute[] fattr = (FieldShowAttribute[])field.GetCustomAttributes(typeof(FieldShowAttribute),false);
                  FieldShowAttribute[] fattr = (FieldShowAttribute[])field.GetCustomAttributes(typeof(FieldShowAttribute),false);11
 if(fattr.Length != 0)
                    if(fattr.Length != 0)12

 
                     {
{13
 label1.Text += field.Name+fattr[0].CreateShowInstance().Show(field.GetValue(item));
                        label1.Text += field.Name+fattr[0].CreateShowInstance().Show(field.GetValue(item));14
 }
                    }15
 }
                }16
 }
            }17
 }
        }也可以用表态的:

 修改后的FieldShowAttribute
修改后的FieldShowAttribute1
 using System;
using System;2
 using System.Reflection;
using System.Reflection;3
 using System.ComponentModel;
using System.ComponentModel;4

5
 namespace Relaction.Attribute_in_member_field
namespace Relaction.Attribute_in_member_field6


 {
{7
 public interface IFieldShow
    public interface IFieldShow8

 
     {
{9
 string Show(object o);
        string Show(object o);10
 }
    }11

 /**//// <summary>
    /**//// <summary>12
 /// 可用于字段或属性的自定义属性。
    /// 可用于字段或属性的自定义属性。13
 /// </summary>
    /// </summary>14
 ///
    /// 15
 [AttributeUsage(AttributeTargets.Field|AttributeTargets.Property,Inherited = false,AllowMultiple=false)]
    [AttributeUsage(AttributeTargets.Field|AttributeTargets.Property,Inherited = false,AllowMultiple=false)]16
 public class FieldShowAttribute:Attribute
    public class FieldShowAttribute:Attribute17

 
     {
{18
 public FieldShowAttribute(bool show,Type showclass)
        public FieldShowAttribute(bool show,Type showclass)19

 
         {
{20
 _show = show;
            _show = show;21
 _showclass = showclass;
            _showclass = showclass;22
 if(_showclass == null)
            if(_showclass == null)23

 
             {
{24
 throw(new Exception("出错!"));
                throw(new Exception("出错!"));25
 }
            }26
 }
        }27

28

29
 public bool Show
        public bool Show30

 
         {
{31
 get
            get32

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

37
 public Type ShowClass
        public Type ShowClass38

 
         {
{39
 get
            get 40

 
             {
{41
 return _showclass;
                return _showclass;42
 }
            }43
 }private Type _showclass = null;
        }private Type _showclass = null;44
 
    45
 public IFieldShow CreateShowInstance()
        public IFieldShow CreateShowInstance()46

 
         {
{47
 object o ;
            object o ;48

 ConstructorInfo cinfo = _showclass.GetConstructor(new Type[]
            ConstructorInfo cinfo = _showclass.GetConstructor(new Type[] {});
{});49
 o = cinfo.Invoke(null);
            o = cinfo.Invoke(null);50
 return (IFieldShow)o;
            return (IFieldShow)o;51
 }
        }52
 public static string ShowValue(FieldShowAttribute attr,object o,FieldInfo valueobject)
        public static string ShowValue(FieldShowAttribute attr,object o,FieldInfo valueobject)53

 
         {
{54
 MethodInfo mi = attr.ShowClass.GetMethod("Show",BindingFlags.Static|BindingFlags.Public);
            MethodInfo mi = attr.ShowClass.GetMethod("Show",BindingFlags.Static|BindingFlags.Public);55

 return (string)mi.Invoke(null,new object[]
            return (string)mi.Invoke(null,new object[] {valueobject.GetValue(o)});
{valueobject.GetValue(o)});56
 }
        }57
 }
    }58
 }
}59


 修改后贴上FieldShowAttribute类
修改后贴上FieldShowAttribute类1
 using System;
using System;2

3
 namespace Relaction.Attribute_in_member_field
namespace Relaction.Attribute_in_member_field4


 {
{5

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

 
     {
{10
 public StringFieldShow()
        public StringFieldShow()11

 
         {
{12
 }
        }13

 IFieldShow 成员#region IFieldShow 成员
        IFieldShow 成员#region IFieldShow 成员14

15
 public string Show(object o)
        public string Show(object o)16

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

20
 #endregion
        #endregion21
 }
    }22
 public class StringFieldShow2
    public class StringFieldShow223

 
     {
{24
 public static string Show(object o)
        public static string Show(object o)25

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

 
     {
{31
 [FieldShow(true,typeof(StringFieldShow2))]
        [FieldShow(true,typeof(StringFieldShow2))]32
 public int _num = 0;
        public int _num = 0;33

34
 public AttrFieldTest()
        public AttrFieldTest()35

 
         {
{36
 }
        }37
 }
    }38
 }
}39


 客户调用
客户调用1
 private void button4_Click(object sender, System.EventArgs e)
    private void button4_Click(object sender, System.EventArgs e)2

 
         {
{3

 AttrFieldTest[] o = new AttrFieldTest[]
            AttrFieldTest[] o = new AttrFieldTest[] {new AttrFieldTest(),new AttrFieldTest()};
{new AttrFieldTest(),new AttrFieldTest()};4
 o[0]._num = 15;
            o[0]._num = 15;5
 o[1]._num = 30;
            o[1]._num = 30;6
 foreach(AttrFieldTest item in o)
            foreach(AttrFieldTest item in o)7

 
             {
{8
 foreach(FieldInfo field in item.GetType().GetFields())
                foreach(FieldInfo field in item.GetType().GetFields())9

 
                 {
{10
 FieldShowAttribute[] fattr = (FieldShowAttribute[])field.GetCustomAttributes(typeof(FieldShowAttribute),false);
                    FieldShowAttribute[] fattr = (FieldShowAttribute[])field.GetCustomAttributes(typeof(FieldShowAttribute),false);11
 if(fattr.Length != 0)
                    if(fattr.Length != 0)12

 
                     {
{13
 label1.Text += FieldShowAttribute.ShowValue(fattr[0],item,field);
                        label1.Text += FieldShowAttribute.ShowValue(fattr[0],item,field);14
 }
                    }15
 }
                }16
 }
            }17
 }
        }.jpg) 
  
 
                    
                     
                    
                 
                    
                
 
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号