.NET 4引入了协方差.我想这很有用.毕竟,MS经历了将其添加到C#语言的所有麻烦.但是,为什么协方差比良好的旧多态更有用呢?
我写这个例子来理解为什么我应该实现Covariance,但我仍然没有得到它.请赐教.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Sample
{
class Demo
{
public delegate void ContraAction<in T>(T a);
public interface IContainer<out T>
{
T GetItem();
void Do(ContraAction<T> action);
}
public class Container<T> : IContainer<T>
{
private T item;
public Container(T item)
{
this.item = item;
}
public T GetItem()
{
return item;
}
public void Do(ContraAction<T> action)
{
action(item);
}
}
public class Shape
{
public void Draw()
{
Console.WriteLine("Shape Drawn");
}
}
public class …Run Code Online (Sandbox Code Playgroud) 我知道我可以通过添加自己的Equals实现来避免拳击.
public struct TwoDoubles
{
public double m_Re;
public double m_Im;
public TwoDoubles(double one, double two)
{
m_Re = one;
m_Im = two;
}
public override bool Equals(object ob)
{
return Equals((TwoDoubles)ob);
}
public bool Equals(TwoDoubles ob)
{
TwoDoubles c = ob;
return m_Re == c.m_Re && m_Im == c.m_Im;
}
}
Run Code Online (Sandbox Code Playgroud)
我不能把这称为覆盖,就像重载一样.通过运行时的魔力,它可以Equals()根据调用者的类型正确调用正确的实现.
为什么我不能覆盖并更改参数类型,TwoDoubles并根据需要通过运行时的功能进行装箱?是因为C#不支持参数逆转(如果这就是为什么它不支持的原因......似乎只是一小步object o = new TwoDoubles())?
更新
澄清: object是结构的继承层次结构的一部分.为什么我们不能指定更多派生类型作为参数来覆盖较少派生类型的实现?这将允许我们写:
public override bool Equals(TwoDoubles ob)
{
TwoDoubles c = ob;
return m_Re …Run Code Online (Sandbox Code Playgroud) 我有一组$.get()请求需要重构以包含失败回调.请求是形式的
$.get(url,
{},
function(data) {//success, do work
//work
},'json');//or 'html'
Run Code Online (Sandbox Code Playgroud)
根据jQuery API,我只需添加一个jqHXR对象.所以在我的情况下,我相信我应该这样做
var jqxhr = $.get(url,
{},
function(data) {//success, do work
//work
},'json').error(function() { alert("error"); });//or 'html'
Run Code Online (Sandbox Code Playgroud)
我不明白示例中第二次成功回调的原因.我想它可以用于建立一个回调链.我希望错误执行错误,并成功执行成功.那么,这是正确的吗?
所以我可以做到这一点
string selectedOption = ViewBag.SelectedOption;
<select id="SelectedYear" name="SelectedYear">
<option value="2010">2010</option>//if(selectedOption == 2010)...meh...
<option value="2011">2011</option>
<option value="2012">2012</option>
<option value="2013">2013</option>
</select>
Run Code Online (Sandbox Code Playgroud)
而且我知道我可以存储SelectedOption在a中div并使用jQuery以简洁的方式设置所选的选项$(document).ready.
是否有一种简洁的方法来直接完成MVC3/razor的任务?
一个博客州
标签虽然有一个很大的警告 - 标签本身不受版本控制,这意味着无法跟踪或审核标签上的活动历史记录.另外,如果文件被删除,标签不会保留版本的副本,因此如果文件被删除,任何依赖于该文件版本的标签基本上都是软件.
这不是我读过有关TFS标签的类似信息的唯一地方.缺乏历史已经足够清楚了.第二部分"标签不保留版本的副本......"尚不清楚.事实上,我创建了一个测试项目>标记它>删除了一个文件>执行了一个Get by Label并且该文件又回来了.那指的是什么?截至最近,TFS中的标签功能是否已更改?
我意识到文件删除实际上并没有删除历史记录,原因是什么?换句话说,如果我跑
tf销毁"$/MyTeamProject/Project/FileName.cs"
这是删除文件的意义吗?如果是这样,这似乎是一个特殊的情况甚至考虑.我的意思是,它是故意不可恢复的历史删除.在这种情况下,变更集对标签不会有任何改进.
我已经安装了Microsoft Visual Studio 2008 SP1和VS 2010 SP1.我正在使用Windows 7.
然后,我注意到在控制面板中的"已安装程序"中,只有以下是已安装的.NET框架:
出于某些原因,当我为Visual Studio 2008安装PowerCommands时,它说我需要安装.NET Framework 3.5.
当我尝试使用dotnetfx35.exe时,它只是解压缩安装程序,但不会继续安装过程.
这只是因为我使用的是Windows 7吗?或者是否有正确的方法来设置.NET Framework 3.5?
谢谢!
在他的书中,Jon Skeet提到了对隐式打字的7个限制.我需要澄清最后两点:
答:您希望变量具有的类型是初始化表达式的编译时类型.
B.初始化表达式不涉及声明的变量.
这本书按照发布的顺序(C#3之前的C#2)涵盖材料.此时尚未引入C#4,因此我假设A没有引用dynamic.那么,编译时类型何时与初始化表达式的执行时类型不同?
对于B,何时初始化表达式涉及声明的变量?
此代码根据其内容自动调整RichTextBox的大小.我遇到了问题,尤其是桌子. \t可能会被忽略.我尝试了托管解决方案,现在我正在尝试平台调用.电流输出:

[DllImport("gdi32.dll")]
static extern bool GetTextExtentPoint32(IntPtr hdc, string lpString, int cbString, out SIZE lpSize);
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr GetDC(IntPtr hWnd);
[StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct SIZE
{
public int cx;
public int cy;
public SIZE(int cx, int cy)
{
this.cx = cx;
this.cy = cy;
}
}
public static void Main()
{
Form form = new Form();
RichTextBox rtfBox = new RichTextBox();
rtfBox.Rtf = @"{\rtf1\ansi\deff0{\fonttbl{\f0\fnil Arial;}}\viewkind4\uc1\trowd\trgaph100\cellx1000\cellx2000\pard\intbl\lang1033\f0\fs20 hi\cell bye\cell\row\intbl one\cell two\cell\row\pard\par}";
rtfBox.ScrollBars = RichTextBoxScrollBars.None;
string sInput = "hi\t …Run Code Online (Sandbox Code Playgroud) 我有:
private readonly ReportingService2010 _rs = new ReportingService2010();
错误:
The type or namespace name 'ReportingService2010' could not be found (are you missing a using directive or an assembly reference?)
Run Code Online (Sandbox Code Playgroud)
我设置了对SSRS服务的引用.该引用不允许我按预期访问ReportingService2010.最接近的是:
MySsrsServiceNamespace.ReportingService2010SoapClient
我怎么应该ReportingService2010上课? MSDN模糊地列出了这个类.
请注意我尝试过使用ReportingService2010SoapClient.此类与ReportingService2010的文档不匹配.例如,ListChildren()只接受4个参数,并且Url属性不存在.
我已经使用nuget将NLog添加到项目中并添加了NLog.config.我正在运行调试器并得到一个NullReferenceException由于事实LogManager.Configuration是null:
LogManager.Configuration.AddTarget("sentinel", sentinalTarget);
这行代码在静态构造函数中运行.
LogManager.ThrowExceptions是假的,所以我怀疑配置有问题NLog.config内容:
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
autoReload="true"
throwExceptions="false">
<variable name="appName" value="YourAppName" />
<targets async="true">
<target xsi:type="File"
name="default"
layout="${longdate} - ${level:uppercase=true}: ${message}${onexception:${newline}EXCEPTION\: ${exception:format=ToString}}"
fileName="${specialfolder:ApplicationData}\${appName}\Debug.log"
keepFileOpen="false"
archiveFileName="${specialfolder:ApplicationData}\${appName}\Debug_${shortdate}.{##}.log"
archiveNumbering="Sequence"
archiveEvery="Day"
maxArchiveFiles="30"
/>
<target xsi:type="EventLog"
name="eventlog"
source="${appName}"
layout="${message}${newline}${exception:format=ToString}"/>
<target xsi:type="NLogViewer"
name="viewer"
address="udp://127.0.0.1:9999"/>
<target xsi:type="OutputDebugString" name="DbWin" layout="Log4JXmlEventLayout">
<layout xsi:type="Log4JXmlEventLayout" />
</target>
</targets>
<rules>
<logger name="*" writeTo="default" minlevel="Info" />
<logger name="*" writeTo="eventlog" minlevel="Error" />
<logger name="*" …Run Code Online (Sandbox Code Playgroud) c# ×6
.net ×2
.net-3.5 ×1
covariance ×1
javascript ×1
jquery ×1
nlog ×1
polymorphism ×1
razor ×1
tfs ×1
tfs2010 ×1
types ×1
windows-7 ×1
winforms ×1