2010-10-01 37 views
13
public class MyWebControl { 

    [ExternallyVisible] 
    public string StyleString {get;set;} 

} 

public class SmarterWebControl : MyWebControl { 

    [ExternallyVisible] 
    public string CssName{get;set;} 

    new public string StyleString {get;set;} //Doesn't work 

} 

Có thể loại bỏ thuộc tính trong phân lớp không? Tôi muốn thuộc tính được thừa hưởng bởi các lớp con khác, không phải là thuộc tính này.C# Ghi đè một thuộc tính trong một phân lớp

Chỉnh sửa: Rất tiếc, có vẻ như tôi đã quên biên dịch hoặc một điều gì đó vì mã như đã đăng ở trên, thực tế, hoạt động!

+0

vui mừng mà làm việc, tôi cảm thấy bối rối là tại sao nó sẽ không. Phải nói mặc dù, tôi không chắc chắn tôi thích việc sử dụng mới ở đây (mặc dù tôi kinda chống mới nói chung). Để ai đó nhìn vào lớp, nó không nói cho họ biết tại sao bạn làm điều đó. Sử dụng một thuộc tính với một tham số sai, mặt khác là tự ghi lại imho –

+0

Đúng, và nếu tôi đang viết toàn bộ điều từ đầu tôi có thể làm theo cách đó;) –

Trả lời

5

Nó phù hợp với tôi.

mã kiểm tra:

public static void Main() 
{ 
    var attribute = GetAttribute(typeof (MyWebControl), "StyleString", false); 
    Debug.Assert(attribute != null); 

    attribute = GetAttribute(typeof(SmarterWebControl), "StyleString", false); 
    Debug.Assert(attribute == null); 

    attribute = GetAttribute(typeof(SmarterWebControl), "StyleString", true); 
    Debug.Assert(attribute == null); 
} 

private static ExternallyVisibleAttribute GetAttribute(Type type, string propertyName, bool inherit) 
{ 
    PropertyInfo property = type.GetProperties().Where(p=>p.Name.Equals(propertyName, StringComparison.OrdinalIgnoreCase)).FirstOrDefault(); 

    var list = property.GetCustomAttributes(typeof(ExternallyVisibleAttribute), inherit).Select(o => (ExternallyVisibleAttribute)o); 

    return list.FirstOrDefault(); 
} 
13

Đây chính xác là lý do tại sao các thuộc tính khung có thể được "ghi đè", tham số boolean (trên khuôn mặt của nó) có vẻ vô nghĩa. Lấy ví dụ BrowsableAttribute; tham số boolean sẽ có vẻ là giám khảo đã lỗi thời bằng tên, nhưng hãy ví dụ này:

class SomeComponent 
{ 
    [Browsable(true)] 
    public virtual string SomeInfo{get;set;} 
} 

class SomeOtherComponent : SomeComponent 
{ 
    [Browsable(false)] // this property should not be browsable any more 
    public override string SomeInfo{get;set;} 
} 

vậy, để trả lời câu hỏi của bạn, bạn có thể làm thuộc tính ExternallyVisible bạn lấy một tham số boolean, để chỉ ra cho dù đó là thực sự từ bên ngoài hiển thị và khi bạn kế thừa, bạn có thể thay đổi thành false - giống như BrowsableAttribute.

2

Bạn có thể kế thừa thuộc tính và thêm thuộc tính kiểm soát xem mã attrbiute có kích hoạt hay không.

thì bạn có thể ghi đè hành vi thuộc tính trên lớp được kế thừa?

do đó bạn sẽ sử dụng (nếu bạn thêm một tham số để các nhà xây dựng)

[ExternallyVisible(false)] 

[ExternallyVisible(Enabled = false)] 

i fyou sử dụng một tính chất của kích hoạt trong lớp atrtibute

3

Tôi không hiểu gì là rắc rối. Mã đã đăng của bạn thực hiện điều mong đợi (ít nhất, điều bạn mong đợi nó làm) trong thử nghiệm của tôi: nghĩa là, thuộc tính StyleString không có thuộc tính ExternallyVisible. Đây là mã thử nghiệm của tôi:

[AttributeUsage(AttributeTargets.Property)] 
public class ExternallyVisible : Attribute 
{ 
} 

public class MyWebControl 
{ 
    [ExternallyVisible] 
    public string StyleString { get; set; } 
} 

public class SmarterWebControl : MyWebControl 
{ 

    [ExternallyVisible] 
    public string CssName { get; set; } 

    new public string StyleString { get; set; } //Doesn't work 

} 

class Program 
{ 
    static void Main() 
    { 
     MyWebControl myctrl = new MyWebControl(); 
     SmarterWebControl smartctrl = new SmarterWebControl(); 

     MemberInfo info = typeof(SmarterWebControl); 
     PropertyInfo[] props = (typeof(SmarterWebControl)).GetProperties(); 
     Console.WriteLine("{0} properties", props.Length); 

     foreach (var prop in props) 
     { 
      Console.WriteLine(prop.Name); 
      foreach (var attr in prop.GetCustomAttributes(true)) 
      { 
       Console.WriteLine(" " + attr); 
      } 
     } 

     Console.ReadLine(); 
    } 

} 

Trong .NET 4.0, tôi nhận được kết quả này:

2 properties 
CssName 
    sotesto.ExternallyVisible 
StyleString 

Nói cách khác, các thuộc tính không được áp dụng cho các StyleString tài sản.

+0

Đánh tôi với nó. :) –

Các vấn đề liên quan