我如何GetCustomAttributes? [英] How do I GetCustomAttributes?
问题描述
我已经用了2.0框架尝试以下code和我得到一个属性回来了,但是当我尝试这在紧凑的框架,它总是返回一个空数组。在MSDN documenation说,它的支持,我是不是做错了什么?
测试X =新的测试();
字段信息field_info = x.GetType()getfield命令(ArrayShorts);
[对象] custom_attributes = field_info.GetCustomAttributes(typeof运算(MarshalAsAttribute),FALSE); [StructLayout(LayoutKind.Sequential)]
公共结构测试
{
[的MarshalAs(UnmanagedType.ByValArray,SizeConst = 4)]
公共USHORT [] ArrayShorts;
}
编辑2
所以我的CF队现在正在检查,但我相信你已经发现了一个bug。这说明,甚至更好:
公共类MyAttribute:属性
{
公共MyAttribute(UnmanagedType富)
{
} 公众诠释吧{搞定;组; }
}[StructLayout(LayoutKind.Sequential)]
公共结构测试
{
[CLSCompliant(假)]
[MyAttribute(UnmanagedType.ByValArray,酒吧= 4)]
[的MarshalAs(UnmanagedType.ByValArray,SizeConst = 4)]
公共USHORT [] ArrayShorts;
}类节目
{
静态无效的主要(字串[] args)
{ 字段信息field_info = typeof运算(测试).GetField(ArrayShorts);
[对象] custom_attributes = field_info.GetCustomAttributes(typeof运算(MarshalAsAttribute),FALSE);
的Debug.WriteLine(属性:+ custom_attributes.Length.ToString());
custom_attributes = field_info.GetCustomAttributes(typeof运算(MyAttribute),FALSE);
的Debug.WriteLine(属性:+ custom_attributes.Length.ToString());
custom_attributes = field_info.GetCustomAttributes(typeof运算(CLSCompliantAttribute),FALSE);
的Debug.WriteLine(属性:+ custom_attributes.Length.ToString());
}
}
根据完整的框架,我回去的:
属性:1
属性:1
属性:1
在CF 3.5我得到这样的:
属性:0
属性:1
属性:1
所以你可以看到它完全能够返回一个属性的,要么定制或BCL内,只是没有MarshalAsAttribute。
编辑3
好吧,我做了一个小挖多,而且事实证明,在CF行为实际上是<一个href=\"http://blog.opennetcf.com/ctacke/2009/08/14/NotAllCustomAttributesAreCreatedEqual.aspx\">correct如果你去的规范。这完全不合逻辑,但它是正确的。
I have tried the following code using the 2.0 framework and I get an attribute back, but when I try this on the compact framework, it always returns an empty array. The MSDN documenation says its supported, am I doing something wrong?
Test x = new Test();
FieldInfo field_info = x.GetType().GetField("ArrayShorts");
object[] custom_attributes = field_info.GetCustomAttributes(typeof(MarshalAsAttribute), false);
[StructLayout(LayoutKind.Sequential)]
public struct Test
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
public ushort[] ArrayShorts;
}
EDIT 2
So I'm checking with the CF team now but I believe you've found a bug. This shows it even better:
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());
}
}
Under the full framework I get back this:
Attributes: 1
Attributes: 1
Attributes: 1
Under CF 3.5 I get this:
Attributes: 0
Attributes: 1
Attributes: 1
So you can see it's fully capable of returning an attribute, either custom or within the BCL, just not the MarshalAsAttribute.
EDIT 3 Alright, I did a little more digging, and it turns out that the CF behavior is actually correct if you go by the spec. It goes against all logic, but it's right.
这篇关于我如何GetCustomAttributes?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!