为什么我不能用/施放/一个Action到的ThreadStart? [英] Why can't I use/cast an Action for/to a ThreadStart?

查看:389
本文介绍了为什么我不能用/施放/一个Action到的ThreadStart?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

两者都是代表和具有相同的签名,但我不能用行动为的ThreadStart。



为什么?



 动作doIt方法; 
的doIt =()=>的MyMethod(测试);
线程t;

T =新的Thread(doIt方法);
t.Start();



不过这似乎工作:

 螺纹吨; 

T =新的Thread(()=>的MyMethod(测试));
t.Start();


解决方案

正如其他人所指出的,问题是,委托类型不是结构。也就是说,根据其结构他们没有对等。



现在,这无疑是对某些类型的一件好事。如果你有



 结构MyRectangle {INT X;诠释Ÿ; INT宽度; INT高度; ...} 

 结构YourRectangle {INT X1; INT Y1; INT×2; INT Y2; ...} 



显然这将是一个错误,让被分配到的变量MyRectangle的实例YourRectangle,只是因为它们都包括四个整数。该整数的的语义的是不同的,因此,类型是不等价的。



同样是,在理论上,代表真实的。你可以有



 委托INT纯(字符串x); 
代表INT Func键(字符串x);



,其中纯功能是一个没有副作用,并给予相同的输入输出相同。由于每个纯在逻辑上是一个函数功能,但每一个函数功能不一定是纯粹的,不应该有它们之间的结构类型。



在课程类型系统确实实践不支持像纯函数很好的概念。而在实践中,绝大多数尝试委托类型之间的转换是非常安全:从 Func键<转换,INT,BOOL> 谓词< INT> 等。



所以,两件事,一个向后看,一个向前看。倒退:如果我们不得不做一遍的话,我想代表可能会在CLI进行结构类型。你总是不知道哪些功能会当你设计一个全新的框架,和非结构性的委托类型迄今果然如预期或许是不一样有用是有用的。前锋:我希望看到在CLR的未来版本,使更多的结构类型更多的功能。在C#4中的无软的功能,例如,为约使得两种类型的在语义上和结构上是相同的,但在不同的组件所定义,逻辑统一结构


Both are delegates and have the same signature, but I can not use Action as ThreadStart.

Why?

Action doIt;
doIt = () => MyMethod("test");
Thread t;

t = new Thread(doIt);
t.Start();

but this seems to work:

Thread t;

t = new Thread(() => MyMethod("test"));
t.Start();

解决方案

As others have noted, the problem is that delegate types are not "structural". That is, they do not have equivalence based on their "structure".

Now, this is arguably a good thing for some types. If you have

struct MyRectangle { int x; int y; int width; int height; ... }

and

struct YourRectangle { int x1; int y1; int x2; int y2; ... } 

obviously it would be a mistake to allow instances of MyRectangle to be assigned to variables of YourRectangle, just because they both consisted of four ints. The semantics of the ints are different and therefore the types are not equivalent.

The same is, in theory, true of delegates. You could have

delegate int Pure(string x);
delegate int Func(string x);

where a "pure" function is one with no side effects and the same output given the same input. Since every Pure is logically a Func, but every Func is not necessarily a Pure, there shouldn't be structural typing between them.

In practice of course the type system does not support notions like "pure function" very well. And in practice, the vast majority of attempts to convert between delegate types are perfectly safe: converting from a Func<int, bool> to a Predicate<int> and so on.

So, two things, one looking backwards and one looking forwards. Backwards: if we had to do it all over again, I think delegates would probably be structurally typed in the CLI. You don't always know what features are going to be useful when you design a brand new framework, and non-structural delegate types have thus far turned out to be not as useful as perhaps anticipated. Forwards: I expect to see more features in future versions of the CLR that enable more structural typing. The "no pia" feature in C# 4, for example, is about making two types that are semantically and structurally the same, but defined in different assemblies, logically unify structurally.

这篇关于为什么我不能用/施放/一个Action到的ThreadStart?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆