那类头上中括号里的像标签一样的东西是干什么用的.下面的代码是什么意思.有什么书系统详细的介绍?解释详细的90以分上

Ptrtoptr 2015-04-08 09:16:13
// Generated by .NET Reflector from C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.ComponentModel.Composition\v4.0_4.0.0.0__b77a5c561934e089\System.ComponentModel.Composition.dll
namespace System.ComponentModel.Composition
{
using System;
using System.Runtime;
using System.Runtime.CompilerServices;

[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property | AttributeTargets.Method | AttributeTargets.Class, AllowMultiple=true, Inherited=false)]
public class ExportAttribute : Attribute
{
[TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
public ExportAttribute() : this(null, null)
{
}

[TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
public ExportAttribute(string contractName) : this(contractName, null)
{
}

[TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
public ExportAttribute(Type contractType) : this(null, contractType)
{
}

public ExportAttribute(string contractName, Type contractType)
{
this.ContractName = contractName;
this.ContractType = contractType;
}

public string ContractName
{
[CompilerGenerated, TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
get
{
return this.<ContractName>k__BackingField;
}
[CompilerGenerated]
private set
{
this.<ContractName>k__BackingField = value;
}
}

public Type ContractType
{
[CompilerGenerated, TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
get
{
return this.<ContractType>k__BackingField;
}
[CompilerGenerated]
private set
{
this.<ContractType>k__BackingField = value;
}
}
}
}
...全文
340 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
john_QQ:2335298917 2015-04-11
  • 打赏
  • 举报
回复
特性,这些代码定义了一个特性,也就是attribute,同时又用attribute修饰了这个特性
Ptrtoptr 2015-04-11
  • 打赏
  • 举报
回复
引用 5 楼 tcmakebest 的回复:
贴个标签有什么用,当然只有做这个标签的人才知道, 当然还有就是看开发文档了, 看到某个地方需要用的就用.
你感觉我知道了.还来问什么?你这不是费话吗?就这个我问的问题主要是希望你能帮我解决心中的颖惑.不能的话你的回答有什么意义?
tcmakebest 2015-04-11
  • 打赏
  • 举报
回复
曾经自己也定义过属性,就是在写类定义,需要与数据库表结构应对的时候,发现有些字段名与语法冲突不能写成类成员名,那就换个名字,然后再贴个标签,在标签里记录下对应的字段名.然后在自动生成类实例的时候,通过反射取这些标签,如果有则从中取字段名,如果没有那对应的字段名就是这个成员名.
tcmakebest 2015-04-11
  • 打赏
  • 举报
回复
小时候使用字典的时候, 对每一个字会都有一大堆字来解释, 而这一大堆字中的每一个也与前面的那个字一样有类似的解释. 这不就是一个无穷死循环了么,没有必要把字典中的每个字的意思都查出来弄清楚. 对于这些属性的意义,大多数年情况下就是为了实现某个功能就需要这些属性,可以查看其英文意思领悟,其实粘贴一下就OK了. 写Webservice方法的时候, 前面总是要有一个 [WebMethod],那就复制吧.
tcmakebest 2015-04-10
  • 打赏
  • 举报
回复
贴个标签有什么用,当然只有做这个标签的人才知道, 当然还有就是看开发文档了, 看到某个地方需要用的就用.
Ptrtoptr 2015-04-09
  • 打赏
  • 举报
回复
引用 楼主 Ptrtoptr 的回复:
// Generated by .NET Reflector from C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.ComponentModel.Composition\v4.0_4.0.0.0__b77a5c561934e089\System.ComponentModel.Composition.dll
namespace System.ComponentModel.Composition
{
    using System;
    using System.Runtime;
    using System.Runtime.CompilerServices;
    
    [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property | AttributeTargets.Method | AttributeTargets.Class, AllowMultiple=true, Inherited=false)]
    public class ExportAttribute : Attribute
    {
        [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
        public ExportAttribute() : this(null, null)
        {
        }
        
        [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
        public ExportAttribute(string contractName) : this(contractName, null)
        {
        }
        
        [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
        public ExportAttribute(Type contractType) : this(null, contractType)
        {
        }
        
        public ExportAttribute(string contractName, Type contractType)
        {
            this.ContractName = contractName;
            this.ContractType = contractType;
        }
        
        public string ContractName
        {
            [CompilerGenerated, TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
            get
            {
                return this.<ContractName>k__BackingField;
            }
            [CompilerGenerated]
            private set
            {
                this.<ContractName>k__BackingField = value;
            }
        }
        
        public Type ContractType
        {
            [CompilerGenerated, TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
            get
            {
                return this.<ContractType>k__BackingField;
            }
            [CompilerGenerated]
            private set
            {
                this.<ContractType>k__BackingField = value;
            }
        }
    }
}
引用 2 楼 tcmakebest 的回复:
相当于给类,方法,属性等东西贴标签, 然后会对应在某些地方检测某个东西是否贴了某个标签进行筛选. 具体某个标签有什么用,那是根据需要了.
引用 2 楼 tcmakebest 的回复:
相当于给类,方法,属性等东西贴标签, 然后会对应在某些地方检测某个东西是否贴了某个标签进行筛选. 具体某个标签有什么用,那是根据需要了.
感谢各路神仙的回答.我看了给出的链接文章, "Attribute的目的是为元素提供关联附加信息" 定制特性可以应用的目标元素可以为:程序集(assembly)、 模块(module)、类型(type)、属性(property)、事件(event)、字段(field)、方法(method)、参数(param)、返回值(return)。 语法我能看懂了.但是就我上面贴出的代码来看,我还是很迷惑.它有什么实际意思 比如ExportAttribute里面的 ContractName 属性使用了 CompilerGenerated和TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")特性. 我再进入CompilerGeneratedAttribute这个特性.发现这个特性使用了 __DynamicallyInvokableAttribute和TargetedPatchingOptOut特性. 再进入 __DynamicallyInvokableAttribute这个特性 发现这个特性又使用了TargetedPatchingOptOut特性. 它们这样相互使用有什么实际意义.如果我分析代码的话.怎么考虑它们的用途? 还有个问题上面的代码有一段如下:
  public string ContractName
        {
      
      [CompilerGenerated, TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
           
	 get
            {
                return this.<ContractName>k__BackingField;
            }
      
      [CompilerGenerated]
       
     private set
            {
                this.<ContractName>k__BackingField = value;
            }
}
那里的 this.<ContractName>k__BackingField; 是什么语法,我用C#都两年也没见过这种语法,惭愧啊,请赐教!!!
宝_爸 2015-04-09
  • 打赏
  • 举报
回复
msdn有详细的文章: Attributes Tutorial https://msdn.microsoft.com/en-AU/library/aa288454(v=vs.71).aspx
tcmakebest 2015-04-08
  • 打赏
  • 举报
回复
相当于给类,方法,属性等东西贴标签, 然后会对应在某些地方检测某个东西是否贴了某个标签进行筛选. 具体某个标签有什么用,那是根据需要了.

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧