我有一个WPF Window,在某个地方有一个ListView我绑定List<string>到的地方.
现在在什么地方我ListView有一个TextBox和Content属性设置为{Binding}.
但这是速记.如何编写完整绑定以绑定到自身?
{Binding Path=Self}不起作用,也不起作用{Binding Self}(后者是前者的捷径).
我正在尝试更改由其创建的html输入的发出名称@Html.HiddenFor.
我正在使用的代码:
@Html.HiddenFor(e => e.SomeProperty, new { @id = "some_property", @name = "some_property" }
Run Code Online (Sandbox Code Playgroud)
现在这适用于id,但它不适用于名称.现在我真的不关心id现在,我需要name改变,因为那是被发布回目标服务器的那个.
在那儿
SomeProperty在我的模型中申请的财产?Html.HiddenFor覆盖name属性的方法?还是我坚持<input ...>用手做一个平原?
请考虑以下代码:
namespace DisposeTest
{
using System;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Calling Test");
Test();
Console.WriteLine("Call to Test done");
}
static void Test()
{
DisposeImplementation di = new DisposeImplementation();
}
}
internal class DisposeImplementation : IDisposable
{
~DisposeImplementation()
{
Console.WriteLine("~ in DisposeImplementation instance called");
}
public void Dispose()
{
Console.WriteLine("Dispose in DisposeImplementation instance called");
}
}
}
Run Code Online (Sandbox Code Playgroud)
即使我在Test();调用之后放置了一个等待循环,Dispose也永远不会被调用.所以这很糟糕.我想编写一个简单易用的类,以确保清理所有可能的资源.我不想把这个责任交给我班级的用户.
可能的解决方案:使用using或调用自己处理(基本相同).我可以强制用户使用吗?或者我可以强制调用处理吗?
呼叫GC.Collect();后Test();也不起作用.
把di以null不调用任何处置.解构器可以工作,因此对象在退出时会被解构Test()
好的,现在很清楚!
谢谢大家的答案!我会在评论中添加警告!
我有以下代码:
class Foo
{
public Foo()
{
Bar bar;
if (null == bar)
{
}
}
}
class Bar { }
Run Code Online (Sandbox Code Playgroud)
代码专家已经看到这会给出错误.在if语句之前可能没有初始化Bar.
所以现在我想知道:酒吧的价值是什么,不应该是空的吗?它们不是设置为空吗?(空指针?)
我正在尝试阅读我编译的C#代码.
这是我的代码:
using(OleDbCommand insertCommand = new OleDbCommand("...", connection))
{
// do super stuff
}
Run Code Online (Sandbox Code Playgroud)
但!
我们都知道使用被转换为:
{
OleDbCommand insertCommand = new OleDbCommand("...", connection)
try
{
//do super stuff
}
finally
{
if(insertCommand != null)
((IDisposable)insertCommand).Dispose();
}
}
Run Code Online (Sandbox Code Playgroud)
(因为OleDbCommand是一个引用类型).
但是当我反编译我的程序集(用.NET 2.0编译)时,我在Resharper中得到了这个:
try
{
insertCommand = new OleDbCommand("", connection);
Label_0017:
try
{
//do super stuff
}
finally
{
Label_0111:
if ((insertCommand == null) != null)
{
goto Label_0122;
}
insertCommand.Dispose();
Label_0122:;
}
Run Code Online (Sandbox Code Playgroud)
我在说这句话:if ((insertCommand == null) != null).
假设insertCommand为null.然后第一部分返回true.(true …
我似乎遇到了一个问题,我们正在使用的应用程序使用Ninject内核,并包含一个特定的配置,该配置是使用WCF调用的内容(username,token ...)收集的.
基于特定配置,用户被分配了应用程序的权限,这通过Ninject内核中的特定实例进行屏蔽.
我们不能只重新组合Ninject内核,我们想要做的是运行几个Nunit测试,但是在一个单独的应用程序域中运行它们(每次使用不同的设置重新创建内核).
我只找到了在不同应用领域运行整个测试项目的方法,但没有测试每个测试.
有办法解决这个问题吗?
不幸的是,Ninject配置不在我们这边,我们必须以这种方式"生活".
我有一个主要的wpf窗口:
<Window x:Class="NorthwindInterface.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:ViewModels="clr-namespace:NorthwindInterface.ViewModels" Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<ViewModels:MainViewModel />
</Window.DataContext>
<ListView ItemsSource="{Binding Path=Customers}">
</ListView>
</Window>
Run Code Online (Sandbox Code Playgroud)
而MainViewModel是这样的:
class MainViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged = delegate { };
public MainViewModel()
{
Console.WriteLine("test");
using (NorthwindEntities northwindEntities = new NorthwindEntities())
{
this.Customers = (from c in northwindEntities.Customers
select c).ToList();
}
}
public List<Customer> Customers { get;private set; }
Run Code Online (Sandbox Code Playgroud)
现在问题是在designermode中我看不到我的MainViewModel,它突出显示它无法创建MainViewModel的实例.它正在连接到数据库.这就是为什么(当我评论代码时问题得到解决).
但我不希望这样.关于此的最佳实践的任何解决方案?
为什么在使用MVVM时这会起作用:
/// <summary>
/// Initializes a new instance of the <see cref="MainViewModel"/> class.
/// </summary>
public MainViewModel()
{ …Run Code Online (Sandbox Code Playgroud) 考虑以下代码:
scores |> Map.fold (fun state key value ->
state + (findCoefficient conversion.Coefficients key) * value)
0m
Run Code Online (Sandbox Code Playgroud)
findCoefficient返回一个小数,scores是一个Map<string, decimal>
现在当我在Visual Studio中编写这段代码时,F#Power Tools给了我这个lint建议/警告:
Lint:如果在函数调用链中没有部分应用可变参数,那么函数调用和lambda可以用composition替换.例如
fun -> x |> isValid |> not可以替换为isValid >> not
在这种情况下我该怎么做?
我正在尝试构建一个验证某个类型实例的属性.
为了做到这一点,我必须将其ObjectInstance转换为该类型.
我需要在该类型的成员上设置属性.
所以我们需要求助于and循环定义的关键字.
但是在下面的例子中我得到了错误
自定义属性必须调用对象构造函数
在下面标出的行上.
namespace Test
open System
open System.ComponentModel.DataAnnotations
[<AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)>]
type MyAttribute() =
class
inherit ValidationAttribute ()
override this.IsValid (value: Object, validationContext: ValidationContext) =
match validationContext.ObjectInstance with
| :? MyClass as item ->
// TODO more validation
ValidationResult.Success
| _ ->
new ValidationResult("No no no")
end
and MyClass(someValue) =
[<Required>]
[<Range(1, 7)>]
//vvvvvvvvvvvvvvv
[<MyAttribute>]
//^^^^^^^^^^^^^^^
member this.SomeValue : int = someValue
Run Code Online (Sandbox Code Playgroud)
我试着手动调用构造函数,例如:
[<MyAttribute()>]
// or
[<new …Run Code Online (Sandbox Code Playgroud) 我今天偶然发现了这个奇怪的事情:
http://www.yoda.arachsys.com/csharp/teasers.html
问题#5.
代码:
using System;
class Test
{
enum Foo
{
Bar,
Baz
};
const int One = 1;
const int Une = 1;
static void Main()
{
Foo f = One - Une;
Console.WriteLine(f);
}
}
Run Code Online (Sandbox Code Playgroud)
现在根据问题#5的http://www.yoda.arachsys.com/csharp/teasers-answers.html上的答案
...这是一个已知的错误,因为过早地进行了一些优化,收集0的常量并认为任何已知的0常量都应该可以转换为任何枚举的0值.它现在和我们在一起,并且不太可能被修复,因为它可能会破坏一些在技术上非法但工作得非常好的代码.当然,规范可能会改变.
但为什么?
One&Une都是const.即它们可以在编译时计算,因此它就变成了
Foo f = 0.既然0是任何一个有效的值enum,为什么不编译呢?
c# ×5
.net ×4
f# ×2
wpf ×2
asp.net-mvc ×1
binding ×1
c#-2.0 ×1
data-binding ×1
dispose ×1
enums ×1
idisposable ×1
lint ×1
mvvm ×1
ninject ×1
nunit ×1
razor-2 ×1
self ×1
testing ×1
unit-testing ×1
variables ×1