【发布时间】:2021-10-05 02:42:35
【问题描述】:
我正在尝试将 getter 和 setter 添加到此结构中,因此我可以将其用作 listView 项目源。问题是,这会导致 MarshalAs 无效,因此不允许编译。解决此问题的最佳选择是什么?
属性“MarshalAs”在此声明类型上无效。它仅对“字段、参数、返回”声明有效。**
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct FileExplorerData
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 251)]
public string name { get; set; }
public FILETIME lastwrite { get; set; }
public bool type { get; set; }
public int size { get; set; }
};
【问题讨论】:
-
简单 -
MarshalAs不能用于property -
是的@T.S.,这正是问题所在。您有可以推荐的解决方案吗?
-
[System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Parameter | System.AttributeTargets.ReturnValue, Inherited=false)]-- 这是你的解决方案 -
但是为什么你需要这个数据源呢?这是winforms还是WPF?
-
@T.S.我有一个这些结构的列表,我将其设置为 WPF 列表视图的数据源。