public class Foo
{
public void DoFoo()
{
int x;
var coll = TheFunc("bar", out x);
}
public Func<string, int, ICollection<string>> TheFunc { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
错误:"参数2不应与'out'关键字一起传递."
public class Foo
{
public void DoFoo()
{
int x;
var coll = TheFunc("bar", out x);
}
public Func<string, out int, ICollection<string>> TheFunc { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
错误:"无效的方差修饰符.只能将接口和委托类型参数指定为变体."
如何在此函数中获取out参数?
public class Foo<T>
{
public Foo(Bar bar)
{
if (bar == null) throw new ArgumentNullException("bar");
}
}
public class Foo : Foo<Object>
{
public Foo(Bar bar) : base(bar) { }
}
Run Code Online (Sandbox Code Playgroud)
基本上,我明白我应该对通用 Foo 的构造函数进行单元测试。我还应该对非通用版本的构造函数进行单元测试吗?我问的原因是因为异常是在通用构造函数级别抛出的......
[TestMethod]
[ExpectedExeption(typeof(ArgumentNullException))]
public void GenericFooNullArgumentInConstructor()
{
var foo = new Foo<int>(null);
}
//Is this test necessary?
[TestMethod]
[ExpectedExeption(typeof(ArgumentNullException))]
public void NonGenericFooNullArgumentInConstructor()
{
var foo = new Foo(null);
}
Run Code Online (Sandbox Code Playgroud) 我有一个方法,其中有很多条件:
public bool IsLegalSomething(Order order)
{
var item0 = order.Items.SingleOrDefault(x => x.ItemCode == "ItemCode0");
var item1 = order.Items.SingleOrDefault(x => x.ItemCode == "ItemCode1");
...
var itemN = order.Items.SingleOrDefault(x => x.ItemCode == "ItemCodeN");
return ((item0.Status == Status.Open) && (item1.Status == Status.Closed)
&& ...
&& (itemN.Status == Status.Canceled));
}
Run Code Online (Sandbox Code Playgroud)
我想对这个函数进行单元测试,但是条件太多了,如果考虑每一个组合,单元测试的数量就会变得疯狂。该 return 语句中有 16 个条件,并且由于每个条件都是 true/false,因此我需要检查 2^16 种不同的组合。我真的需要在此处创建 2^16 个不同的单元测试以确保利用每个条件吗?请注意,这是一个简单的例子。由于法律要求,我的一些功能具有复杂的条件:
return (condition0 && condition1 && (condition2 || condition3)
&& (condition4 || (condition5 && condition6)) ...)
Run Code Online (Sandbox Code Playgroud)
通过我的一些函数的数学计算,条件可以产生的不同组合的数量是数百万种!我研究了数据驱动单元测试(DDUT)以及参数化单元测试(PUT),但这只是使单元测试成为“填空”风格。我仍然需要提供所有的各种组合和预期的结果!例如:
// Parameterized Unit Test
[TestCase(..., Result = true)] …Run Code Online (Sandbox Code Playgroud) 基本上,我想知道下面的文件下载问题是否有最好的做法,不仅仅是临时使用,而是最终将它们移动到应用程序文件夹.我面临一些选择:
//Option 1 - Random file
String tempfile = Path.GetTempFileName();
WriteData(tempfile);
File.Move(tempfile, Path.Combine(Environment.CurrentDirectory, filename);
//Option 2 - Temp Path + Random file name
String tempfile = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
WriteData(tempfile);
File.Move(tempfile, Path.Combine(Environment.CurrentDirectory, filename);
//Option 3 - Temp Path + real file name
String tempfile = Path.Combine(Path.GetTempPath(), filename);
WriteData(tempfile);
File.Move(tempfile, Path.Combine(Environment.CurrentDirectory, filename);
//Option 4 - Temp Application Path + Random file name
String tempfile = Path.Combine(Environment.CurrentDirectory, Settings.Default.DownloadFolder, Path.GetRandomFileName());
WriteData(tempfile);
File.Move(tempfile, Path.Combine(Environment.CurrentDirectory, filename);
//Optioin 5 - Temp Application Path + file name
String …Run Code Online (Sandbox Code Playgroud) Prismv4和MEF有点新鲜.
我浏览了快速入门并尝试将它们中的两个组合在一起,但我似乎无法让它工作.
首先,我有一个Bootstrapper来加载Shell Window.
public sealed class ClientBootstrapper : MefBootstrapper
{
protected override void ConfigureAggregateCatalog()
{
base.ConfigureAggregateCatalog();
//Add this assembly to export ModuleTracker (Shell is in this Assembly).
AggregateCatalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly()));
}
protected override DependencyObject CreateShell()
{
return Container.GetExportedValue<Shell>();
}
protected override void InitializeShell()
{
base.InitializeShell();
Application.Current.MainWindow = (Window)Shell;
Application.Current.MainWindow.Show();
}
}
Run Code Online (Sandbox Code Playgroud)
这很好.弹出了Shell窗口,出现了一个很好的Hello World消息.然后我尝试在Shell窗口中创建一个Region,这样我就可以将View加载到该区域.我甚至没有把它用于甚至将它移动到外部组件上.
[ModuleExport(typeof(HelloWorldModule), InitializationMode = InitializationMode.OnDemand)]
public class HelloWorldModule : IModule
{
[Import(AllowRecomposition = true)]
private IRegionViewRegistry regionViewRegistry;
[ImportingConstructor()]
public HelloWorldModule(IRegionViewRegistry registry)
{
this.regionViewRegistry = registry;
}
public void …Run Code Online (Sandbox Code Playgroud) 我有一个ListView数据绑定到ObservableCollection...
<ListView x:Name="List1" ItemsSource="{Binding MyList}" />
Run Code Online (Sandbox Code Playgroud)
我似乎无法找到任何在集合发生变化时触发的事件,所以我想我不知何故需要以某种方式挂钩到collectionschanged通知中?我真的不确定该怎么做.
基本上,当集合发生变化时,我想做更多的工作,而不是ListView在更新它的列表时所做的工作.
wpf listview observablecollection inotifycollectionchanged itemssource
我有以下内容:
public class Foo
{
public int x { get; set; }
}
public class Bar
{
public void DoWork(IEnumerable<Foo> foos)
{
var enumOfX = ?;
//Other code that uses enumOfX
}
}
Run Code Online (Sandbox Code Playgroud)
如何创建IEnumerable<int>所有x的?
基本上,我总是在理解你应该尽可能地返回公开基类型并且在内部担心实现细节,这是有道理的......
但是,我不知道该怎么做.基本上,我现在有:
ReadOnlyObservableCollection<Foo> MyFoos {get; private set; }
Run Code Online (Sandbox Code Playgroud)
我想知道是否应该将其作为a ReadOnlyCollection<Foo>或ICollection<Foo>因为内部返回,因为我从未真正使用任何可观察的部分或尝试写入集合.WPF似乎不关心我返回什么,它仍然绑定它并正确触发集合更改通知事件.但是,我在某处读到了我应该设计这个以确实有任何消费视图处理我的ViewModel.
所以我在这里有点不知所措.我认为将它ReadOnlyObservableCollection<T>留作最有意义的是明确地告诉消费者视图他们可以和不能对财产做什么,但我也认为你应该减少类型到他们的基本类型当你能够.所以我不知道该怎么做.特别是因为WPF不关心我返回的类型,它会发现它是可观察的.
public IPredefinedInterface
{
void DoSomething(Object obj);
}
public class MyClass<T> : IPredefinedInterface
{
public void DoSomething(Object obj)
{
if (!(obj is T)) throw new ???
SomeOtherFunc((T)obj);
}
}
Run Code Online (Sandbox Code Playgroud)
不知道这里适当的异常是什么... ArgumentException,InvalidCastException等?
我想知道CLR类型是否会返回以下不同的结果:
Object.Equals(objA, objB)
objA.Equals(objB)
(objA == objB)
Run Code Online (Sandbox Code Playgroud)
我确实意识到在CLR之外有人可以轻松地实现IEqualtableEquals并且不正确地重载==运算符.我不关心那些不正当地实施这些的人.我所关注的是类(包括String,Int32等)以不同的方式实现这3个.
此外,如果可能的话,哪一个应该是用于整体比较的(全面).我想知道这是因为我遇到了一个文件,它使用Object.Equals(objA, objB)了整个视图模型而不是其他两个.
private string _name;
public string Name
{
get { return _name; }
set
{
if (Equals(_name, value)) return;
...
}
}
private int _id;
public int Id
{
get { return _id; }
set
{
if (Equals(_id, value)) return;
...
}
}
private Object _obj;
public Object TheObject
{
get { return _obj; }
set
{
if (Equals(_obj, value)) return;
...
}
}
Run Code Online (Sandbox Code Playgroud) c# ×9
wpf ×3
unit-testing ×2
.net ×1
casting ×1
exception ×1
func ×1
generics ×1
ienumerable ×1
iequatable ×1
itemssource ×1
linq ×1
listview ×1
mef ×1
mvvm ×1
out ×1
prism-4 ×1
regions ×1