博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Programming Concepts
阅读量:6275 次
发布时间:2019-06-22

本文共 4698 字,大约阅读时间需要 15 分钟。

Attributes provide a powerful method of associating metadata, or declarative information, with code (assemblies, types, methods, properties, and so forth).

After an attribute is associated with a program entity, the attribute can be queried at run time by using a technique called reflection.
For more information, see .

Attributes have the following properties:

  • Attributes add metadata to your program. Metadata is information about the types defined in a program. All .NET assemblies contain a specified set of metadata that describes the types and type members defined in the assembly. You can add custom attributes to specify any additional information that is required. For more information, see, .
  • You can apply one or more attributes to entire assemblies, modules, or smaller program elements such as classes and properties.
  • Attributes can accept arguments in the same way as methods and properties.
  • Your program can examine its own metadata or the metadata in other programs by using reflection. For more information, see .

Using Attributes 

Attributes can be placed on most any declaration, though a specific attribute might restrict the types of declarations on which it is valid.

In C#, you specify an attribute by placing the name of the attribute, enclosed in square brackets ([]), above the declaration of the entity to which it applies.
In Visual Basic, an attribute is enclosed in angle brackets (< >).
It must appear immediately before the element to which it is applied, on the same line.

 

In this example, the SerializableAttribute attribute is used to apply a specific characteristic to a class:

using System; [Serializable]    class SampleClass    {        // Objects of this type can be serialized.    }

 

A method with the attribute DllImportAttribute is declared like this:

using System.Runtime.InteropServices;        [DllImport("user32.dll")]        extern static void SampleMethod();

 

More than one attribute can be placed on a declaration:

public sealed class InAttribute : Attribute

public sealed class OutAttribute : Attribute

using System.Runtime.InteropServices;        void MethodA([In][Out] ref double x) { }        void MethodB([Out][In] ref double x) { }        void MethodC([In, Out] ref double x) { }

 

Some attributes can be specified more than once for a given entity. An example of such a multiuse attribute is ConditionalAttribute:

using System.Diagnostics;[Conditional("DEBUG"), Conditional("TEST1")]        void TraceMethod() { }

 

By convention, all attribute names end with the word "Attribute" to distinguish them from other items in the .NET Framework.

However, you do not need to specify the attribute suffix when using attributes in code.

For example, [DllImport] is equivalent to [DllImportAttribute], but DllImportAttribute is the attribute's actual name in the .NET Framework.

 

 

Reflection provides objects (of type ) that describe assemblies, modules and types.

You can use reflection to dynamically create an instance of a type, bind the type to an existing object, or get the type from an existing object and invoke its methods or access its fields and properties.
If you are using attributes in your code, reflection enables you to access them.
For more information, see .

 

Here's a simple example of reflection using the static method GetType - inherited by all types from the Object base class - to obtain the type of a variable:

// Using GetType to obtain type information: 

 

using System;int i = 0;Type type = i.GetType();Console.WriteLine(type);

输出结果:System.Int32

 

 

The following example uses reflection to obtain the full name of the loaded assembly.

using System;using System.Reflection;            Type type = typeof(int);            Assembly assembly = type.Assembly;            Console.WriteLine(assembly);            int i = 0;            type = i.GetType();            assembly = type.Assembly;            Console.WriteLine(assembly);

输出结果:

mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

 

The C# keywords protected and internal have no meaning in IL and are not used in the reflection APIs.

The corresponding terms in IL are Family and Assembly.
To identify an internal method using reflection, use the property.
To identify a protected internal method, use the .

 

Reflection Overview

Reflection is useful in the following situations:

When you have to access attributes in your program's metadata. For more information, see .
For examining and instantiating types in an assembly.
For building new types at runtime. Use classes in .
For performing late binding, accessing methods on types created at run time. See the topic .

 

Related Sections

 

转载地址:http://jngpa.baihongyu.com/

你可能感兴趣的文章
vue part3.3 小案例ajax (axios) 及页面异步显示
查看>>
软件测试(二)之 Failure, Error & Fault
查看>>
浅谈MVC3自定义分页
查看>>
.net中ashx文件有什么用?功能有那些,一般用在什么情况下?
查看>>
select、poll、epoll之间的区别总结[整理]【转】
查看>>
CSS基础知识(上)
查看>>
PHP中常见的面试题2(附答案)
查看>>
26.Azure备份服务器(下)
查看>>
mybatis学习
查看>>
LCD的接口类型详解
查看>>
C#中使用RabbitMQ收发队列消息
查看>>
Spring Boot Unregistering JMX-exposed beans on shutdown
查看>>
poi 导入导出的api说明(大全)
查看>>
说说自己对RESTful API的理解s
查看>>
Mono for Android 优势与劣势
查看>>
将图片转成base64字符串并在JSP页面显示的Java代码
查看>>
js 面试题
查看>>
sqoop数据迁移(基于Hadoop和关系数据库服务器之间传送数据)
查看>>
腾讯云下安装 nodejs + 实现 Nginx 反向代理
查看>>
magento2-- 理解自动加载
查看>>