C# 中定长数组的长度问题?

追_逐 2014-04-08 09:53:14

public struct STRU_TEST
{
[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 20, ArraySubType = UnmanagedType.I4)]
public int[] nData;
};

/*
* 以上这个结构体中的成员使用的时候似乎要先new
* STRU_TEST test = new STRU_TEST();
* test.nData = new int[20];
* for (int i = 0; i < test.nData.Length; ++i)
* test.nData[i] = i + 1;
*
* 我的问题是:能不能从结构体的定义中得到SizeConst = 20 的这个值,用于
* test.nData = new int[20], 替换这个20,用变量的形式。
* 我试了这样一句:test.nData = new int[test.nData.Length];
* 但是报错了!。。。
*/
...全文
440 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
xdashewan 2014-04-08
  • 打赏
  • 举报
回复
可以反射获取啊

    public struct STRU_TEST
    {
        [MarshalAsAttribute(SizeConst = 20)]
        public int[] nData;
    }

    public class MarshalAsAttribute : Attribute
    {
        public int SizeConst;
    }

            STRU_TEST stu = new STRU_TEST();
            Type clsType = typeof(STRU_TEST);
            FieldInfo f = clsType.GetField("nData");
            MarshalAsAttribute ma = (MarshalAsAttribute)Attribute.GetCustomAttribute(f, typeof(MarshalAsAttribute));

MarshalAsAttribute对象就可以拿到SizeConst了
追_逐 2014-04-08
  • 打赏
  • 举报
回复
引用 1 楼 gomoku 的回复:
不能。 因为编译器必须事先知道结构的具体布局。
引用 2 楼 gomoku 的回复:
如果长度不变,但想方便引用,可以用const int。

public struct STRU_TEST
{
        [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = DataLength , ArraySubType = UnmanagedType.I4)]
        public int[] nData;

        public const int DataLength = 20;  //<--
};
额~ OK 那好吧!那就这样了。
gomoku 2014-04-08
  • 打赏
  • 举报
回复
如果长度不变,但想方便引用,可以用const int。

public struct STRU_TEST
{
        [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = DataLength , ArraySubType = UnmanagedType.I4)]
        public int[] nData;

        public const int DataLength = 20;  //<--
};
gomoku 2014-04-08
  • 打赏
  • 举报
回复
不能。 因为编译器必须事先知道结构的具体布局。
guxingfeiyu 2014-04-08
  • 打赏
  • 举报
回复
同意4楼,但要注意: 遍历的做法:
            
            STRU_TEST stu_test = new STRU_TEST();
            Type t = stu_test.GetType();
            
            foreach (FieldInfo fieldInfo in t.GetFields())
            {
                if(fieldInfo == null)
                    continue;
                if ("nData".Equals(fieldInfo.Name))
                {
                    IList<CustomAttributeData> list= fieldInfo.GetCustomAttributesData();
                    if (list == null || list.Count < 1)
                        continue;
                    foreach (CustomAttributeData attData in list)
                    {
                        IList<CustomAttributeNamedArgument> customArgs = attData.NamedArguments;
                        if (customArgs == null) continue;
                        foreach (CustomAttributeNamedArgument customArg in customArgs)
                        {
                            if (customArg == null)
                                continue;
                            Console.WriteLine("{0}:{1}", customArg.MemberName, customArg.TypedValue.Value);
                        }
                    }
                }
                
            }

111,120

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧