English 中文(简体)
如何将不同的属性适用于母体接口中的成员而不是同一成员的成员。
原标题:How to apply a different attribute to an interface s member than that of the same member in a parent interface
  • 时间:2009-10-22 07:08:15
  •  标签:

mag interface interface:

public interface IAnimal
{
    string Color { get; }
}


public interface ICat : IAnimal
{
}

在本案中,ICat继承Inimal s Color财产。

是否可以在不向伊尼马尔人增加的情况下,给科罗氏族遗产添加一个属性?

以下是我试图实现的目标,但提供了汇编者警告:

public interface IAnimal
{
    string Color { get; }
}


public interface ICat : IAnimal
{
    [MyProperty]
    string Color { get; }
}
问题回答

这在C# 4.0 beta为我编集。 2. 结 论 它的确发出警告,可能有必要提出新的关键词。

另外,就好奇而言,这令我感到有趣: http://blogs.msdn.com/ericfallert/archive/2009/09/14/what-the-difference-between-a-partial-method-anda-partial-

我假定你正在发出警告。

warning CS0108:  ICat.Color  hides inherited member  IAnimal.Color . Use the new keyword if hiding was intended.

What are you trying to accomplish by applying that attribute?
If you want to avoid that warning you might do something like the following:

public class MyPropertyAttribute : Attribute { }

public interface IAnimal {
    string Color { get; }
}

public abstract class Cat : IAnimal {
    [MyProperty]
    public string Color {
        get { return CatColor; }
    }
    protected abstract string CatColor {
        get;
    }
}




相关问题
热门标签