组合动态类型
这可能是一个愚蠢的问题,但我很想知道是否有办法做到这一点。
为了简化导入大量空格分隔文件的过程,我想出了一个简单的方案来描述动态类型的布局并将其扔给调用委托的解析器。
布局如下所示:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否尝试过使用
dynamic
而不是var
?这应该将键入延迟到运行时,因此您的潜在对象不必匹配。var
只是任何静态类型的替代品;它实际上根本不是动态的。Have you tried using
dynamic
instead ofvar
? 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.