变量strCSSClass通常具有值,但有时为空.
我不想在这个输入元素的HTML中包含一个空的class ="",这意味着如果strCSSClass为空,我根本不需要class =属性.
以下是执行条件HTML属性的一种方法:
<input type="text" id="@strElementID" @(CSSClass.IsEmpty() ? "" : "class=" + strCSSClass) />
Run Code Online (Sandbox Code Playgroud)
有更优雅的方式吗?特别是我可以遵循与元素的其他部分使用相同的语法:class ="@ strCSSClass"?
我正在尝试使用NSubstitute或其他模拟框架和MSTest(Visual Studio 2010)来模拟Excel电子表格.我不确定是否有比这更好的方法 - 这对测试来说不太合适:
这是一个例子(这是现在的所有原型代码,并不是很干净):
int[] lowerBounds = { 1, 1 };
int[] lengths = { 2, 2 };
//Initialize a 1-based array like Excel does:
object[,] values = (object[,])Array.CreateInstance(typeof(object), lengths, lowerBounds);
values[1,1] = "hello";
values[2,1] = "world";
//Mock the UsedRange.Value2 property
sheet.UsedRange.Value2.Returns(values);
//Test:
GetSetting(sheet, "hello").Should().Be("world"); //FluentAssertions
Run Code Online (Sandbox Code Playgroud)
到目前为止,非常好:如果GetSetting方法与我的测试在同一个项目中,则会通过.但是,当GetSetting在我的VSTO Excel-Addin项目中时,它会在GetSetting函数的第一行失败并出现以下错误:
System.MissingMethodException: Error: Missing method 'instance object [MyExcel.AddIn] Microsoft.Office.Interop.Excel.Range::get_Value2()' from class 'Castle.Proxies.RangeProxy'.
Run Code Online (Sandbox Code Playgroud)
作为参考,GetSetting从工作表中的columnA获取一个值,并返回columnB中的值.
public static string GetSetting(Excel.Worksheet sheet, string settingName) {
object[,] value = sheet.UsedRange.Value2 as object[,];
for (int …Run Code Online (Sandbox Code Playgroud)