int.TryPrase
是伟大的,但只有一个问题......至少需要使用两行代码:
int intValue;
string stringValue = "123";
int.TryParse(stringValue, out intValue);
....
Run Code Online (Sandbox Code Playgroud)
当然我可以这样做:
string stringValue = "123";
int intValue = Convert.ToInt32(string.IsNullOrWhiteSpace(stringValue) ? 0 : stringValue);
Run Code Online (Sandbox Code Playgroud)
只需一行代码.
我怎样才能让int.TryParse使用一个衬里,或者还有第三种选择?
谢谢!
Bezden最好地回答了这个问题,但实际上我计划使用Reddogs解决方案.
我试图对一些代码进行单元测试,我需要替换它:
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create( uri );
httpWebRequest.CookieContainer = new CookieContainer();
Run Code Online (Sandbox Code Playgroud)
同
WebRequest webRequest = WebRequest.Create( uri );
webRequest.CookieContainer = new CookieContainer();
Run Code Online (Sandbox Code Playgroud)
基本上,如何在不使用HttpWebRequest的情况下将cookie添加到请求中?
decimal result = 100 * 200;
Run Code Online (Sandbox Code Playgroud)
VS
decimal result = Decimal.Multiply(100, 200);
Run Code Online (Sandbox Code Playgroud) Visual Studio基于一种方法为我创建了一个单元测试项目(右键单击添加测试).当我尝试访问数据库时,我得到一个例外.抛出此代码以查看我的连接是什么:
ConnectionStringSettings connStringSettings = ConfigurationManager.
ConnectionStrings["myConnectionString"];
Run Code Online (Sandbox Code Playgroud)
但是,connStringSettings
是空的.经过检查,ConnectionStrings集合只有一个.它似乎不是从我的web.config读取.
我的DAL是隔离的,不能通过代码设置其连接字符串.它的连接字符串在代码中设置如下:
set
{
value = System.Configuration.ConfigurationManager.
ConnectionStrings["myConnectionString"].ConnectionString;
}
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个问题?
c# integration-testing connection-string web-config visual-studio-2010
我有一些产生无限循环的代码.现在我需要编写一个在大约200ms后失败的测试.200ms将指示代码处于无限循环中.
例如:
public void CodeUnderTest()
{
while(true)
{
}
}
Run Code Online (Sandbox Code Playgroud) 尝试实施新的内容页面时有一个"duh"时刻
这是结构
Master Page
---- Nested Master Page
-------- Nested Master's Content Page
Run Code Online (Sandbox Code Playgroud)
标记:
母版页
<asp:ContentPlaceHolder ID="bodyContent" runat="server">
</asp:ContentPlaceHolder>
Run Code Online (Sandbox Code Playgroud)
嵌套母版页
MasterPageFile="~/Views/Shared/Administrator.Master"
<asp:Content ID="Content2" CotentPlaceHolderID="bodyContent" runat="server">
</asp:Content>
Run Code Online (Sandbox Code Playgroud)
嵌套大师的内容页面
MasterPageFile="~/Views/Intervention/InterventionMaster.master"
<asp:Content runat="server" ID="myContent" ContentPlaceHolderID="Content2">
</asp:Content>
Run Code Online (Sandbox Code Playgroud)
收到错误:
在母版页'/Views/Intervention/InterventionMaster.master'中找不到ContentPlaceHolder'Content2',在内容页面中验证内容控件的ContentPlaceHolderID属性.
我能做错什么?
构建Windows Universal应用程序时,我遇到了一个奇怪的构建错误.
严重级代码说明项目文件行抑制状态错误无法找到C:\ Users\me\Source\Repos\TT\Windows\MyCommonLibrary\packages.config.确保此项目已安装Microsoft.Bcl.Build,并且packages.config位于项目文件旁边.MyApp的
'MyApp'有一个项目引用'MyCommonLibrary'.
真正奇怪的是,即使它显示为构建"错误".这不会影响我构建"MyApp"或"MyCommonLibrary"的能力!
它也不会阻止我在本地或其他地方部署和运行我的应用程序.当它没有破坏构建时,将它列为构建错误只是一件令人讨厌的事情!
由于接口不能包含实现,因此在我看来,这会导致从接口继承的类中的代码重复.在下面的示例中,假设,假设从流中设置读取的前10行左右是重复的. 尽量不要关注这里的措辞,而是关注在每个类之间创建重复代码是多么容易的概念.
例如:
public interface IDatabaseProcessor
{
void ProcessData(Stream stream);
}
public class SqlServerProcessor : IDatabaseProcessor
{
void ProcessData(Stream stream)
{
// setting up logic to read the stream is duplicated code
}
}
public class DB2Processor : IDatabaseProcessor
{
void ProcessData(Stream stream)
{
// setting up logic to read the stream is duplicated code
}
}
Run Code Online (Sandbox Code Playgroud)
我意识到使用ProcessData的抽象基类并添加非抽象成员是一种解决方案.但是,如果我真的真的想要使用界面呢?
从我的.csproj文件:
<Content Include="log4net.config">
<SubType>Designer</SubType>
</Content>
<Content Include="log4net.Release.config">
<DependentUpon>log4net.config</DependentUpon>
</Content>
<Content Include="log4net.Debug.config">
<DependentUpon>log4net.config</DependentUpon>
</Content>
<Content Include="log4net.Live.config">
<DependentUpon>log4net.config</DependentUpon>
</Content>
<Content Include="log4net.Demo.config">
<DependentUpon>log4net.config</DependentUpon>
</Content>
Run Code Online (Sandbox Code Playgroud)
在我的.csproj文件的底部:
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" />
<Target Name="AfterCompile" Condition="exists('log4net.$(Configuration).config')">
<TransformXml Source="log4net.config"
Destination="$(IntermediateOutputPath)$(TargetFileName).config"
Transform="log4net.$(Configuration).config" />
<ItemGroup>
<AppConfigWithTargetPath Remove="log4net.config"/>
<AppConfigWithTargetPath Include="$(IntermediateOutputPath)$(TargetFileName).config">
<TargetPath>$(TargetFileName).config</TargetPath>
</AppConfigWithTargetPath>
</ItemGroup>
</Target>
Run Code Online (Sandbox Code Playgroud)
来自log4net.config
<connectionString name="ConnName"
value="Data Source=localhost\sqlexpress;Initial Catalog=localdb;Persist Security Info=True;Integrated Security=SSPI;" />
Run Code Online (Sandbox Code Playgroud)
从log4net.Live.config(删除敏感数据)
<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<connectionString name="ConnName" value="Data Source=127.0.0.1;Initial Catalog=DBName;Persist Security Info=True;User ID=userid;Password=pword"
providerName="System.Data.SqlClient" xdt:Transform="Replace" xdt:Locator="Match(name)" />
</configuration>
Run Code Online (Sandbox Code Playgroud)
我检查了msbuild输出,我看到它正确地转换了我的web.config,但我看到没有输出它转换log4net.此外,当我在发布后检查log4net.config文件时,它具有原始连接字符串.
我究竟做错了什么 :)?
谢谢!
更新 …
asp.net msbuild configuration visual-studio-2010 slowcheetah
我有一个自动生成的类,上面有一个属性.我想在另一个相同类型的部分类中向该属性添加一些数据注释.我该怎么办?
namespace MyApp.BusinessObjects
{
[DataContract(IsReference = true)]
public partial class SomeClass: IObjectWithChangeTracker, INotifyPropertyChanged
{
[DataMember]
public string Name
{
get { return _name; }
set
{
if (_name != value)
{
_name = value;
OnPropertyChanged("Name");
}
}
}
private string _name;
}
}
Run Code Online (Sandbox Code Playgroud)
在另一个文件中我有:
namespace MyApp.BusinessObjects
{
public partial class SomeClass
{
private SomeClass()
{
}
[Required]
public string Name{ get; set; }
}
}
Run Code Online (Sandbox Code Playgroud)
目前,我收到一条错误,指出name属性已存在.
c# ×7
.net ×3
asp.net ×3
unit-testing ×2
.net-2.0 ×1
.net-4.0 ×1
master-pages ×1
math ×1
msbuild ×1
mstest ×1
oop ×1
refactoring ×1
slowcheetah ×1
web-config ×1
webforms ×1