组合动态类型

发布于 2024-12-09 14:00:55 字数 868 浏览 1 评论 0原文

这可能是一个愚蠢的问题,但我很想知道是否有办法做到这一点。

为了简化导入大量空格分隔文件的过程,我想出了一个简单的方案来描述动态类型的布局并将其扔给调用委托的解析器。

布局如下所示:

var layout = new 
{
    Code = new SDFColumn() { Start = 0, Length = 20 },
    Name = new SDFColumn() { Start = 20, Length = 3 }
    // etc
};

一切都很好。我现在的情况是,我有 2 个非常大的 SDF 需要导入,它们的结构 85% 相同,除了最后的一些差异。

有没有一种方法可以将一种布局附加到另一种布局,例如:

var layoutCommon = new
{
     /* Common fields */
};

var layoutFile01 = new
{
     /* Changes for first file type */
};

var layoutFile02 = new
{
     /* Changes for the second file type */
};


var finalLayout = /* ??? */;

我意识到行不通的一件事是:

var completeLayout = { };

if(file01)
    completeLayout = { /* everything */ };
else
    completeLayout = { /* everything */ };

显然,这行不通,因为所有 3 种类型本质上都是不同的。

任何想法将不胜感激:)

This might be a dumb question, but I'd love to know if there was a way I could do this.

To ease the process of importing lots and lots of Space-delimited files, I came up with a simple scheme to describe the layout in a dynamic type and throw it to a parser which calls a delegate.

A layout looks likes this:

var layout = new 
{
    Code = new SDFColumn() { Start = 0, Length = 20 },
    Name = new SDFColumn() { Start = 20, Length = 3 }
    // etc
};

All works great. I'm now in a situation where I have 2 very large SDFs to import, whose structure is 85% identical, bar a few differences at the end.

Is there a way to append the layout of one to another, e.g:

var layoutCommon = new
{
     /* Common fields */
};

var layoutFile01 = new
{
     /* Changes for first file type */
};

var layoutFile02 = new
{
     /* Changes for the second file type */
};


var finalLayout = /* ??? */;

One thing I realised would not work, was:

var completeLayout = { };

if(file01)
    completeLayout = { /* everything */ };
else
    completeLayout = { /* everything */ };

This doesn't work, obviously, because all 3 types are fundamentally different.

Any ideas would be appreciated :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

难如初 2024-12-16 14:00:55

您是否尝试过使用dynamic而不是var?这应该将键入延迟到运行时,因此您的潜在对象不必匹配。 var 只是任何静态类型的替代品;它实际上根本不是动态的。

Have you tried using dynamic instead of var? That should delay typing until runtime, so your potential objects don't have to match. var is simply a substitute for any static type; it's not actually dynamic at all.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文