EDIT 2
Vì vậy, tôi đang kiểm tra với các đội CF bây giờ nhưng tôi tin rằng bạn đã tìm thấy một lỗi. Điều này cho thấy nó thậm chí còn tốt hơn:
public class MyAttribute : Attribute
{
public MyAttribute(UnmanagedType foo)
{
}
public int Bar { get; set; }
}
[StructLayout(LayoutKind.Sequential)]
public struct Test
{
[CLSCompliant(false)]
[MyAttribute(UnmanagedType.ByValArray, Bar = 4)]
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
public ushort[] ArrayShorts;
}
class Program
{
static void Main(string[] args)
{
FieldInfo field_info = typeof(Test).GetField("ArrayShorts");
object[] custom_attributes = field_info.GetCustomAttributes(typeof(MarshalAsAttribute), false);
Debug.WriteLine("Attributes: " + custom_attributes.Length.ToString());
custom_attributes = field_info.GetCustomAttributes(typeof(MyAttribute), false);
Debug.WriteLine("Attributes: " + custom_attributes.Length.ToString());
custom_attributes = field_info.GetCustomAttributes(typeof(CLSCompliantAttribute), false);
Debug.WriteLine("Attributes: " + custom_attributes.Length.ToString());
}
}
Trong khuôn khổ đầy đủ tôi nhận được trở lại này:
Attributes: 1
Attributes: 1
Attributes: 1
Dưới CF 3.5 Tôi có được điều này:
Attributes: 0
Attributes: 1
Attributes: 1
Vì vậy, bạn có thể nhìn thấy nó là hoàn toàn có khả năng trả về một thuộc tính, hoặc là tùy chỉnh hoặc trong BCL, không phải là thuộc tính MarshalAsAttribute.
EDIT 3 Alright, tôi đã làm một ít đào hơn, và nó quay ra rằng hành vi CF thực sự là correct if you go by the spec. Nó đi ngược lại tất cả logic, nhưng nó đúng.
Tôi đang xử lý FieldInfo cho ví dụ trên của mình. Tôi có thể thử và xem nếu PropertyInfo sẽ làm việc, nhưng tôi tự hỏi tại sao ví dụ của tôi không hoạt động. – SwDevMan81
boo cho lỗi: P Bạn có biết nếu có một công việc xung quanh? – SwDevMan81
Tôi đoán công việc xung quanh chỉ có thể là tạo thuộc tính tùy chỉnh của riêng tôi (chỉ cần phát minh lại bánh xe tôi đoán)? Kể từ khi có vẻ như hoạt động ok. – SwDevMan81