简体   繁体   English

自定义属性的构造函数如何获取可选参数

[英]how the constructor of custom attribute take optional parameters

Hi guys i surf on the internet for custom attributes. 嗨,大家好我在互联网上浏览自定义属性。 i grasp the concept but, surprise and confuse to see that they set properties value through constructor of attribute class as parameter which is not taking any parameter as value from constructor. 我掌握了这个概念但是,惊讶和混淆,看到他们通过属性类的构造函数设置属性值作为参数,该参数不从构造函数中获取任何参数作为值。 please clarify the core concept. 请澄清核心概念。

[DeBugInfo(45, "Zara Ali", "12/8/2012", **Message = "Return type mismatch"**)]
 //like the Message here.

    public class DeBugInfo : System.Attribute
{
private int bugNo;
private string developer;
private string lastReview;
public string message;
public DeBugInfo(int bg, string dev, string d)
  {
   this.bugNo = bg;
   this.developer = dev;
   this.lastReview = d;
  }
  public int BugNo
 {
 get
 {
 return bugNo;
 }

public string Developer
{
get
{
 return developer;
 }
 }
 public string LastReview
{
get 
{
return lastReview;
}

public string Message
{
 get
{
 return message;
 }
  set
  {
    message = value;
  }
 } 




 //////////////////////////

The attribute syntax is ... a bit different to regular C# code. 属性语法与常规C#代码有点不同。 In regular C#, if you were creating that object manually, that would be similar to an object initializer: 在常规C#中,如果您手动创建该对象,则类似于对象初始值设定项:

var obj = new DeBugInfo(45, "Zara Ali", "12/8/2012") {
    Message = "Return type mismatch"
};

However, in reality, attributes aren't really instantiated in this way, at least not until they are absolutely required. 但是,实际上,属性并没有以这种方式实际实例化 ,至少在它们绝对需要之前是这样。 An attribute is actually raw metadata stamped into the IL, consisting of: 属性实际上是标记在IL中的原始元数据,包括:

  • a constructor token 构造函数标记
  • parameters for the constructor, composed of simple values 构造函数的参数,由简单值组成
  • additional property/field assignment mappings and corresponding simply values 附加属性/字段分配映射和相应的简单值

You can actually inspect all this information without ever actually creating an instance of the type. 实际上,您可以检查所有这些信息,而无需实际创建该类型的实例。 But if you use Attribute.GetAttributes etc, it figures out what it represents, and essentially applies the constructor and mappings at runtime. 但是如果你使用Attribute.GetAttributes等,它会找出它代表什么,并且基本上在运行时应用构造函数和映射。

For a complete example: 有关完整示例:

using System;

class DeBugInfoAttribute : Attribute
{
    public string Message { get; set; }
    public DeBugInfoAttribute(int i, string j, string k)
    {
        // to show we never get here!
        throw new NotImplementedException();
    }
}

[DeBugInfo(45, "Zara Ali", "12/8/2012", Message = "Return type mismatch")]
static class Program
{
    static void Main()
    {
        // this doesn't create the attribute, so no exception
        foreach (var data in typeof(Program).GetCustomAttributesData())
        {
            Console.WriteLine(data.AttributeType.Name);
            var parameters = data.Constructor.GetParameters();
            int i = 0;
            foreach (var arg in data.ConstructorArguments)
            {
                Console.WriteLine("{0}: {1}",
                    parameters[i++].Name,
                    arg.Value);
            }
            foreach (var binding in data.NamedArguments)
            {
                Console.WriteLine("{0}: {1}",
                    binding.MemberInfo.Name,
                    binding.TypedValue);
            }
        }
        // but this does: BOOM!
        var attribs = Attribute.GetCustomAttributes(typeof(Program));
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM