我显示错误:
'Timer'是'System.Windows.Forms.Timer'和'System.Threading.Timer'之间的模糊引用当我添加时钟代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Threading;
using System.Security.Cryptography;
namespace SocketClient
{
public partial class SocketClient : Form
{
System.Net.Sockets.TcpClient clientSocket = new System.Net.Sockets.TcpClient();
NetworkStream serverStream = default(NetworkStream);
string readData = null;
public SocketClient()
{
InitializeComponent();
Timer timer = new Timer();
timer.Tick += new EventHandler(TimerOnTick);
timer.Interval = 1000;
timer.Start();
}
private void TimerOnTick(object sender, EventArgs ea)
{
Invalidate();
}
protected override void OnPaint(PaintEventArgs pea) …Run Code Online (Sandbox Code Playgroud) 我刚看完YouTube上的Google清洁代码视频(请参阅链接,第一篇文章),关于if从代码中删除语句并使用多态.
观看视频后,我看了一下我在观看视频之前编写的一些代码,并注意到我可以使用这种方法的地方,主要是多次实现相同类型逻辑的地方.举个例子:
我有一些像这样的代码.
public int Number
{
get
{
string returnValue;
if (this.internalTableNumber == null)
returnValue = this.RunTableInfoCommand(internalTableName,
TableInfoEnum.TAB_INFO_NUM);
else
returnValue = this.RunTableInfoCommand(internalTableNumber.Value,
TableInfoEnum.TAB_INFO_NUM);
return Convert.ToInt32(returnValue);
}
}
Run Code Online (Sandbox Code Playgroud)
RunTableInfoCommand的作用并不重要,但主要的是我有许多具有完全相同的属性的属性,if唯一改变的是TableInfoEnum.
我想知道是否有人可以帮助我重构这一点,以便它仍然做同样的事情但没有任何if陈述?
我正在编写一个管理脚本,我需要计算磁盘上文件的大小.
这些文件位于压缩的NTFS卷上.
我无法使用FileInfo.Length,因为这是文件大小而不是磁盘上的大小.例如,如果我有一个100MB的文件,但由于NTFS压缩它只使用25MB,我需要我的脚本返回25MB.
有没有办法在Powershell中做到这一点?
(我知道GetCompressedFileSize()Win32的调用,但我希望这已经在某种程度上受到了影响.)
是否有可能List根据对象的属性获取a的不同元素List?
喜欢: Distinct(x => x.id)
对我来说没有用的是:.Select(x => x.id).Distinct()因为那时我会回来List<int>而不是List<MyClass>
在任何时候,我都可以接收一个需要长时间运行才能满足的方法调用.
这些方法有以下几种.因为它们共享一个资源,所以它们不能同时运行是很重要的 - 每个都应该按顺序运行.
通常,我只是按顺序进行调用:
var result1 = await Foo1Async();
var result2 = await Foo2Async();
Run Code Online (Sandbox Code Playgroud)
但是,在这种情况下,对我的方法调用是异步的:
void Bar1()
{
var result = await Foo1Async();
// ...
}
void Bar2()
{
var result = await Foo2Async();
// ...
}
Run Code Online (Sandbox Code Playgroud)
我想我需要使用Task.ContinueWith,并想出了这个:
readonly object syncLock = new Object();
private Task currentOperationTask = TaskUtilities.CompletedTask;
public Task<TResult> EnqueueOperation<TResult>(Func<Task<TResult>> continuationFunction)
{
lock (this.syncLock)
{
var operation = this.currentOperationTask.ContinueWith(predecessor => continuationFunction()).Unwrap();
this.currentOperationTask = operation;
return operation;
}
}
Run Code Online (Sandbox Code Playgroud)
我这样使用它:
void Bar1()
{
var result = …Run Code Online (Sandbox Code Playgroud) 我需要一些属性类中的Guid属性,如下所示:
public class SomeAttribute : Attribute {
private Guid foreignIdentificator;
public Guid ForeignIdentificator {
get { return this.foreignIdentificator; }
set { this.foreignIdentificator = value; }
}
}
Run Code Online (Sandbox Code Playgroud)
但是在属性定义中,我只能使用原始类型,它们是常量(我理解为什么,这让我有意义).解决方法可以将"ForeignIdentificator"定义为字符串,并在运行时创建Guid:
public class SomeAttribute : Attribute {
private string foreignIdentificator;
public string ForeignIdentificator {
get { return this.foreignIdentificator; }
set { this.foreignIdentificator = value; }
}
public Guid ForeignIdentificatorGuid {
get { return new Guid( ForeignIdentificator ); }
}
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,我没有检查类型安全性."ForeignIdentificator"属性可以包含任何字符串值,并且在创建过程中Guid将在运行时抛出异常,而不是在编译时抛出异常.
我知道编译器检查"System.Runtime.InteropServices.GuidAttribute"的字符串值是否为"Guid兼容性".这个检查正是我需要的,但我不知道这个检查是否在编译器中硬编码或者我可以明确定义(以及如何).
你知道某种方式,如何确保"Guid compatibilitybilty"检查属性?或者另一种方式,如何在属性中达到类型安全的Guid定义?谢谢.
在我修复了这个警告之后,我怎么能把它变成一个错误,所以它不会再次滑入?
msbuild /p:TreatWarningsAsErrors 不起作用
我试图让Visual Studio 2015(14.0)在使用C#重构实现接口时使用自动属性.
就是我想要这个;
public object SomeProperty { get; set; }
Run Code Online (Sandbox Code Playgroud)
与此相反;
public object SomeProperty
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}
Run Code Online (Sandbox Code Playgroud)
我已经在Visual Studio的过去版本中通过编辑代码片段文件(此处的说明)完成了此操作,但我无法使用Visual Studio 2015来实现此功能.
c# ×4
.net ×3
async-await ×1
attributes ×1
c++ ×1
distinct ×1
grails ×1
guid ×1
ide ×1
linq ×1
msbuild ×1
polymorphism ×1
powershell ×1
refactoring ×1