C# lambda 内容在调用之前不会发生,对吗?另外,代码清理
我有以下方法:
protected static void updateExistingSection(XmlDocument doc,
XmlNode rootNode, string sectionTag, CreateSection createSection,
Func<XmlNode[]> createChildNodes, Action<XmlNode> firstSection)
{
IEnumerable<XmlNode> sections =
getChildNodesByName(rootNode, sectionTag);
if (sections.Count() < 1)
{
rootNode.AppendChild(createSection(doc, createChildNodes()));
return;
}
removeSubsequentNodes(sections, rootNode, firstSection);
}
private static void updateExistingCredentialsSection(XmlDocument doc,
XmlNode rootNode, string newUser, string newPassword, string newHost)
{
updateExistingSection(doc, rootNode, CREDENTIALS_SECTION_TAG,
createCredentialsSection,
() => new[] {
createNode(USER_TAG, doc, newUser),
createNode(PASSWORD_TAG, doc, newPassword),
createNode(HOST_TAG, doc, newHost)
},
credentialsNode =>
{
updateExistingLeafNode(USER_TAG, doc, credentialsNode, newUser);
updateExistingLeafNode(PASSWORD_TAG, doc, credentialsNode,
newPassword);
updateExistingLeafNode(HOST_TAG, doc, credentialsNode, newHost);
});
}
我的问题涉及在 updateExistingCredentialsSection
中传递给 updateExistingSection
的第五个参数,() =>; new[] { createNode(USER_TAG, doc, newUser), ... }
一。据我了解,除非在 updateExistingSection
中调用该 lambda 表达式,否则这些 createNode
调用不会发生,对吧?对于为 updateExistingSection
提供的最后一个参数中的 updateExistingLeafNode
调用也是如此。
另外,从设计的角度来看,这两种方法看起来都很荒谬吗?您是否看到一种方法可以使任一方法更小,或者需要更少的参数?我一直在尝试将事情干燥,这就是导致首先编写 updateExistingSection
的原因,因为我有几种方法执行相同的功能。
I have the following methods:
protected static void updateExistingSection(XmlDocument doc,
XmlNode rootNode, string sectionTag, CreateSection createSection,
Func<XmlNode[]> createChildNodes, Action<XmlNode> firstSection)
{
IEnumerable<XmlNode> sections =
getChildNodesByName(rootNode, sectionTag);
if (sections.Count() < 1)
{
rootNode.AppendChild(createSection(doc, createChildNodes()));
return;
}
removeSubsequentNodes(sections, rootNode, firstSection);
}
private static void updateExistingCredentialsSection(XmlDocument doc,
XmlNode rootNode, string newUser, string newPassword, string newHost)
{
updateExistingSection(doc, rootNode, CREDENTIALS_SECTION_TAG,
createCredentialsSection,
() => new[] {
createNode(USER_TAG, doc, newUser),
createNode(PASSWORD_TAG, doc, newPassword),
createNode(HOST_TAG, doc, newHost)
},
credentialsNode =>
{
updateExistingLeafNode(USER_TAG, doc, credentialsNode, newUser);
updateExistingLeafNode(PASSWORD_TAG, doc, credentialsNode,
newPassword);
updateExistingLeafNode(HOST_TAG, doc, credentialsNode, newHost);
});
}
My question concerns the fifth parameter passed in updateExistingCredentialsSection
to updateExistingSection
, the () => new[] { createNode(USER_TAG, doc, newUser), ... }
one. It was my understanding that those createNode
calls will not occur unless that lambda expression is called in updateExistingSection
, right? Likewise for the updateExistingLeafNode
calls in the last parameter given to updateExistingSection
.
Also, from a design perspective, does either method look ridiculous? Do you see a way I could make either method smaller, or require fewer parameters? I've been trying to DRY things out, which is what led to writing updateExistingSection
in the first place, since I had several methods doing the same functionality.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正确的。将 lambda 视为方法。您可以在任何地方定义它们(法律允许),但其中的代码只有在显式调用时才会运行。
事实上,如果您传递给某些 lambda 表达式的函数中存在分支逻辑,则它们的代码可能永远不会在给定的执行路径中运行。
Correct. Think of the lambdas as methods. You can define them anywhere (legally allowed), but the code within them is not run until explicitly invoked.
In fact, the code for some lambdas might never be be run in a given execution path if there is branching logic in the function you pass them to.