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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

C#類可以從其接口繼承屬性嗎?

C#類可以從其接口繼承屬性嗎?

C#
慕無(wú)忌1623718 2019-11-13 14:33:53
這似乎暗示“否”。不幸的是。[AttributeUsage(AttributeTargets.Interface | AttributeTargets.Class, AllowMultiple = true, Inherited = true)]public class CustomDescriptionAttribute : Attribute{    public string Description { get; private set; }    public CustomDescriptionAttribute(string description)    {        Description = description;    }}[CustomDescription("IProjectController")]public interface IProjectController{    void Create(string projectName);}internal class ProjectController : IProjectController{    public void Create(string projectName)    {    }}[TestFixture]public class CustomDescriptionAttributeTests{    [Test]    public void ProjectController_ShouldHaveCustomDescriptionAttribute()    {        Type type = typeof(ProjectController);        object[] attributes = type.GetCustomAttributes(            typeof(CustomDescriptionAttribute),            true);        // NUnit.Framework.AssertionException:   Expected: 1   But was:  0        Assert.AreEqual(1, attributes.Length);    }}類可以從接口繼承屬性嗎?還是我在這里樹(shù)錯(cuò)樹(shù)了?
查看完整描述

3 回答

?
慕絲7291255

TA貢獻(xiàn)1859條經(jīng)驗(yàn) 獲得超6個(gè)贊

否。每當(dāng)實(shí)現(xiàn)接口或重寫派生類中的成員時(shí),都需要重新聲明屬性。


如果您只關(guān)心ComponentModel(而不是直接反射),則有一種方法([AttributeProvider])從現(xiàn)有類型中建議屬性(以避免重復(fù)),但這僅對(duì)屬性和索引器用法有效。


舉個(gè)例子:


using System;

using System.ComponentModel;

class Foo {

    [AttributeProvider(typeof(IListSource))]

    public object Bar { get; set; }


    static void Main() {

        var bar = TypeDescriptor.GetProperties(typeof(Foo))["Bar"];

        foreach (Attribute attrib in bar.Attributes) {

            Console.WriteLine(attrib);

        }

    }

}

輸出:


System.SerializableAttribute

System.ComponentModel.AttributeProviderAttribute

System.ComponentModel.EditorAttribute

System.Runtime.InteropServices.ComVisibleAttribute

System.Runtime.InteropServices.ClassInterfaceAttribute

System.ComponentModel.TypeConverterAttribute

System.ComponentModel.MergablePropertyAttribute


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

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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