服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - C#基础学习系列之Attribute和反射详解

C#基础学习系列之Attribute和反射详解

2022-01-21 13:55Boyd Wang C#

大家在使用Attribute的时候大多需要用到反射,所以放在一起。下面这篇文章主要给大家介绍了关于C#基础学习系列之Attribute和反射的相关资料,文中给出了详细的示例代码供大家参考学习,需要的朋友们下面随着小编来一起学习学习

前言

本文主要给大家介绍了关于c#基础之attribute和反射的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。

attribute(特性)

attribute是c#的一种语言特性,用于为各种实体(class,field,property)附加一些说明性信息, 并且可以在运行时环境中检索这些信息(通过反射)。

所有的attribute必须继承自attribute类,按照约定,特性类的名称带有 attribute 后缀。使用特性时可以包含或省略此后缀。

attributeusage

attributeusage是attribute的attribute,用于给自定义的attribute加一些限定。

  • attributetargets
  • allowmultiple
  • inherited

attributetargets指定你这个attribute限制用于哪类实体上,在这里,实体是指: class、method、constructor、field、property、genericparameter或者用all,表明可用于所有实体。每个target标记可以用|链接,如attributetargets.class|attributetargets.method表示这个attribute可用于class或者method。

下面例子表明了每种target的用法:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
using system;
 
namespace atttargscs {
 // this attribute is only valid on a class.
 [attributeusage(attributetargets.class)]
 public class classtargetattribute : attribute {
 }
 
 // this attribute is only valid on a method.
 [attributeusage(attributetargets.method)]
 public class methodtargetattribute : attribute {
 }
 
 // this attribute is only valid on a constructor.
 [attributeusage(attributetargets.constructor)]
 public class constructortargetattribute : attribute {
 }
 
 // this attribute is only valid on a field.
 [attributeusage(attributetargets.field)]
 public class fieldtargetattribute : attribute {
 }
 
 // this attribute is valid on a class or a method.
 [attributeusage(attributetargets.class|attributetargets.method)]
 public class classmethodtargetattribute : attribute {
 }
 
 // this attribute is valid on a generic type parameter.
 [attributeusage(attributetargets.genericparameter)]
 public class genericparametertargetattribute : attribute {
 }
 
 // this attribute is valid on any target.
 [attributeusage(attributetargets.all)]
 public class alltargetsattribute : attribute {
 }
 
 [classtarget]
 [classmethodtarget]
 [alltargets]
 public class testclassattribute {
  [constructortarget]
  [alltargets]
  testclassattribute() {
  }
 
  [methodtarget]
  [classmethodtarget]
  [alltargets]
  public void method1() {
  }
 
  [fieldtarget]
  [alltargets]
  public int myint;
 
  public void genericmethod<
   [genericparametertarget, alltargets] t>(t x) {
  }
 
  static void main(string[] args) {
  }
 }
}

allowmultiple

allowmultiple表明了这个attribute可否多次应用于同一个实体,默认为false

?
1
2
3
4
5
[attributeusage(attributetargets.class, allowmultiple = true)]
class multiuseattr : attribute { }
 
[multiuseattr, multiuseattr]
class class2 { }

inherited

inherited表明这个attribute是否可以被继承传递,即子类或子类从父类继承的成员是否带这个attribute,默认为true

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
[attributeusage(attributetargets.class | attributetargets.method |
    attributetargets.property | attributetargets.field,
    inherited = true)]
public class inheritedattribute : attribute
{}
 
[attributeusage(attributetargets.class | attributetargets.method |
    attributetargets.property | attributetargets.field,
    inherited = false)]
public class notinheritedattribute : attribute
{}
 
using system;
using system.reflection;
 
[inheritedattribute]
public class basea
{
 [inheritedattribute]
 public virtual void methoda()
 {}
}
 
public class deriveda : basea
{
 public override void methoda()
 {}
}
 
[notinheritedattribute]
public class baseb
{
 [notinheritedattribute]
 public virtual void methodb()
 {}
}
 
public class derivedb : baseb
{
 public override void methodb()
 {}
}
 
public class example
{
 public static void main()
 {
  type typea = typeof(deriveda);
  console.writeline("deriveda has inherited attribute: {0}",
      typea.getcustomattributes(typeof(inheritedattribute), true).length > 0);
  methodinfo membera = typea.getmethod("methoda");
  console.writeline("deriveda.membera has inherited attribute: {0}\n",
      membera.getcustomattributes(typeof(inheritedattribute), true).length > 0);
 
  type typeb = typeof(derivedb);
  console.writeline("derivedb has inherited attribute: {0}",
      typeb.getcustomattributes(typeof(inheritedattribute), true).length > 0);
  methodinfo memberb = typeb.getmethod("methodb");
  console.writeline("derivedb.memberb has inherited attribute: {0}",
      memberb.getcustomattributes(typeof(inheritedattribute), true).length > 0);
 }
}
// the example displays the following output:
//  deriveda has inherited attribute: true
//  deriveda.membera has inherited attribute: true
// 
//  derivedb has inherited attribute: false
//  derivedb.memberb has inherited attribute: false

反射

reflection,中文翻译为反射,是审查元数据并收集关于它的类型信息的能力。元数据(编译以后的最基本数据单元)就是一大堆的表,当编译程序集或者模块时,编译器会创建一个类定义表,一个字段定义表,和一个方法定义表等。

反射是.net中获取运行时类型信息的方式,.net的应用程序由几个部分:‘程序集(assembly)'、‘模块(module)'、‘类型(class)'组成,而反射提供一种编程的方式,让程序员可以在程序运行期获得这几个组成部分的相关信息, assemblies contain modules. modules contain classes. classes contain functions.

C#基础学习系列之Attribute和反射详解

system.reflection命名空间包含的几个类,允许你反射(解析)这些元数据表的代码

?
1
2
3
4
5
6
7
8
9
system.reflection.assembly
system.reflection.memberinfo
system.reflection.eventinfo
system.reflection.fieldinfo
system.reflection.methodbase
system.reflection.constructorinfo
system.reflection.methodinfo
system.reflection.propertyinfo
system.type

C#基础学习系列之Attribute和反射详解

以下是上面几个类的使用方法:

  • 使用assembly定义和加载程序集,加载在程序集清单中列出模块,以及从此程序集中查找类型并创建该类型的实例。
  • 使用module了解包含模块的程序集以及模块中的类等,还可以获取在模块上定义的所有全局方法或其他特定的非全局方法。
  • 使用constructorinfo了解构造函数的名称、参数、访问修饰符(如pulic 或private)和实现详细信息(如abstract或virtual)等。
  • 使用type的getconstructors或 getconstructor方法来调用特定的构造函数。
  • 使用methodinfo了解方法的名称、返回类型、参数、访问修饰符(如pulic 或private)和实现详细信息(如abstract或virtual)等。
  • 使用type的getmethods或getmethod方法来调用特定的方法。
  • 使用fiedinfo了解字段的名称、访问修饰符(如public或private)和实现详细信息(如static)等,并获取或设置字段值。
  • 使用eventinfo了解事件的名称、事件处理程序数据类型、自定义属性、声明类型和反射类型等,添加或移除事件处理程序。
  • 使用propertyinfo了解属性的名称、数据类型、声明类型、反射类型和只读或可写状态等,获取或设置属性值。
  • 使用parameterinfo了解参数的名称、数据类型、是输入参数还是输出参数,以及参数在方法签名中的位置等。

反射的作用:

  • 可以使用反射动态地创建类型的实例,将类型绑定到现有对象,或从现有对象中获取类型
  • 应用程序需要在运行时从某个特定的程序集中载入一个特定的类型,以便实现某个任务时可以用到反射。

使用反射获取类型

?
1
2
3
4
5
6
public void process(object processobj)
{
type t = processsobj.gettype();
if(t.getinterface(“itest”) !=null)
     
}

创建一个对象

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class testclass {
   private string _value;
   public testclass() {
   }
   public testclass(string value) {
    _value = value;
   }
   public string getvalue( string prefix ) {
   if( _value==null )
    return "null";
   else
     return prefix+" : "+_value;
   }
 
//获取类型信息
type t = type.gettype("testspace.testclass");
//构造器的参数
object[] constuctparms = new object[]{"timmy"};
//根据类型创建对象
object dobj = activator.createinstance(t,constuctparms);
//获取方法的信息
methodinfo method = t.getmethod("getvalue");
//调用方法的一些标志位,这里的含义是public并且是实例方法,这也是默认的值
bindingflags flag = bindingflags.public | bindingflags.instance;
//getvalue方法的参数
object[] parameters = new object[]{"hello"};
//调用方法,用一个object接收返回值
object returnvalue = method.invoke(dobj,flag,type.defaultbinder,parameters,null);

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对服务器之家的支持。

原文链接:http://www.boydwang.com/2017/09/attribute-and-reflection/

延伸 · 阅读

精彩推荐
  • C#Unity3D UGUI实现缩放循环拖动卡牌展示效果

    Unity3D UGUI实现缩放循环拖动卡牌展示效果

    这篇文章主要为大家详细介绍了Unity3D UGUI实现缩放循环拖动展示卡牌效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参...

    诗远3662022-03-11
  • C#C#直线的最小二乘法线性回归运算实例

    C#直线的最小二乘法线性回归运算实例

    这篇文章主要介绍了C#直线的最小二乘法线性回归运算方法,实例分析了给定一组点,用最小二乘法进行线性回归运算的实现技巧,具有一定参考借鉴价值,需要...

    北风其凉8912021-10-18
  • C#C# 后台处理图片的几种方法

    C# 后台处理图片的几种方法

    本篇文章主要介绍了C# 后台处理图片的几种方法,非常具有实用价值,需要的朋友可以参考下。...

    IT小伙儿10162021-12-08
  • C#C#基础之泛型

    C#基础之泛型

    泛型是 2.0 版 C# 语言和公共语言运行库 (CLR) 中的一个新功能。接下来通过本文给大家介绍c#基础之泛型,感兴趣的朋友一起学习吧...

    方小白7732021-12-03
  • C#聊一聊C#接口问题 新手速来围观

    聊一聊C#接口问题 新手速来围观

    聊一聊C#接口问题,新手速来围观,一个通俗易懂的例子帮助大家更好的理解C#接口问题,感兴趣的小伙伴们可以参考一下...

    zenkey7072021-12-03
  • C#c#学习之30分钟学会XAML

    c#学习之30分钟学会XAML

    一个界面程序的核心,无疑就是界面和后台代码,而xaml就是微软为构建应用程序界面而创建的一种描述性语言,也就是说,这东西是搞界面的...

    C#教程网8812021-12-10
  • C#C#实现的文件操作封装类完整实例【删除,移动,复制,重命名】

    C#实现的文件操作封装类完整实例【删除,移动,复制,重命名】

    这篇文章主要介绍了C#实现的文件操作封装类,结合完整实例形式分析了C#封装文件的删除,移动,复制,重命名等操作相关实现技巧,需要的朋友可以参考下...

    Rising_Sun3892021-12-28
  • C#浅谈C# winForm 窗体闪烁的问题

    浅谈C# winForm 窗体闪烁的问题

    下面小编就为大家带来一篇浅谈C# winForm 窗体闪烁的问题。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    C#教程网7962021-12-21