比方说,我有一个有一组整数的类
foo()
{
int a;
int b;
int c;
int d;
....
string s;
}
Run Code Online (Sandbox Code Playgroud)
现在的问题是最好的可读性,foo()的init()函数,如果它看起来像
void init()
{
a=b=c=d=1; //for some reason they are init to 1;
s = "abc";
}
Run Code Online (Sandbox Code Playgroud)
要么
void init()
{
a=1;
b=1;
c=1;
d=1;
s = "abc";
}
Run Code Online (Sandbox Code Playgroud)
?
类中字符串的原因是可能存在其他相同类型的组的提示,当然,类可能会随着需求的变化而增长
编辑:在这个问题走得太远之前,这个问题的意图很简单:在Effective C++第12项中(更喜欢初始化到构造函数中的赋值),Scott使用链赋值而不是a = c; B = C; 我确定他知道什么时候使用什么,但我也记得我读过的书也建议使用int a; int b; 在类似的作业情况下.在我的程序中我有一个类似的情况,需要初始化一组相关的单个内置类型,我发现通过使链分配确实使它更容易阅读,特别是如果类有许多其他不同类型的实例变量.它似乎与我读过的书和我的记忆相矛盾,因此这个问题.
我想知道是否有更好的方法来更改字典键,例如:
var dic = new Dictionary<string, int>();
dic.Add("a", 1);
Run Code Online (Sandbox Code Playgroud)
后来我决定将键值对设为("b",1),是否可以重命名键而不是添加新的键值对("b",1)然后删除"a" ?
提前致谢.
是否有选项只显示失败的测试?我不得不切换到使用吉他来实现这一点,但我想念命令行工具.
我是Ruby新手,使用ruby 1.9.2P180和Rails 3.1.0.rc2
我在my_app_root/public/stylesheets/screen.css和我的application.html.erb中有"screen.css"
<%= stylesheet_link_tag 'screen.css', :media => 'screen' %>
Run Code Online (Sandbox Code Playgroud)
根据这里它应该工作,但我的rails服务器说:
由PagesController#home作为HTML处理在布局/应用程序内呈现页面/ home.html.erb(0.0ms)在4ms内完成200 OK(视图:3.6ms | ActiveRecord:0.0ms)
在2011-06-18 11:27:53开始GET"/assets/screen.css"为127.0.0.1 +1200服务资产/screen.css - 404未找到(2ms)(pid 10966)
ActionController :: RoutingError(没有路由匹配[GET]"/assets/screen.css"):
我在这做错了什么?
提前致谢
我们总是需要尝试捕获我们的代码,它变得丑陋像
public void foo()
{
try
{
DoSomething();
}
catch(Exception e)
{
//do whatever with e
}
}
public int FooReturnInt()
{
try
{
return IntAfterSomeCalculation();
}
catch(Exception e)
{
//do exactly whatever with e as foo()
}
}
Run Code Online (Sandbox Code Playgroud)
想象一下,我们有一个庞大的类,有许多像这样的公共函数,我们必须在每个函数中应用相同的try catch.
理想情况下,因为try catch部分是相同的,我们可以将Func <>作为参数传递给辅助函数,它执行类似的操作
public void TryCatch(Func<something> theFunction)
{
try
{
theFunction();
}
catch(Exception e)
{
//processthe common exception
}
}
Run Code Online (Sandbox Code Playgroud)
然后我想象这会整理我的代码很多,现在的问题是如何正确编写这个函数?此函数的返回类型取决于函数的返回类型.
我知道如果我们使用datacontract,我们可以对类进行更多控制,但是,请考虑以下2种情况
[DataContract]
public class Customer
{
[DataMember]
public string CustomerName {get; set;}
[DataMember]
public int Age{get; set;}
}
Run Code Online (Sandbox Code Playgroud)
和
public class Customer
{
public string CustomerName {get; set;}
public int Age{get; set;}
}
Run Code Online (Sandbox Code Playgroud)
它们都在.net客户端上正确序列化.我个人不用第二个例子.任何人都可以指出我在两个班级的差异吗?我打算发送这两个类中的所有公共属性.
我遇到了一个非常有趣的可靠会话行为.我正在使用netTcp绑定+双工通道+可靠会话.
当我试图收听channel.faulted时,如果安全模式设置为transport,则当客户端断开连接时,故障事件将立即触发.
但是,当我将绑定的安全模式设置为None或Message时,故障事件不再在相同的情况下触发.他们最终会在服务器端出现故障的一半ReciveTimeout,我理解当时可靠的会话会发送心跳消息.
问题是:为什么wcf绑定不会出现故障?
这种情况的解决方法是我可以手动"ping"连接.
我已经搜索过,并且在SO上有类似的问题,但是,没有人谈论如何在"All Profile"(Windows 7,AKA"任何配置文件"在Vista/Windows Server 2008上)添加例外.互联网上的示例仅涉及添加到当前配置文件.
原因是我的虚拟机有一个问题:windows 2008 x86,当前的防火墙配置文件是Domain,我的应用程序被添加到Domain的Exception列表中.(防火墙设置为默认设置:阻止任何不在例外列表中的入站呼叫.)但是,入站呼叫仍会被阻止,除非:1.关闭此虚拟机上的防火墙.2.手动将我的应用程序的规则配置文件更改为"任何"
这是非常令人困惑,因为我以为只有活动的配置文件应该是"积极的",应该是功能性的,不管其他配置文件挡住了我的申请呼入电话.
我正在使用XPSP2 INetFwMgr接口来添加缺少"任何"配置文件支持的异常.
我正在使用c#,但任何语言都会受到赞赏.
我有一个使用net tcp duplex回调的自托管wcf服务
在客户端,我在Channel和ChennelFactory上听故障事件.当频道出现故障时,客户端将重新创建频道并重新订阅.
在服务器端,我通过调用存储回调通道和存储通道的引用,OperationContext.Current.Channel
以便我可以监听此通道上的故障和已关闭事件.出现故障时,服务器将删除该订户.
这大部分时间都在工作,直到最近我发现了一个意外的行为:回调通道在服务器上出现故障,但通道在客户端似乎没问题,这导致客户端在服务器已经删除了该用户和客户端时没有重新订阅不会收到任何回调.
我想在双工通信上,如果一端检测到故障,双工通道应该出现故障.
启用可靠会话并且超时很长(2小时)(这可能会导致客户端无法快速感知故障?)
谁能解释为什么会这样?
另一个问题是它为何会出现故障?我连接了40个客户端,但服务器会在随机时间检测到随机客户端出现故障.
在我的VS2010上,当intellisense显示时,如果我按下Ctrl键,整个intellisense将变暗,很难看到直到Ctrl键被释放.
这非常烦人,因为我使用autohotkey将我的向上箭头键重新加载到Ctrl+ J和Ctrl+ K来保存我的RSI.
有没有人知道一个选项/解决方法来配置键绑定以导航intellisense或使其不变暗?
我有 WCF 服务,在一个 ServiceHost 内的不同端点使用 BasicHttpBinding 和 NetTcpBinding。NetTcp 使用自签名证书,该证书是从文件加载的,一切都很好,直到我尝试实际使用 BasicHttpBinding,所以我这样做:
在服务器上:
var ServiceHost host = new ServiceHost(blah blah);
host.Credentials.ServiceCertificate.Certificate = GetCertificate(); //load a certificate from file
host.Credentials.ClientCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.None;
var httpBinding = new BasicHttpBinding();
httpBinding.Security.Mode = BasicHttpSecurityMode.Transport;
httpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;
Run Code Online (Sandbox Code Playgroud)
在客户端:
ChannelFactory.Credentials.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.None;
var cer = GetCertificate();
ChannelFactory.Credentials.ClientCertificate.Certificate = cer;
var httpBinding = new BasicHttpBinding();
httpBinding.Security.Mode = BasicHttpSecurityMode.Transport;
httpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;
//accept any cert
System.Net.ServicePointManager.ServerCertificateValidationCallback =
((sender, certificate, chain, sslPolicyErrors) => true);
Run Code Online (Sandbox Code Playgroud)
但是当连接时,我收到此错误
异常 - 向https://localhost/MyService发出 HTTP 请求时发生错误 …
我试图弄清楚如何在自托管WCF应用程序中多次打开和关闭ServiceHost对象:
我做了什么:1.创建ServiceHost对象
在这种情况下,如何在不重新创建该对象的情况下再次打开ServiceHost(相反,如果可能的话,我将重新创建CommunicationObject).
提前致谢.
我试图使用"使用"最小化实体框架连接上下文范围,而在平均时间我希望能够将一个上下文注入我的类.我在互联网上搜索但是没有找到像我这样的情况,或者我只是做错了什么,无论如何,这里是代码:
[TestFixture]
public class Dummy
{
private IFoo ifoo;
[Test]
public void CreateIfNotExist()
{
using (var foo = GetNewIFoo())
{
foo.Dosomething();
}
Assert.IsNull(ifoo);//test fail here
}
[Test]
public void NotCreateIfExist()
{
ifoo = new Bar();
using (var foo = GetNewIFoo())
{
foo.Dosomething();
}
Assert.IsNull(ifoo);//test fail here
}
private IFoo GetNewIFoo()
{
if (ifoo == null)
{
ifoo = new Foo();//return new Foo();
}
return ifoo;
}
}
Run Code Online (Sandbox Code Playgroud)
第一个测试失败,创建了foo的对象序列 - > foo做某事 - > foo处置(使用on foo调用),而状态变量ifoo仍然是Foo()的类型.
第二次测试失败,对象生命序列与之前相同.
我很困惑因为我认为GetNewIFoo()会返回ifoo的引用而使用关键字只会在ifoo上调用dispose?
另外,有没有什么好方法可以控制上下文范围,同时保持注入IContext的能力?
c# ×6
wcf ×5
c++ ×2
windows ×2
.net ×1
c#-4.0 ×1
callback ×1
certificate ×1
css ×1
datacontract ×1
dictionary ×1
duplex ×1
faulted ×1
firewall ×1
googletest ×1
intellisense ×1
keyword ×1
reliability ×1
ssl ×1
try-catch ×1
using ×1