为什么ICustomAttributeProvider.GetCustomAttributes()返回object[]而不是Attribute[]?
在使用ICustomAttributeProvidermscorlib 的实现时是否有任何情况,系统程序集将返回非类型的对象Attribute?
我已经使用2.0框架尝试了以下代码,我得到了一个属性,但是当我在紧凑框架上尝试这个时,它总是返回一个空数组.MSDN文档说它支持,我做错了吗?
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;
}
Run Code Online (Sandbox Code Playgroud) c# attributes compact-framework marshalling getcustomattributes
我试图用来GetCustomAttributes()获取属性上定义的属性.问题是该属性是一个被覆盖的属性,我无法弄清楚如何从表达式中提取被覆盖的属性.我只能弄清楚如何获得基类.
这是一些代码
public class MyAttribute : Attribute
{
//...
}
public abstract class Text
{
public abstract string Content {get; set;}
}
public class Abstract : Text
{
[MyAttribute("Some Info")]
public override string Content {get; set;}
}
Run Code Online (Sandbox Code Playgroud)
现在我试图MyAttribute摆脱抽象类.但我需要通过一个Expression.这就是我一直在使用的:
Expression<Func<Abstract, string>> expression = c => c.Content;
Expression exp = expression.Body;
MemberInfo memberType = (exp as MemberExpression).Member;
var attrs = Attribute.GetCustomAttributes(memberType, true);
Run Code Online (Sandbox Code Playgroud)
不幸的是atts最终为空.问题是menberType最终是为了Text.Content而不是Abstract.Content班级.因此,当我获得属性时,它什么都不返回.
在回答这个问题时,我尝试Type.GetCustomAttributes(true)在一个实现接口的类上使用,该接口上定义了一个Attribute.我很惊讶地发现GetCustomAttributes没有返回界面上定义的属性.为什么不呢?接口链不是接口的一部分吗?
示例代码:
[Attr()]
public interface IInterface { }
public class DoesntOverrideAttr : IInterface { }
class Program
{
static void Main(string[] args)
{
foreach (var attr in typeof(DoesntOverrideAttr).GetCustomAttributes(true))
Console.WriteLine("DoesntOverrideAttr: " + attr.ToString());
}
}
[AttributeUsage(AttributeTargets.All, Inherited = true)]
public class Attr : Attribute
{
}
Run Code Online (Sandbox Code Playgroud)
输出:没什么
我有一些代码来定义自定义属性然后读入代码,它无法工作.为了尝试解决问题我已经回去并尝试使用DisplayName,但是,我仍然遇到相同的问题GetCustomAttribute或GetCustomAttributes无法列出它们.我有一个例子如下.
我在类中设置了DisplayName属性,例如......
class TestClass
{
public TestClass() { }
[DisplayName("this is a test")]
public long testmethod{ get; set; }
}
Run Code Online (Sandbox Code Playgroud)
然后我有一些代码列出上面类中每个方法的DisplayName属性.
TestClass testClass = new TestClass();
Type type = testClass.GetType();
foreach (MethodInfo mInfo in type.GetMethods())
{
DisplayNameAttribute attr = (DisplayNameAttribute)Attribute.GetCustomAttribute(mInfo, typeof(DisplayNameAttribute));
if (attr !=null)
{
MessageBox.Show(attr.DisplayName);
}
}
Run Code Online (Sandbox Code Playgroud)
问题是没有列出DisplayName属性,上面的代码编译,运行并且不显示任何消息框.
我甚至尝试使用GetCustomAttributes的每个循环,再次列出每个方法的所有属性,从未列出DisplayName属性,但是,我确实获得了编译属性和其他此类系统属性.
任何人都知道我做错了什么?
更新 - 非常感谢NerdFury指出我使用的是Method而不是Properties.一旦改变,每件事都有效.
我是自定义属性的新手,所以我想知道是否有可能获得属性的值.我使用自定义属性的类中的属性示例是:
Private mFiller As String
<Position(378), Length(34), DataType("A"), ParticipantDependant("P/D"), RequiredProperty("Required"), Format("Blank")> _
Public Property Filler() As String
Get
Return mFiller
End Get
Set(ByVal value As String)
mFiller = value
End Set
End Property
Run Code Online (Sandbox Code Playgroud)
我正在尝试获取这些属性的值(即获取位置= 378,长度= 34等).我开始的循环看起来像这样:
Dim gwlImport As New ClientGWLImport
Dim properties() As PropertyInfo = gwlImport.GetType.GetProperties
Dim tmpInfo As PropertyInfo
For Each tmpInfo In properties
Debug.Print("Attributes for : " & tmpInfo.Name)
For Each tmpAttribute As Object In tmpInfo.GetCustomAttributes(True)
Debug.Print(tmpAttribute.ToString)
Next tmpAttribute
Next tmpInfo
Run Code Online (Sandbox Code Playgroud)
这让我得到了所有属性的名称,但我不确定如何获取值.有任何想法吗?
干杯,
瑞安