在C#中,我是否有一个Func参数来表示具有此签名的方法?
XmlNode createSection(XmlDocument doc, params XmlNode[] childNodes)
Run Code Online (Sandbox Code Playgroud)
我尝试了一个类型的参数,Func<XmlDocument, params XmlNode[], XmlNode>但是,哦,ReSharper/Visual Studio 2008疯狂突出显示红色.
更新: 好的,谷歌搜索'c#params func'没有产生任何结果,但'c#params delegate'让我想到了这个问题.按照Jon Skeet在那里的回答,看起来似乎我可以创建一个delegate,比方说Foo,然后不是在我的类型方法中有一个参数Func<XmlDocument, params XmlNode[], XmlNode>,我接受一个类型的参数Foo.
这似乎不应该是困难的,但我现在卡住了.我正在尝试从与给定XPath查询字符串匹配的节点获取特定属性的属性值.这是我到目前为止所拥有的:
public static IEnumerable<string> GetAttributes(this XmlDocument xml,
string xpathQuery, string attributeName)
{
var doc = new XPathDocument(new XmlNodeReader(xml));
XPathNavigator nav = doc.CreateNavigator();
XPathExpression expr = nav.Compile(xpathQuery);
XPathNodeIterator iterator = nav.Select(expr);
while (iterator.MoveNext())
{
XPathNavigator curNav = iterator.Current;
if (curNav.HasAttributes)
{
XmlNode curNode = ((IHasXmlNode)curNav).GetNode();
if (null != curNode)
{
XmlAttribute attrib = curNode.Attributes[attributeName];
if (null != attrib)
{
yield return attrib.Value;
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这目前抛出异常:
System.InvalidCastException:无法将类型为"MS.Internal.Xml.Cache.XPathDocumentNavigator"的对象强制转换为"System.Xml.IHasXmlNode".
我错了吗?是否有更简单的方法从匹配节点获取属性值?
当我DeploymentItem在MSTest单元测试中使用该属性时,我无法将XSL文件复制到与测试程序集相同的目录.我按照所选答案解决了这个问题,我需要复制的文件将其"复制到输出目录"设置为"始终复制".当我检查我的ProjectDir\bin目录(目标目录)时,我想复制的文件确实存在,与DLL和PDB一起.
我有几个单元测试,具有以下设置:
private const string DLL = "Service.dll";
private const string XSL_PATH = "transform.xsl";
[TestInitialize]
public void InitializeTest()
{
Assert.IsTrue(File.Exists(DLL)); // passes
}
[TestMethod]
[DeploymentItem(DLL)]
[DeploymentItem(XSL_PATH)]
public void XmlToResultsTest()
{
Assert.IsTrue(File.Exists(XSL_PATH)); // fails
}
Run Code Online (Sandbox Code Playgroud)
XSL测试失败,因为当我检查MSTest的TestResults\specificTestRun\Out目录时,我看到了DLL和PDB,但我的XSL文件不存在.我想知道的是,为什么 XSL文件不会与DLL和PDB一起被复制,即使我明确告诉Visual Studio将其复制到那里DeploymentItem?
看看其他人的代码,在花括号块中包含一个额外的空间似乎很常见.这有什么理由吗?对我而言似乎增加了额外的击键以增加丑陋.特别是当事物嵌套时:
lambda { (1..5).map { |i| { :a => { :b => i } } } }
Run Code Online (Sandbox Code Playgroud)
出于某种原因,它看起来更简洁,更连贯:
lambda {(1..5).map {|i| {:a => {:b => i}}}}
Run Code Online (Sandbox Code Playgroud)
也许额外的空间是一些文本编辑器的副作用或有历史原因或什么?我没有在样式指南中看到这个问题,如果它像2个空格缩进我想遵循惯例,但如果没有充分的理由我想我会继续按照自己的方式做事.你更喜欢哪个?为什么?
我正在使用WTForms并且我正在尝试显示SelectField,但是我收到以下错误:
>>> form.status()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python26\Lib\site-packages\wtforms\fields.py", line 136, in __call__
return self.widget(self, **kwargs)
File "C:\Python26\Lib\site-packages\wtforms\widgets.py", line 237, in __call__
for val, label, selected in field.iter_choices():
File "C:\Python26\Lib\site-packages\wtforms\fields.py", line 390, in iter_choices
for value, label in self.choices:
ValueError: too many values to unpack
Run Code Online (Sandbox Code Playgroud)
这是我的表格:
class TestForm(Form):
status = SelectField(u'Status', choices=Test.statuses())
Run Code Online (Sandbox Code Playgroud)
该Test.statuses静态方法返回一个字符串列表.我究竟做错了什么?
我有一个大的xml文件(40 Gb),我需要分成更小的块.我正在使用有限的空间,所以当我将它们写入新文件时,有没有办法从原始文件中删除行?
谢谢!
我有一个实现IDisposable的抽象类,如下所示:
public abstract class ConnectionAccessor : IDisposable
{
public abstract void Dispose();
}
Run Code Online (Sandbox Code Playgroud)
在Visual Studio 2008 Team System中,我在项目上运行了代码分析,其中一个警告如下:
Microsoft.Design:修改'ConnectionAccessor.Dispose()'以便它调用Dispose(true),然后在当前对象实例上调用GC.SuppressFinalize(在Visual Basic中为'this'或'Me'),然后返回.
它只是愚蠢,告诉我修改抽象方法的主体,还是应该在任何派生实例中做进一步的事情Dispose?
我不知道这是否可行,但在我的一些单元测试中,我最终用相同的参数初始化不同的对象.我希望能够将这些参数存储在某个变量中,并使用该变量初始化多参数对象构造函数,而不是执行以下操作:
Thing thing1 = new Thing(arg1, arg2, arg3, arg4);
Thing thing2 = new Thing(arg1, arg2, arg3, arg4);
Thing thing3 = new Thing(arg1, arg2, arg3, arg4);
Run Code Online (Sandbox Code Playgroud)
我可以做以下事情:
MagicalArgumentsContainer args = (arg1, arg2, arg3, arg4);
Thing thing1 = new Thing(args);
Thing thing2 = new Thing(args);
Thing thing3 = new Thing(args);
Run Code Online (Sandbox Code Playgroud)
有没有办法在没有覆盖Thing的构造函数的情况下执行此操作来获取一个手动爆炸并从中获取参数的列表?也许一些C#语法糖?
我正在尝试使用Treetop来解析ERB文件.我需要能够处理如下行:
<% ruby_code_here %>
<%= other_ruby_code %>
Run Code Online (Sandbox Code Playgroud)
由于Treetop是用Ruby编写的,而你用Ruby编写Treetop语法,Treetop中已经有一些现有方法可以说"嘿,在这里寻找Ruby代码,并给我分解",而不必编写单独的规则来处理Ruby语言的所有部分?我正在寻找一种方法,在我的.treetop语法文件中,有类似的东西:
rule erb_tag
"<%" ruby_code "%>" {
def content
...
end
}
end
Run Code Online (Sandbox Code Playgroud)
凡ruby_code由树顶提供了一些规则进行处理.
编辑: 其他人使用Ruby-lex解析ERB,但我在尝试重现他所做的事时遇到了错误.在生成解析器类时,rlex程序没有生成完整的类.
编辑:对,所以你很多都很郁闷,但感谢你的信息.:)对于我的Master的项目,我正在编写一个测试用例生成器,需要使用ERB作为输入.幸运的是,出于我的目的,我只需要识别ERB代码中的一些内容,例如if语句和其他条件以及循环.我想我可以提出Treetop语法来匹配它,但需要注意的是Ruby并不完整.
我正在尝试将所选的默认浅灰色突出显示更改为聚焦ListViewItem时显示的蓝色突出显示ListView.我一直在努力在线调和不同的StackOverflow答案和来源,但我还没想出我还需要什么样的XAML.我有以下内容:
<ListView ItemContainerStyle="{StaticResource checkableListViewItem}"
SelectionMode="Multiple"
View="{StaticResource fieldValueGridView}"/>
Run Code Online (Sandbox Code Playgroud)
引用的View资源是:
<GridView x:Key="fieldValueGridView" AllowsColumnReorder="False">
<GridViewColumn Header="Field">
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock FontWeight="Bold" Text="{Binding Path=DisplayName}"/>
<TextBlock FontWeight="Bold" Text=": "/>
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Value" DisplayMemberBinding="{Binding Path=FieldValue}"/>
</GridView>
Run Code Online (Sandbox Code Playgroud)
引用的ItemContainerStyle资源是:
<Style TargetType="ListViewItem" BasedOn="{StaticResource {x:Type ListViewItem}}"
x:Key="checkableListViewItem">
<Setter Property="IsSelected" Value="{Binding Path=IsChecked}" />
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="VerticalContentAlignment" Value="Top" />
</Style>
Run Code Online (Sandbox Code Playgroud)
的ListView的IsEnabled属性不会改变,如果该事项.我想以某种方式合并<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="{x:Static SystemColors.HighlightColor}"/>,或类似的东西,以突出显示ListViewItem未聚焦的选定的ListView.
我有以下风格,但这似乎并没有影响ListViewItem …