比如说我创造了一只鸭子
Duck myDuck = DuckFactory.CreateDuck();
Run Code Online (Sandbox Code Playgroud)
在过去,我总是检查myDuck是否为空
if (myDuck == null)
{
// Do stuff
}
Run Code Online (Sandbox Code Playgroud)
我最近查看了一些首先检查null的代码.
if (null == myDuck)
{
// Do stuff
}
Run Code Online (Sandbox Code Playgroud)
在我看来,这些是相同的,但这两者之间有什么不同吗?一个人对另一个人有任何表现上的好处吗?是否有建议的最佳做法来检查对象是否为空?
我试图从我的ASP.NET 2.0 WebForms应用程序中运行的WCF Web服务获取JQGrid的数据.问题是WCF Web服务期望将数据格式化为JSON字符串,并且JQGrid正在执行HTTP Post并将其作为Content-Type传递:application/x-www-form-urlencoded.
虽然返回到JQGrid的数据格式似乎有几种选择(它接受JSON,XML等),但似乎没有办法改变它将输入传递给Web服务的方式.
所以我试图找出如何调整WCF服务以便它接受
Content-Type: application/x-www-form-urlencoded
Run Code Online (Sandbox Code Playgroud)
而不是
Content-Type:"application/json; charset=utf-8"
Run Code Online (Sandbox Code Playgroud)
当我使用JQuery进行测试以使用url编码发送Ajax请求时(如下所示):
$.ajax({
type: "POST",
url: "../Services/DocLookups.svc/DoWork",
data: 'FirstName=Howard&LastName=Pinsley',
contentType: "Content-Type: application/x-www-form-urlencoded",
dataType: "json",
success: function(msg) {
alert(msg.d);
}
});
Run Code Online (Sandbox Code Playgroud)
呼叫失败.使用Fiddler检查流量,我发现服务器返回的错误:
{"ExceptionDetail":{"HelpLink":null,"InnerException":null,"Message":
"The incoming message has an unexpected message format 'Raw'. The expected
message formats for the operation are 'Xml', 'Json'. This can be because
a WebContentTypeMapper has not been configured on the binding.
See the documentation of WebContentTypeMapper for more details."...
Run Code Online (Sandbox Code Playgroud)
请注意,由于编码的不同,此代码可以正常工作
$.ajax({
type: "POST",
url: "../Services/DocLookups.svc/DoWork", …Run Code Online (Sandbox Code Playgroud) 好的,这就是我到目前为止...感谢Paolo.
它工作正常,但只有我有现有的单选按钮选项.
如果我需要创建一个没有任何预先存在的新单选按钮怎么办?
最终我想要做的是创建一个选项数组,循环它们并输出一个选项列表作为单选按钮.所以最初,"abc"div中没有单选按钮.
提前致谢!
<script>
$(document).ready(function() {
// add a new input when a new color is chosen, for example
$('#aaa').change(function() {
var radio = $('<input>').attr({
type: 'radio', name: 'colorinput', value: '2', id: 'test'
});
$(':radio:last-child', '#abc').after(radio).after('option 3 ');
});
});
</script>
<form id='abcdef'>
<select id="aaa">
<option>red</option>
<option>blue</option>
<option>other</option>
</select>
<div id="abc">
Input<BR>
option 1 <input type="radio" name="colorinput" value="1" />
option 2 <input type="radio" name="colorinput" value="2" />
</div>
<BR>
</form>
Run Code Online (Sandbox Code Playgroud) 可以说我有一些接口:
public interface IFoo {
IBar DoesStuff();
}
public interface IBar {
string Thingo { get; }
}
Run Code Online (Sandbox Code Playgroud)
我在整个代码库中使用此代码.需要将IFoo进程移动到不同的系统上(x64与x32的差异),这是我们使用WFC的原因.我的WCF服务实现了此接口.当我创建"服务引用"时,会创建代理存根,但会更改接口.
public interface IFoo {
object DoesStuff();
}
Run Code Online (Sandbox Code Playgroud)
我尝试将IBar/Bar定义为DataService和DataContract,没有区别.有没有办法使用我的界面生成代理代码?
我在想,如果模拟对象可以生成我的界面对象进行测试,那么我是否应该能够获得服务来尊重它?或者做了一些愚蠢和错误的事情?
在阅读了有关LINQ的书之后,我正在考虑重写我在c#中编写的mapper类以使用LINQ.我想知道是否有人能帮助我.注意:它有点令人困惑,但User对象是本地用户,而用户(小写)是从Facebook XSD生成的对象.
原始Mapper
public class FacebookMapper : IMapper
{
public IEnumerable<User> MapFrom(IEnumerable<User> users)
{
var facebookUsers = GetFacebookUsers(users);
return MergeUsers(users, facebookUsers);
}
public Facebook.user[] GetFacebookUsers(IEnumerable<User> users)
{
var uids = (from u in users
where u.FacebookUid != null
select u.FacebookUid.Value).ToList();
// return facebook users for uids using WCF
}
public IEnumerable<User> MergeUsers(IEnumerable<User> users, Facebook.user[] facebookUsers)
{
foreach(var u in users)
{
var fbUser = facebookUsers.FirstOrDefault(f => f.uid == u.FacebookUid);
if (fbUser != null)
u.FacebookAvatar = fbUser.pic_sqare;
}
return users;
} …Run Code Online (Sandbox Code Playgroud) 我有一个LINQ语句,我想在First地址中合并昵称为'Me'.
using (var ctx = new DataEntities())
{
return from c in ctx.Customers.Include("Addresses")
let m = from a in c.Addresses where a.Nickname == "Me" select a
where m.Any()
select new
{
Id = c.CustomerId,
m.First().Name,
m.First().Address1,
m.First().Address2,
m.First().City,
m.First().State,
m.First().Zip,
m.First().Email,
m.First().PhoneNumber
};
}
Run Code Online (Sandbox Code Playgroud)
我在想:
当我尝试将文本块添加到边框元素时,我只看到文本的一部分.我将文本添加到边框后旋转文本,这是导致问题的原因.增加边框的宽度可以解决此问题.但是,我的边界只需要20个单位.
alt text http://img257.imageshack.us/img257/1702/textcrop.jpg
我在这里失踪了什么?
<Border
Name="BranchBorder"
CornerRadius="0"
HorizontalAlignment="Left"
Width="20">
<TextBlock
Name="Branch"
FontSize="14"
FontWeight="Bold"
VerticalAlignment="Center">
<TextBlock.RenderTransform>
<RotateTransform
Angle="-90"/>
</TextBlock.RenderTransform>
Branch
</TextBlock>
</Border>
Run Code Online (Sandbox Code Playgroud) 你可以对上传到S3的文件设置权限吗?
或者如果有人知道该文件的公会/网址,他们可以公开访问它?
如果是,我们可以设置什么样的权限?
我的DOM看起来像:
<div id="blah-1">
<div class="class1">
<div class="class11>
<a href=""><img src=""></a> <b>blah</b>
<a href=""><img src=""></a>
</div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
我必须改变第一个或有时第二个img的来源.
我正在使用jQuery,并且还没有强大的选择器处理能力!