Programming Concepts
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 Reflection (C# and Visual Basic).
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, Creating Custom Attributes (C# and Visual Basic).
- 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 Accessing Attributes by Using Reflection (C# and Visual Basic).
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 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 Extending Metadata Using Attributes.
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 = ;
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 = ;
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 IsAssembly property.
To identify a protected internal method, use the IsFamilyOrAssembly.
Reflection Overview
Reflection is useful in the following situations:
When you have to access attributes in your program's metadata. For more information, see Retrieving Information Stored in Attributes.
For examining and instantiating types in an assembly.
For building new types at runtime. Use classes in System.Reflection.Emit.
For performing late binding, accessing methods on types created at run time. See the topic Dynamically Loading and Using Types.
Related Sections
Reflection in the .NET Framework
Viewing Type Information
Reflection and Generic Types
System.Reflection.Emit
Retrieving Information Stored in Attributes
Programming Concepts的更多相关文章
- Introduction to Multi-Threaded, Multi-Core and Parallel Programming concepts
https://katyscode.wordpress.com/2013/05/17/introduction-to-multi-threaded-multi-core-and-parallel-pr ...
- Important Programming Concepts (Even on Embedded Systems) Part V: State Machines
Earlier articles in this series: Part I: Idempotence Part II: Immutability Part III: Volatility Part ...
- Introduction to ASP.NET Web Programming Using the Razor Syntax (C#)
1, http://www.asp.net/web-pages/overview/getting-started/introducing-razor-syntax-c 2, Introduction ...
- DCI:The DCI Architecture: A New Vision of Object-Oriented Programming
SummaryObject-oriented programming was supposed to unify the perspectives of the programmer and the ...
- C++11: Multi-core Programming – PPL Parallel Aggregation Explained
https://katyscode.wordpress.com/2013/08/17/c11-multi-core-programming-ppl-parallel-aggregation-expla ...
- C语言学习书籍推荐《Practical C++ Programming》下载
下载链接 :点我 C++ is a powerful, highly flexible, and adaptable programming language that allows software ...
- (转) [it-ebooks]电子书列表
[it-ebooks]电子书列表 [2014]: Learning Objective-C by Developing iPhone Games || Leverage Xcode and Obj ...
- NVMe over Fabrics又让RDMA技术火了一把
RDMA是个什么鬼?相信大部分不关心高性能网络的童鞋都不太了解.但是NVMe over Fabrics的出现让搞存储的不得不抽出时间来看看这个东西,这篇文章就来介绍下我所了解的RDMA. RDMA(R ...
- Unix调试工具dbx使用方法
dbx 命令 用途 提供了一个调试和运行程序的环境. 语法 dbx [ -a ProcessID ] [ -c CommandFile ] [ -d NestingDepth ] [ -I Dire ...
随机推荐
- T-SQL基础 (子查询,连接查询,交叉查询,事务|| 笔记0807)
一: A.子查询: 1.select 字段名 from table where 字段名=(select 字段名 from table 条件) //只能做1个匹配 2.select 字段名 from ...
- DotNet Core 之旅(一)
1.下载安装 DotNetCore.1.0.0-SDK.Preview2-x64.exe 下载链接:https://www.microsoft.com/net/download ps:如果有vs201 ...
- Android no such table (找不到表)
今天在学习项目时,一直报错no such table Users.说找不到Users表.我就纳闷了,数据库是复制过去的,检查表名也没有写错.这是怎么回事呢?检查了半天才发现原来是数据库的路径错了. 我 ...
- store procedure 翻页
store procedure 翻页例子 .turn page CREATE PROCEDURE pageTest --用于翻页的测试 --需要把排序字段放在第一列 ( )=null, --当前页面里 ...
- Ubuntu 12.04安装PPTP
1.安装软件 sudo apt-get install pptpd ufw 2.编辑/etc/ppp/pptpd-options 找到 refuse-pap refuse-chap refuse-ms ...
- python常用函数 库 转
可能经常用到的标准模块和第三方常用的50个库 本文由python培训班授课老师整理 数学计算: numbers - Numeric abstract base classes math ...
- c# 串口发送接收数据
/********************** 串口数据接收事件 *****************************/ private void SerialPort_DataReceived ...
- 反射 介绍System.Type类
本节先介绍system.Type类,通过这个类可以访问关于任何数据类型的信息. 1. system.Type类以前把Type看作一个类,但它实际上是一个抽象的基类.只要实例化了一个Type对象,实际上 ...
- ipod nano 无法添加mp4视频 电影失败解决方法
我的是nano7. 导入mp4各种错误, 同步资料库 无效等等方法都没用. 后来发现当中 多个mp4,少年pi.mp4竟然导入成功, 怀疑是mp4格式不符合nano 于是:(测试后成功) 先拉到资料库 ...
- 24种设计模式--观察者模式【Observer Pattern】
<孙子兵法>有云: “知彼知己,百战不殆:不知彼而知己,一胜一负:不知彼,不知己,每战必殆”,那怎么才能知己知彼呢?知己是很容易的,自己的军队嘛,很容易知道,那怎么知彼呢?安插间谍是很好 ...