您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

C#类可以从其接口继承属性吗?

C#类可以从其接口继承属性吗?

否。每当实现接口或重写派生类中的成员时,都需要重新声明属性

如果您只关心ComponentModel(而不是直接反射),则有一种方法[AttributeProvider])从现有类型中建议属性(以避免重复),但这仅对属性和索引器用法有效。

举个例子:

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
c# 2022/1/1 18:17:43 有526人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶