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.
Nguồn
2010-10-01 15:23:40
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 –
Đú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 đó;) –