我的代码中有以下几行.他们占据的位置比应有的多.对较小代码的任何建议.
string longestString;
string shortestString;
if (string1.Length > string2.Length)
{
longestString = string1;
shortestString = string2;
}
else
{
longestString = string2;
shortestString = string1;
}
Run Code Online (Sandbox Code Playgroud)
我知道,这不是一个非常重要的问题,但这是整个方法的2/3,而不是重要的东西.
使用 xUnit 时,可以使用 InlineData 属性对不同的数据多次运行相同的测试。
[Theory]
[InlineData("A", 1)]
[InlineData("B", 2)]
public void TestAllValues(string x, int y)
Run Code Online (Sandbox Code Playgroud)
我想在所有可能的组合中组合这些参数。我可以写如下。
[Theory]
[InlineData("A", 1)]
[InlineData("A", 2)]
[InlineData("A", 3)]
[InlineData("B", 1)]
[InlineData("B", 2)]
[InlineData("B", 3)]
public void TestAllValues(string x, int y)
Run Code Online (Sandbox Code Playgroud)
在我的情况下,我需要测试更多的组合,比如字母表中的每个字母以及从 1 到 100 的每个数字。我喜欢写一些类似的东西
[Theory]
[InlineData("A-Z", 1..100)]
public void TestAllValues(string x, int y)
Run Code Online (Sandbox Code Playgroud)
或任何不需要 2.600 行的等价物。这个例子是为了简单起见,但我真的需要很多案例来测试。
作为奖励问题。我可以在测试名称中反映组合吗?
我基本上有以下类(在C#上创建的示例为泛型类创建隐式转换?).
class MyClass<T>
{
public MyClass(T val)
{
Value = val;
}
public T Value { get; set; }
public static implicit operator MyClass<T>(T someValue)
{
return new MyClass<T>(someValue);
}
public static implicit operator T(MyClass<T> myClassInstance)
{
return myClassInstance.Value;
}
}
Run Code Online (Sandbox Code Playgroud)
一个人可以做到
MyClass<IFoo> foo1 = new Foo();
MyClass<Foo> foo2 = new Foo();
//But not
MyClass<IFoo> foo3 = (IFoo)new Foo();
Run Code Online (Sandbox Code Playgroud)
当尝试做类似的事情时,会出现真正的问题
void Bar(IFoo foo)
{
Bar2(foo);
//What should be the same as
Bar2<IFoo>(new MyClass<IFoo>(foo));
}
void Bar2<T>(MyClass<T> …Run Code Online (Sandbox Code Playgroud) 我有这种情况,我尝试在创建的同一个线程上处理事件.这通常在UiThread中完成,但我不是在UiThread上开始.我有一些测试基本上有以下步骤.我遗漏了一些细节.我不确定这是否应该按照我认为的那样行事.
首先,我检查当前线程的Id
var myThreadId = Thread.CurrentThread.ManagedThreadId;
Run Code Online (Sandbox Code Playgroud)
我创建了一个SynchronizationContext,并设置为当前
var _context = new SynchronizationContext();
SynchronizationContext.SetSynchronizationContext(_context);
Run Code Online (Sandbox Code Playgroud)
然后我发送一些动作到上下文(我们现在在另一个线程)
_context.Send(x => _action(sender, e), null);
Run Code Online (Sandbox Code Playgroud)
在这个动作中,我再次检查ThreadId
Assert.Equal(myThreadId, Thread.CurrentThread.ManagedThreadId);
Run Code Online (Sandbox Code Playgroud)
这失败了.我不应该再次使用原始帖子吗?
只是尝试从本地网络中的设备接收 UPnP 广播。我确实发现了很多类似的问题并尝试了很多建议,但没有一个主题成功。我确实用 Wireshark 看到了 UDP 数据包,所以它们实际上是在我的计算机上收到的。有什么建议么?
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
public class UDPListener
{
private const int listenPort = 1900;
private static void StartListener()
{
bool done = false;
IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, listenPort);
UdpClient listener = new UdpClient();
listener.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
listener.Client.Bind(localEndPoint);
listener.JoinMulticastGroup(IPAddress.Parse("239.255.255.250"));
listener.MulticastLoopback = true;
IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, 0);
try
{
while (!done)
{
Console.WriteLine("Waiting for broadcast");
var bytes = listener.Receive(ref groupEP);
Console.WriteLine("Received broadcast from {0} :\n {1}\n",
groupEP.ToString(),
Encoding.ASCII.GetString(bytes, …Run Code Online (Sandbox Code Playgroud) 考虑到SslStream.AuthenticateAsServer方法,第二个参数clientCertificateRequired:
如果设置为 true,则需要客户端证书。否则会抛出异常。客户端证书将在属性RemoteCertificate 中可用。
当设置为 false 时,不需要客户端证书,属性RemoteCertificate应始终为 null。即使是客户提供的。
我喜欢完成的是让客户决定是否提供证书。但是,如果他们确实提供了一个,我想在服务器上知道它。
我试图首先将变量设置为 true,如果失败,则回退到不需要证书。但是,这会导致“已验证异常”。
try{
sslStream.AuthenticateAsServer(x509certificate, true, SslProtocols.Tls, true);
}catch(Exception ex){
sslStream.AuthenticateAsServer(x509certificate, false, SslProtocols.Tls, true);
}
Run Code Online (Sandbox Code Playgroud) 假设一个班级(可能是其他人)
class Foo()
{
IEnumerable<int> SomeNumbers { get; set; }
IEnumerable<string> SomeStrings { get; set; }
int[] ArrayOfInt { get; set; }
List<int> AListOfIntegers { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
和方法
void Initialize(object obj)
{
var props = obj.getType().GetProperties();
foreach(var prop in props)
{
//Some logic in case property is a single value, this is using:
//Convert.ChangeType(value, property.PropertyType, null)
var list = //Do Some Magic
prop.SetValue(obj, list, null);
}
}
Run Code Online (Sandbox Code Playgroud)
如何创建正确类型的列表?
我试过了:
var list = property.PropertyType.GetConstructor(new CType[0]).Invoke(new object[0]);
Run Code Online (Sandbox Code Playgroud)
但这不起作用,因为IEnumerable没有构造函数(它是一个接口),但任何超类型都可以.
var list …Run Code Online (Sandbox Code Playgroud) c# ×6
.net ×1
broadcast ×1
generics ×1
ienumerable ×1
reflection ×1
ssl ×1
sslstream ×1
unit-testing ×1
upnp ×1
xunit ×1
xunit.net ×1