在 C# 3.0 中,我们使用“var” C# 2.0 中的替代方案是什么?
我正在使用托管扩展框架(MEF)学习 .Net 中的可插入架构。 我在网上看到了示例代码,但是当我尝试实现它时,我陷入了困境。
代码使用:
var catalog = new AttributedAssemblyPartCatalog(Assembly.GetExecutingAssembly());
var container = new CompositionContainer(catalog.CreateResolver());
This var
is available on C# 3.0,而我正在 C# 2.0 中编码。
上述两种说法的替代方案是什么? 如何使用 VS 2005 让它们在 c# 2.0 中工作?
我尝试了这个 bt 它现在说
错误 1 无法找到类型或命名空间名称“AttributedAssemblyPartCatalog”(您是否缺少 using 指令或程序集引用?) C:\Documents and Settings\test\ Desktop\MEFDemo\MEFDemo\Program.cs 31 13 MEFDemo
其中我添加了对 SystemComponentModel.Composition 的引用
I am learning plug able architecture in .Net using Managed Extensibility Framework (MEF.)
I saw sample code on the net, but when I tried to implement it I got stuck at one point.
The code was using:
var catalog = new AttributedAssemblyPartCatalog(Assembly.GetExecutingAssembly());
var container = new CompositionContainer(catalog.CreateResolver());
This var
is available on C# 3.0 where as I am coding in C# 2.0.
What is the alternative of above two statements? How can I make them work in c# 2.0 using VS 2005?
i tried this bt its saying now
Error 1 The type or namespace name 'AttributedAssemblyPartCatalog' could not be found (are you missing a using directive or an assembly reference?) C:\Documents and Settings\test\Desktop\MEFDemo\MEFDemo\Program.cs 31 13 MEFDemo
where as i have added referance to SystemComponentModel.Composition
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
基本上,
var
强制编译器根据变量的“初始化程序”来确定(推断)变量的编译时类型 - 实际上是 < 右侧的表达式代码>=符号。 这里的类型很明显:并确保添加
using System.ComponentModel.Composition;
语句。 另外,请注意,AttributedAssemblyPartCatalog
已重命名为AssemblyCatalog
。Basically,
var
forces the compiler to determine (infer) the compile-time type of a variable based on it's "initializer" -- effectively, an expression to the right from=
sign. Here the types are obvious:And make sure you add
using System.ComponentModel.Composition;
statement. Plus, be advised thatAttributedAssemblyPartCatalog
was renamed toAssemblyCatalog
.这就是C# 3.0中类型推断的使用。
在 c# 3.0 中使用关键字时,
编译器会推断类型。 请参阅 scott Guthries 解释
在 c# 2.0 中,您必须声明与 c# 1.1 相同的变量类型,
例如
让你上面的代码示例
HTH
This is the use of type inference in C# 3.0.
When using the keyword
in c# 3.0 the compiler infers the type. See scott guthries explanation
In c# 2.0 you have to declare the type of the variable the same as c# 1.1
e.g.
Making you above code example
HTH
C# 中的变量仍然是强类型的。
var
是隐式类型 - 请参阅 MSDN。在大多数情况下,这仅意味着您必须减少输入,但在某些情况下这是必要的 - 在我链接到的页面上的第二个示例中:
Variables in C# are still strongly typed.
var
is implicit typing - see the MSDN.In most cases it just means you have to type less, but there are cases where it's necessary - in the second example on the page I've linked to:
var 是一个 C# 3.0 关键字,除了从初始化值推断强类型之外什么也不做。
如果没有 var,您可以手动执行编译器在幕后执行的操作; 您在声明中指定变量的类型。
因此;
var is a C# 3.0 keyword and does nothing other than inferring the strong type from the initialization value.
In the absence of var, you manually do what the compiler is doing behind the scenes; you specify the type of the variable in the declaration.
Hence;
MEF 使用 LINQ,因此需要 .NET 3.5。 尝试在 .NET 2.0 上使用它不会有任何运气。
MEF uses LINQ, so it requires .NET 3.5. You won't have any luck trying to use it on .NET 2.0.