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

 更新时间:2020年6月25日 11:18  点击:1747

前言

本文主要给大家介绍了关于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的用法:

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

[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)] 
class MultiUseAttr : Attribute { } 
 
[MultiUseAttr, MultiUseAttr] 
class Class2 { } 

Inherited

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

[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.

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

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


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

  • 使用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了解参数的名称、数据类型、是输入参数还是输出参数,以及参数在方法签名中的位置等。

反射的作用:

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

使用反射获取类型

public void Process(object processObj)
{
Type t = processsObj.GetType();
if(t.GetInterface(“ITest”) !=null)
     …
}

创建一个对象

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);

总结

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

[!--infotagslink--]

相关文章

  • C#实现简单的登录界面

    我们在使用C#做项目的时候,基本上都需要制作登录界面,那么今天我们就来一步步看看,如果简单的实现登录界面呢,本文给出2个例子,由简入难,希望大家能够喜欢。...2020-06-25
  • 浅谈C# 字段和属性

    这篇文章主要介绍了C# 字段和属性的的相关资料,文中示例代码非常详细,供大家参考和学习,感兴趣的朋友可以了解下...2020-11-03
  • C#中截取字符串的的基本方法详解

    这篇文章主要介绍了C#中截取字符串的的基本方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-11-03
  • C#实现简单的Http请求实例

    这篇文章主要介绍了C#实现简单的Http请求的方法,以实例形式较为详细的分析了C#实现Http请求的具体方法,需要的朋友可以参考下...2020-06-25
  • C#连接SQL数据库和查询数据功能的操作技巧

    本文给大家分享C#连接SQL数据库和查询数据功能的操作技巧,本文通过图文并茂的形式给大家介绍的非常详细,需要的朋友参考下吧...2021-05-17
  • C#中new的几种用法详解

    本文主要介绍了C#中new的几种用法,具有很好的参考价值,下面跟着小编一起来看下吧...2020-06-25
  • 使用Visual Studio2019创建C#项目(窗体应用程序、控制台应用程序、Web应用程序)

    这篇文章主要介绍了使用Visual Studio2019创建C#项目(窗体应用程序、控制台应用程序、Web应用程序),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25
  • C#开发Windows窗体应用程序的简单操作步骤

    这篇文章主要介绍了C#开发Windows窗体应用程序的简单操作步骤,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-04-12
  • C#从数据库读取图片并保存的两种方法

    这篇文章主要介绍了C#从数据库读取图片并保存的方法,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下...2021-01-16
  • C#和JavaScript实现交互的方法

    最近做一个小项目不可避免的需要前端脚本与后台进行交互。由于是在asp.net中实现,故问题演化成asp.net中jiavascript与后台c#如何进行交互。...2020-06-25
  • C++调用C#的DLL程序实现方法

    本文通过例子,讲述了C++调用C#的DLL程序的方法,作出了以下总结,下面就让我们一起来学习吧。...2020-06-25
  • 轻松学习C#的基础入门

    轻松学习C#的基础入门,了解C#最基本的知识点,C#是一种简洁的,类型安全的一种完全面向对象的开发语言,是Microsoft专门基于.NET Framework平台开发的而量身定做的高级程序设计语言,需要的朋友可以参考下...2020-06-25
  • C#变量命名规则小结

    本文主要介绍了C#变量命名规则小结,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-09-09
  • C#绘制曲线图的方法

    这篇文章主要介绍了C#绘制曲线图的方法,以完整实例形式较为详细的分析了C#进行曲线绘制的具体步骤与相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
  • C# 中如何取绝对值函数

    本文主要介绍了C# 中取绝对值的函数。具有很好的参考价值。下面跟着小编一起来看下吧...2020-06-25
  • c#自带缓存使用方法 c#移除清理缓存

    这篇文章主要介绍了c#自带缓存使用方法,包括获取数据缓存、设置数据缓存、移除指定数据缓存等方法,需要的朋友可以参考下...2020-06-25
  • c#中(&&,||)与(&,|)的区别详解

    这篇文章主要介绍了c#中(&&,||)与(&,|)的区别详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-06-25
  • 经典实例讲解C#递归算法

    这篇文章主要用实例讲解C#递归算法的概念以及用法,文中代码非常详细,帮助大家更好的参考和学习,感兴趣的朋友可以了解下...2020-06-25
  • C#学习笔记- 随机函数Random()的用法详解

    下面小编就为大家带来一篇C#学习笔记- 随机函数Random()的用法详解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25
  • C#中list用法实例

    这篇文章主要介绍了C#中list用法,结合实例形式分析了C#中list排序、运算、转换等常见操作技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25