Saturday, January 06, 2007

求教:attribute方面的一个问题

发信人: jueww (觉·无我), 信区: DotNET
标 题: 求教:attribute方面的一个问题
发信站: 水木社区 (Sat Jan 6 11:58:50 2007), 站内

我写了下面一个测试程序:
using System;
using System.Runtime.InteropServices;

[StructLayout(LayoutKind.Sequential)]
public struct Point
{
public int x;
public int y;
}

class TestApplication
{
public static void Main()
{
Type type = typeof(Point);//ioint);
foreach(Attribute attr in type.GetCustomAttributes(true))
{
StructLayoutAttribute structLayout = attr as StructLayoutAttribute;
if(structLayout != null)
{
Console.WriteLine("Pack = {0}",structLayout.Value);
}
}
}
}
期望输出attribute的值,但实际结果发现GetCustomAttributes返回为空,这是为什么?


[本篇全文] [本篇作者:pseudocode] [进入讨论区] [返回顶部]4发信人: pseudocode (I can Run!), 信区: DotNET
标 题: Re: 求教:attribute方面的一个问题
发信站: 水木社区 (Sat Jan 6 15:31:18 2007), 站内

 structLayout 用GetCustomAttributes得不到的
type类直接有一个属性是StructLayoutAttribute
这个直接取就好了

No comments: