第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

自定義屬性的構(gòu)造函數(shù)何時運行?

自定義屬性的構(gòu)造函數(shù)何時運行?

瀟湘沐 2019-11-19 11:11:52
什么時候運行?它是否對我應(yīng)用它的每個對象都運行一次,還是只運行一次?它可以做任何事情,或者其動作受到限制嗎?
查看完整描述

3 回答

?
Smart貓小萌

TA貢獻(xiàn)1911條經(jīng)驗 獲得超7個贊

在屬性構(gòu)造函數(shù)內(nèi)設(shè)置調(diào)試器斷點,并編寫一些反射代碼以讀取那些屬性。您會注意到,只有從relfection API返回屬性對象后,才能創(chuàng)建屬性對象。屬性是每個類的。它們是元數(shù)據(jù)的一部分。


看看這個:


Program.cs

using System;

using System.Linq;

[My(15)]

class Program

{

    static void Main(string[] args)

    {

        Console.WriteLine("Program started");

        var ats =

            from a in typeof(Program).GetCustomAttributes(typeof(MyAttribute), true)

            let a2 = a as MyAttribute

            where a2 != null

            select a2;


        foreach(var a in ats)

            Console.WriteLine(a.Value);


        Console.WriteLine("Program ended");

        Console.ReadLine();

    }

}

MyAttribute.cs

using System;

[AttributeUsage(validOn : AttributeTargets.Class)]

public class MyAttribute : Attribute

{

    public MyAttribute(int x)

    {

        Console.WriteLine("MyAttribute created with {0}.", x);

        Value = x;

    }


    public int Value { get; private set; }    

}

結(jié)果

Program started

MyAttribute created with 15.

15

Program ended

但不必?fù)?dān)心屬性構(gòu)造函數(shù)的性能。它們是反思的最快部分:-P


查看完整回答
反對 回復(fù) 2019-11-19
  • 3 回答
  • 0 關(guān)注
  • 525 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號