有没有人有一个好的和有效的扩展方法来查找一系列项目是否有任何重复?
我猜我可以return subjects.Distinct().Count() == subjects.Count()
使用扩展方法,但有点觉得应该有更好的方法.该方法必须对元素进行两次计数并整理所有distict元素.更好的实现应该在它找到的第一个副本上返回true.有什么好建议吗?
我想大纲可能是这样的:
public static bool HasDuplicates<T>(this IEnumerable<T> subjects)
{
return subjects.HasDuplicates(EqualityComparer<T>.Default);
}
public static bool HasDuplicates<T>(this IEnumerable<T> subjects, IEqualityComparer<T> comparer)
{
...
}
Run Code Online (Sandbox Code Playgroud)
但不太确定如何巧妙地实施它......
我想知道哪一个被认为是最干净或最好用的,为什么.
其中一个公开了一个乘客列表,让用户添加和删除等.另一个隐藏列表,只让用户枚举它们并使用特殊方法添加.
例1
class Bus
{
public IEnumerable<Person> Passengers { get { return passengers; } }
private List<Passengers> passengers;
public Bus()
{
passengers = new List<Passenger>();
}
public void AddPassenger(Passenger passenger)
{
passengers.Add(passenger);
}
}
var bus = new Bus1();
bus.AddPassenger(new Passenger());
foreach(var passenger in bus.Passengers)
Console.WriteLine(passenger);
Run Code Online (Sandbox Code Playgroud)
例2
class Bus
{
public List<Person> Passengers { get; private set; }
public Bus()
{
Passengers = new List<Passenger>();
}
}
var bus = new Bus();
bus.Passengers.Add(new Passenger());
foreach(var passenger in bus.Passengers)
Console.WriteLine(passenger); …
Run Code Online (Sandbox Code Playgroud) 试图将用Python编写的一些方法转换为C#.这条线看起来像这样:
d[p] = d.setdefault(p, 0) + 1
Run Code Online (Sandbox Code Playgroud)
setdefault究竟做了什么?我可以在C#字典中使用类似的东西吗?或者更确切地说,我如何将该行转换为C#?
假设我们有这个超级简单的类hierchy:
public class SomeMath
{
public int Add(int x, int y)
{
return x + y;
}
}
public class MoreMath : SomeMath
{
public int Subtract(int x, int y)
{
return x - y;
}
}
Run Code Online (Sandbox Code Playgroud)
Add
在为MoreMath
类编写测试时,我应该为该方法编写测试吗?或者我在测试SomeMath
课程时是否应该只关心这种方法?更一般地说:我应该测试一个类的所有方法,还是应该只测试"新"方法?
我可以想出双方的一些理由.例如,在测试所有方法时,您最终会不止一次地测试相同的东西,这不是很好,而且可能变得乏味.但是,如果你不测试所有方法,改变SomeMath
可能会破坏用法MoreMath
?这也是一件坏事.我想这也可能取决于案例.就像它扩展了一个类,我可以控制或不控制.但无论如何,我是一个全新的测试新手,所以我很想知道人们比我想的更聪明:-)
我有一个带有以下签名的扩展方法:
public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second)
{
...
}
Run Code Online (Sandbox Code Playgroud)
我已经为它编写了一个测试用例,确保两个表达式实际上是组合在一起的.至少这样我的新表达式才有效.
现在我想编写另一个测试用例,确保该方法使用的短路版本and
.任何线索我怎么能这样做?
我以为我可以这样做:
[Test]
public void And_PredicatesAreShortCircuited()
{
var predicateNotUsed = true;
Expression<Func<int, bool>> a = x => false;
Expression<Func<int, bool>> b = x =>
{
predicateNotUsed = false;
return true;
};
var foo = new[] { 1, 2, 3, 4, 5, 6, 7 }
.Where(a.And(b).Compile())
.ToArray();
Assert.That(predicateNotUsed);
}
Run Code Online (Sandbox Code Playgroud)
但是我在整个声明体下得到了一个巨大的红色波浪形,b
表示"带有语句体的lambda表达式无法转换为表达式树".所以...任何选择?或者这是一个不可能的测试?
我在.NEt 2.0中使用c#来简单地尝试上传文件.代码中的一切似乎都没问题,但是当我从FtpWebRequest.GetRequestStream方法创建流时它仍然失败.
这是代码......
FtpWebRequest ftpRequest;
FtpWebResponse ftpResponse;
try
{
string fileName = Path.GetFileName(strCompleteFilePath);
ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://myhost/" + fileName));
ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
ftpRequest.Proxy = null;
ftpRequest.UseBinary = true;
ftpRequest.Credentials = new NetworkCredential("myUserID", "myPW");
ftpRequest.KeepAlive = false;
FileInfo ff = new FileInfo(strCompleteFilePath);
byte[] fileContents = new byte[ff.Length];
using (FileStream fr = ff.OpenRead())
{
fr.Read(fileContents, 0, Convert.ToInt32(ff.Length));
}
using (Stream writer = ftpRequest.GetRequestStream())
{
writer.Write(fileContents, 0, fileContents.Length);
}
ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
}
Run Code Online (Sandbox Code Playgroud)
而错误....
{System.Net.WebException: The remote server returned an error: (501) Syntax …
Run Code Online (Sandbox Code Playgroud) 在Eclipse中创建了一个新的标准java 7项目,并成功地设法获得了javax.xml.ws.Service
类似的实例:
String wsdlURL = "http://example.com:3000/v1_0/foo/bar/SomeService?wsdl";
String namespace = "http://foo.bar.com/webservice";
String serviceName = "SomeService";
QName serviceQN = new QName(namespace, serviceName);
Service service = Service.create(new URL(wsdlURL), serviceQN);
Run Code Online (Sandbox Code Playgroud)
这在主方法中运行良好,所以据我所知,该部分有效.但我无法弄清楚如何实际使用它.在SoapUI中,我使用如下所示的请求调用此相同的服务:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://foo.bar.com/webservice">
<soapenv:Header/>
<soapenv:Body>
<web:SomeWebServiceRequest acAccountName="name" acAccountPassword="password">
<SomeRequest>
<id>012345678901234</id>
<action>Fix</action>
</SomeRequest>
</web:SomeWebServiceRequest>
</soapenv:Body>
</soapenv:Envelope>
Run Code Online (Sandbox Code Playgroud)
如何在Java中执行相同的请求?我的目标是我有很长的列表id
,我需要为每个人运行这样的请求.在SoapUI中手动执行它有点烦人,因此我想使用简单的Java控制台应用程序自动执行它.
我有一个XSLT样式表,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:XQHeaderFunc="java:com.sonicsw.xq.service.xform.HeaderExtension"
xmlns:saxon="http://saxon.sf.net/">
<saxon:script language="java" implements-prefix="XQHeaderFunc" src="java:com.sonicsw.xq.service.xform.HeaderExtension" />
<xsl:output indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="/">
<xsl:variable name="processId" select="XQHeaderFunc:getProperty(XQHeaderFunc:new(),'processId',-1)" />
<xsl:value-of select="XQHeaderFunc:setProperty(XQHeaderFunc:new(),'processId',string(@id),-1)"/>
<root>
<xsl:apply-templates />
</root>
</xsl:template>
<!-- Other stuff -->
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
我想使用第二个XSLT样式表来转换此样式表,以删除与XQHeaderFunc和saxon命名空间有关的任何内容.有没有办法可以做到这一点?
我现在尝试了以下,成功处理元素,但命名空间声明似乎不想消失.
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:XQHeaderFunc="java:com.sonicsw.xq.service.xform.HeaderExtension"
xmlns:saxon="http://saxon.sf.net/"
exclude-result-prefixes="XQHeaderFunc saxon">
<xsl:param name="XQHeaderReplacement" />
<xsl:variable name="apos">'</xsl:variable>
<!-- Copy all nodes -->
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<!-- Remove saxon script tag -->
<xsl:template match="saxon:script" />
<!-- Remove elements with setProperty calls -->
<xsl:template match="*[starts-with(@select, 'XQHeaderFunc:setProperty')]" …
Run Code Online (Sandbox Code Playgroud) 是否可以创建一个类型必须具有索引器的泛型类/方法?
我的想法是让以下两种扩展方法适用于使用索引器获取和设置值的任何类型,但似乎无法找到任何关于它的内容.只有关于使索引器本身通用的东西,这不是我追求的......
public static T GetOrNew<T>(this HttpSessionStateBase session, string key) where T : new()
{
var value = (T) session[key];
return ReferenceEquals(value, null)
? session.Set(key, new T())
: value;
}
public static T Set<T>(this HttpSessionStateBase session, string key, T value)
{
session[key] = value;
return value;
}
Run Code Online (Sandbox Code Playgroud) c# ×7
unit-testing ×2
class ×1
dictionary ×1
duplicates ×1
ftp ×1
ienumerable ×1
indexer ×1
java ×1
java-7 ×1
lambda ×1
php ×1
python ×1
soap ×1
tail ×1
web-services ×1
xslt ×1
xslt-1.0 ×1