介绍

标题中所说的三个特性 CallerMemberNameAttribute / CallerFilePathAttribute / CallerLineNumberAttribute 我们统称为调用者信息特性,正常情况下在 .NET Framework 4.0 中是无法使用的。因为这三个特性是 .NET Framework 4.5 中新增的。然而这三个特性的作用只是请求编译器在编译过程中进行代码的转换。

使用示例

    static void Main( string[] args )
{
var productInfo = new ProductInfo(); productInfo.Name = "lumia"; productInfo.PropertyChanged(); Console.ReadKey( true );
}
} public class ProductInfo
{
private string _name; public string Name
{
get { return this._name; }
set
{
this._name = value;
this.PropertyChanged();
}
} public void PropertyChanged([CallerMemberName]string name = "", [CallerLineNumber]int line = , [CallerFilePath]string file = "")
{
Console.WriteLine("------------------------------------------------");
Console.WriteLine($"Name : {name}, \nLine : {line}, \nPath : {file}");
}
}

注意上面标为橘红色的语句。运行时将自动填充这三个可选参数的值。开发过 WPF 的同学都知道这是多么的方便,不用显示指定参数名称。然而入我上面所说他不能在 .NET Framework 4.0 中使用,不过还好有办法。很简单,我们只需要自己定义这三个特性就可以了,代码如下。

namespace System.Runtime.CompilerServices
{
[AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
public class CallerMemberNameAttribute : Attribute
{ } [AttributeUsage(AttributeTargets.Parameter, Inherited = false )]
public class CallerFilePathAttribute : Attribute
{ } [AttributeUsage(AttributeTargets.Parameter, Inherited = false )]
public class CallerLineNumberAttribute : Attribute
{ }
}

没有什么参数和成员在这三个特性里面。但要注意命名空间一定要与上面的一样。

在 .NET 4.0 中使用 .NET 4.5 中新增的特性(CallerMemberNameAttribute/CallerFilePathAttribute/CallerLineNumberAttribute)的更多相关文章

  1. NET 4.5 中新增的特性调用者信息特性CallerMemberNameAttribute/CallerFilePathAttribute/CallerLineNumberAttribute

    标题中所说的三个特性 CallerMemberNameAttribute / CallerFilePathAttribute / CallerLineNumberAttribute 我们统称为调用者信 ...

  2. 在win7-64bit环境下,boa-constructor 0.6.1 的palette面板中没有控件图标的解决方法

    在win7-64bit环境下,boa-constructor 0.6.1 的palette面板中没有控件图标,空白一片.将面板窗口拉大,发现那些图标在很下面的位置,X轴的排列与正常状态一致. 软件环境 ...

  3. 《转》在win7,boa-constructor 0.6.1 的palette面板中没有控件图标的解决方法

    原地址:http://blog.csdn.net/rickleo/article/details/6532595 在win7-64bit环境下,boa-constructor 0.6.1 的palet ...

  4. C# 6.0:在catch和finally中使用await

    Asyn方法是一个现在很常用的方法,当使用async和await时,你或许曾有这样的经历,就是你想要在catch块或finally块中使用它们,比如当出现一个exception而你希望将日志记在文件或 ...

  5. Python:判断文本中的用户名在数据库中是否存在,存在返回1,不存在返回0

    下面是我写的python的一个小脚本,作用是:判断文本中的用户名在数据库中是否存在,存在返回1,不存在返回0.用的是MySQL数据库. 要注意的是:strip函数的使用,该函数的作用是去除字符串两端多 ...

  6. windows10下安装mysql-8.0.15-winx64以及连接服务器过程中遇到的一些问题

    一.下载安装配置mysql-8.0.15 1.官网(https://dev.mysql.com/downloads/mysql/)下载zip包 2.解包到我的D:\mysql目录下 3.为mysql配 ...

  7. 0.11内核rd_load@ramdisk.c中memcpy函数好像有bug

    0.11内核rd_load@ramdisk.c中memcpy函数好像有bug,如:#define memcpy(dst,src,n) \    __asm__("cld;rep;movsl& ...

  8. Spring 注解驱动(二)Servlet 3.0 注解驱动在 Spring MVC 中的应用

    Spring 注解驱动(二)Servlet 3.0 注解驱动在 Spring MVC 中的应用 Spring 系列目录(https://www.cnblogs.com/binarylei/p/1019 ...

  9. find中的-print0和xargs中-0的奥妙【转】

    find cygnus/firmware_cygnus/target/linux/brcm5830/files/arch/arm/mach-iproc/pm_iproc/ -name "*. ...

随机推荐

  1. MongoDB的下载与安装

      MongoDB的下载与安装 一.简介 MongoDB 是一个基于分布式文件存储的数据库.由 C++ 语言编写.旨在为 WEB 应用提供可扩展的高性能数据存储解决方案.MongoDB 是一个介于关系 ...

  2. Nuget 常用命令

    Update-Package -ProjectName 'NLog' -Reinstall 主题 about_NuGet 简短说明 提供有关 NuGet 程序包管理器命令的信息. 详细说明 本主题介绍 ...

  3. POSTMAN发起请求收到乱码 http 406错误

    web前段异常: The resource identified by this request is only capable of generating responses with charac ...

  4. 用直接路径(direct-path)insert提升性能的两种方法

    1.传统串行insert方式 常见的insert方式有两种: (1) insert into table_name values(....) (2) insert into target_table ...

  5. 【java】分页查询实体类

    package com.dmsd.itoo.tool.pageModel; import java.io.Serializable; import java.util.HashMap; import ...

  6. 【转】apache DateFormatUtils 与 DateUtils 的使用

    在Apache Commons项目的Lang里面,有两个类:DateUtils和DateFormatUtils,专门用于处理时间日期转换.它们在 org.apache.commons.lang.tim ...

  7. super语句

    1.super是对当前对象的父类对象的默认引用, 2.super必须出现在子类中(方法或构造函数),而不是其他位置 3.super无法访问private成员属性和方法

  8. 莫名其妙MyEclipse

    MyEclipse Enterprise Workbench Version: 2015 Stable 2.0Build id: 13.0.0-20150518 整合SS时,\WEB-INF\clas ...

  9. RabbitMQ消息机制广播分发

    public static void SendMessage() { var factory = new ConnectionFactory(); factory.HostName = "1 ...

  10. c#查找string数组的某一个值的索引

    string[] array = { "A","B","C","D","H"};   var ind ...