我有以下winforms类:
class EntityEditorForm<T>: System.Windows.Forms.Form
where T: ICloneable<T> {}
class EntityCollectionEditorForm<T> : System.Windows.Forms.Form
where T: ICloneable<T> {}
Run Code Online (Sandbox Code Playgroud)
第一个表单类是一个编辑器,用于<T>在运行时根据T的类型创建控件.
第二个是一个管理器,用于收集<T>和添加,编辑和删除功能.该集合显示在listview控件中,其中使用自定义属性通过反射填充字段.
"添加"和"编辑"按钮的代码如下所示:
private void buttonEdit_Click (object sender, System.EventArgs e)
{
T entity = default(T);
entity = (T) this.listView.SelectedItems[0].Tag;
new EntityEditor<T>(entity).ShowDialog(this);
}
private void buttonEdit_Click (object sender, System.EventArgs e)
{
T entity = new T(); //This is the code which is causing issues
entity = (T) this.listView.SelectedItems[0].Tag;
new EntityEditor<T>(entity).ShowDialog(this);
}
Run Code Online (Sandbox Code Playgroud)
将default(T)在编辑的情况下工作,但我在与添加的场景麻烦.T entity = new T();看似不合法.
我一直对这个感到困惑.考虑以下循环:
int [] list = new int [] { 1, 2, 3 };
for (int i=0; i < list.Length; i++) { }
foreach (int i in list) { }
while (list.GetEnumerator().MoveNext()) { } // Yes, yes you wouldn't call GetEnumerator with the while. Actually never tried that.
Run Code Online (Sandbox Code Playgroud)
int List{get{return(new int [] {1,2,3});}}怎么办?这会扰乱循环吗?如果没有,它会在每次迭代中创建一个新实例吗?我知道为短期运行任务提供细粒度控制的任务,但我有一种情况是使用foreach循环更自然.问题是,是否有可能告诉Parallel.For期望短时间运行并使用尽可能多的线程来最大化CPU?
如果没有,那么您建议并行化的方法是什么:
bool [,] grid = new bool [1000, 1000];
for (int y=0; y<1000; y++)
for (int x=0; x<1000; x++)
// Ignore the bounds error. This is just to illustrate a very short operation.
grid[x, y] |= grid[x-1, y+1];
Run Code Online (Sandbox Code Playgroud) 我正在使用一个结构,需要隐式运算符对字符串,并遇到一个我没有想过的基本问题.
public static implicit operator Version (string value) {...}
Run Code Online (Sandbox Code Playgroud)
我可以理解只有显式运算符来强制执行转换,但如果隐式运算符已经过载,则无法想到需要它的情况.有吗?
.net c# operator-overloading implicit-conversion explicit-conversion
请考虑以下列表:
List<string> ordered = new List<string> (new string [] { "one", "two", "three", ... });
List<string> sequence1 = new List<string> (new string [] { "two", "three", "ten", ... });
Run Code Online (Sandbox Code Playgroud)
该列表[ordered]是包含所有可能值的超集.
该列表[sequence1]是列表的随机排序子集[ordered].它可能包含部分或全部超集的元素.
我目前有一个冗长的功能,[sequence1]基于此排序,[ordered]并想知道是否更简单的Linq方式来做到这一点.当前方法使用for循环迭代超集,并尝试在子集中查找值并将其移动到其相关位置.
列表中的项目数量永远不会超过几十个,并且将谨慎地调用排序操作,因此效率不是关键.
我正在尝试ReadOnlyDictionary<TKey, TValue>为.NET 4.0 创建自定义.方法是保留私有Dictionary<TKey, TValue>对象和标志,以确定是否允许添加/删除和项目分配.
这工作正常,但我想实现IDictionary<TKey, TValue>完整性的接口.但是,我注意到它扩展了,ICollection<KeyValuePair<TKey, TValue>>而它的属性或方法都没有出现在Dictionary<TKey, TValue>类中.这怎么可能?如果实现了接口,为什么ICollection不暴露成员?
此外,为什么Dictionary班级需要ICollection首先实施?
这是一个粗略的实现:
public sealed class ReadOnlyDictionary<TKey, TValue>:
//IDictionary<TKey, TValue>,
IEnumerable<KeyValuePair<TKey, TValue>>
{
#region Members.
public bool AllowListEdit { get; private set; }
public bool AllowItemEdit { get; private set; }
private Dictionary<TKey, TValue> Dictionary { get; set; }
#endregion Members.
#region Constructors.
public ReadOnlyDictionary (bool allowListEdit, bool allowItemEdit) { this.AllowListEdit = allowListEdit; this.AllowItemEdit = allowItemEdit; …Run Code Online (Sandbox Code Playgroud) 我有一组反映图像上已知位置的坐标.我们称之为模板图像.它有一个独特的条形码和方向标记(也在坐标数组中).
打印,扫描图像并将其反馈到我的应用程序中进行检测.在打印和扫描期间,图像可以以三种方式转换; 翻译,轮换和规模.
假设我可以在失真图像上找到方向标记,我如何使用矩阵变换来获得剩余坐标的相对位置?
我之前在SO上发布了这个问题,但是让它太复杂,无法理解我想要的东西.
编辑
namespace MatrixTest
{
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections.Generic;
public static class Program
{
public static void Main ()
{
Template template = new Template(); // Original template image.
Document document = new Document(); // Printed and scanned distorted image.
template.CreateTemplateImage();
// The template image is printed and scanned. This method generates an example scan or this question.
document.CreateDistortedImageFromTemplateImage();
// Stuck here.
document.Transform();
// Draw transformed points on the image …Run Code Online (Sandbox Code Playgroud) 我正在将一个复杂的算法从32位移植到64位,在我的Core i5机器上运行大约5个小时,运行Windows 7 64位的8GB RAM.
该应用程序面向.NET 4,并使用任务并行库约60%的循环并使用BigInteger类.
我正在处理两个数字范围:
对这些数字执行的操作包括加法,减法乘法,除法,对数和幂.
一旦移植到64位,我将能够对代码进行分析和计时以查看性能提升,但我想知道我是否可以通过计算来估计它.
如果是这样,请推荐一些解释相同的文章.
我正在使用 VS 2010 (C#) T4 模板来生成代码。
我需要迭代项目中的所有类型,列出实体 poco 类并生成包装器。问题是,项目名称空间未被识别。
这是解决方案的结构:
namespace MySolution.Entities
{
public class Employee { ... }
public class Department { ... }
}
// Seperate project referenceing MySolution.Entities.
namespace MySolution.Database
{
public partial class Context { ... }
// Should generate Context.cs as a partial class with after iterating Syste.Types available in MySolution.Entities.
Context.tt
}
Run Code Online (Sandbox Code Playgroud)
这是文本模板:
<#@ template language="C#" #>
<#@ Output Extension=".cs" #>
namespace MySolution.Database
{
public partial class Context:
System.Data.Entity.DbContext
{
<#
System.Type [] types …Run Code Online (Sandbox Code Playgroud) 好的,这是一个非常基本的问题,但我刚刚碰到它.考虑以下课程:
public class WindowComparer: IComparer, Generic.IComparer<Window> { }
Run Code Online (Sandbox Code Playgroud)
场景1:我通常会声明它的方式.
public int Compare (object x, object y)
{
return (this.Compare((Window) x, (Window) y));
}
public int Compare (Window x, Window y) {...}
Run Code Online (Sandbox Code Playgroud)
场景2:MSDN文档通常声明它的方式.
int Collections.IComparer.Compare (object x, object y)
{
// [this.Compare] is not recognized. How to access this?
return (this.Compare((Window) x, (Window) y));
}
int Generic.IComparer<Window>.Compare (Window x, Window y) {...}
Run Code Online (Sandbox Code Playgroud)
但是,在尝试方案2时,intellisense会丢失Compare方法,并且编译器会引发一个错误,指出Compare方法不是WindowComparer的成员.我错过了什么?
c# ×10
.net ×7
generics ×2
64-bit ×1
collections ×1
constraints ×1
constructor ×1
coordinates ×1
dictionary ×1
ienumerable ×1
image ×1
integrity ×1
interface ×1
linq ×1
loops ×1
matrix ×1
optimization ×1
performance ×1
readonly ×1
sorting ×1
syntax ×1
t4 ×1