有人可以澄清PipeTransmissionMode.Message.NET中的含义吗?
.NET如何区分通过管道传递的一条消息?
BinaryFormatter,然后将其作为消息传递给管道吗?PipeTransmissionMode.Message模式时允许的字符串消息?所以,假设我在C#中有以下表达式:
Expression<Func<string>> expr = () => foo.Bar;
Run Code Online (Sandbox Code Playgroud)
如何提取对foo的引用?
注意:我可能在标题中选择了错误的单词; 也许我真的在谈论多项式增长.请参阅此问题末尾的基准测试结果.
让我们从这三个表示不可变堆栈的递归通用接口†开始:
interface IStack<T>
{
INonEmptyStack<T, IStack<T>> Push(T x);
}
interface IEmptyStack<T> : IStack<T>
{
new INonEmptyStack<T, IEmptyStack<T>> Push(T x);
}
interface INonEmptyStack<T, out TStackBeneath> : IStack<T>
where TStackBeneath : IStack<T>
{
T Top { get; }
TStackBeneath Pop();
new INonEmptyStack<T, INonEmptyStack<T, TStackBeneath>> Push(T x);
}
Run Code Online (Sandbox Code Playgroud)
我创建了简单的实现EmptyStack<T>,NonEmptyStack<T,TStackBeneath>.
更新#1:请参阅下面的代码.
我注意到有关其运行时性能的以下内容:
EmptyStack<int>第一次将1,000件物品推到a上需要7秒多.EmptyStack<int>几乎没有时间.更新#2:
我终于进行了更精确的测量.请参阅下面的基准代码和结果.
我在这些测试中只发现.NET 3.5似乎不允许递归深度≥100的泛型类型..NET 4似乎没有这个限制.
前两个事实让我怀疑性能缓慢不是由于我的实现,而是由于类型系统:.NET必须实例化1,000个不同的封闭泛型类型,即:
EmptyStack<int>NonEmptyStack<int, EmptyStack<int>>NonEmptyStack<int, NonEmptyStack<int, EmptyStack<int>>> …我创建了一个简单的类来对我的一些方法进行基准测试 但它准确吗?我对基准测试,计时等等都不熟悉,所以我想在这里可以请一些反馈.此外,如果它是好的,也许其他人也可以使用它:)
public static class Benchmark
{
public static IEnumerable<long> This(Action subject)
{
var watch = new Stopwatch();
while (true)
{
watch.Reset();
watch.Start();
subject();
watch.Stop();
yield return watch.ElapsedTicks;
}
}
}
Run Code Online (Sandbox Code Playgroud)
你可以像这样使用它:
var avg = Benchmark.This(() => SomeMethod()).Take(500).Average();
Run Code Online (Sandbox Code Playgroud)
任何反馈?它看起来是非常稳定和准确的,还是我错过了什么?
使用专用应用程序池而不是将Web应用程序保存在一个默认应用程序池中的优缺点是什么?
如何在类图中指明泛型类或接口?
我真的在寻找两件事:
谢谢!
最近发生了以下(样本)枚举......
enum Color
{
Red,
Green,
Yellow,
Blue
}
Run Code Online (Sandbox Code Playgroud)
......可以用看似更加类型安全的类替换:
class Color
{
private Color() { }
public static readonly Color Red = new Color();
public static readonly Color Green = new Color();
public static readonly Color Yellow = new Color();
public static readonly Color Blue = new Color();
}
Run Code Online (Sandbox Code Playgroud)
对于"类型安全",我的意思是如果Color是枚举,则以下语句将起作用,但如果Color是上述类则不行:
var nonsenseColor = (Color)17; // works if Color is an enum
Run Code Online (Sandbox Code Playgroud)
1)这种模式是否有广泛接受的名称(用类型安全类替换枚举)?
2)在哪种情况下应该使用枚举,什么时候更合适?
我想使用代码合同为此通用接口指定合同:
interface IRandomWriteAccessible<T>
{
T this[uint index] { set; }
uint Length { get; }
}
Run Code Online (Sandbox Code Playgroud)
文档ContractClass说明在为接口指定合同时使用该属性.但是,编译器会抱怨这个:
[ContractClass(typeof(IRandomWriteAccessibleContract<T>))]
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ <-- compiler error
interface IRandomWriteAccessible<T> { … }
[ContractClassFor(typeof(IRandomWriteAccessible<T>))]
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ <-- compiler error
sealed class IRandomWriteAccessibleContract<T> : IRandomWriteAccessible<T> { … }
Run Code Online (Sandbox Code Playgroud)
似乎类型参数不能与属性一起使用.
如何做我写我的通用接口一份合同吗?或者代码合同无法实现这一点吗?
环境:.NET Framework 2.0,VS 2008.
我试图创建将通过某些鼠标事件(一定的.NET控件(标签,面板)的子类MouseDown,MouseMove,MouseUp)到它的父控制(或可替换地到顶层形式).我可以通过在标准控件的实例中为这些事件创建处理程序来实现此目的,例如:
public class TheForm : Form
{
private Label theLabel;
private void InitializeComponent()
{
theLabel = new Label();
theLabel.MouseDown += new MouseEventHandler(theLabel_MouseDown);
}
private void theLabel_MouseDown(object sender, MouseEventArgs e)
{
int xTrans = e.X + this.Location.X;
int yTrans = e.Y + this.Location.Y;
MouseEventArgs eTrans = new MouseEventArgs(e.Button, e.Clicks, xTrans, yTrans, e.Delta);
this.OnMouseDown(eTrans);
}
}
Run Code Online (Sandbox Code Playgroud)
我无法将事件处理程序移动到控件的子类中,因为引发父控件中的事件的方法受到保护,并且我没有父控件的限定符:
无法
System.Windows.Forms.Control.OnMouseDown(System.Windows.Forms.MouseEventArgs)通过类型的限定符访问受保护的成员System.Windows.Forms.Control; 限定符必须是类型TheProject.NoCaptureLabel(或从中派生).
我正在研究覆盖WndProc我的子类中的控制方法,但希望有人可以给我一个更清洁的解决方案.
c# ×5
.net ×3
generics ×2
asp.net ×1
benchmarking ×1
clr ×1
enums ×1
feedback ×1
iis ×1
interface ×1
java ×1
lambda ×1
mouseevent ×1
named-pipes ×1
polymorphism ×1
recursion ×1
syntax ×1
types ×1
uml ×1
winforms ×1