在 C# 3.0 中,我们使用“var” C# 2.0 中的替代方案是什么?

发布于 2024-07-20 04:25:42 字数 661 浏览 7 评论 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

|煩躁 2024-07-27 04:25:42

基本上,var 强制编译器根据变量的“初始化程序”来确定(推断)变量的编译时类型 - 实际上是 < 右侧的表达式代码>=符号。 这里的类型很明显:

AttributedAssemblyPartCatalog catalog = 
    new AttributedAssemblyPartCatalog(Assembly.GetExecutingAssembly());
CompositionContainer container = 
    new CompositionContainer(catalog.CreateResolver());

并确保添加 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:

AttributedAssemblyPartCatalog catalog = 
    new AttributedAssemblyPartCatalog(Assembly.GetExecutingAssembly());
CompositionContainer container = 
    new CompositionContainer(catalog.CreateResolver());

And make sure you add using System.ComponentModel.Composition; statement. Plus, be advised that AttributedAssemblyPartCatalog was renamed to AssemblyCatalog.

始终不够 2024-07-27 04:25:42

这就是C# 3.0中类型推断的使用。

在 c# 3.0 中使用关键字时,

var

编译器会推断类型。 请参阅 scott Guthries 解释

在 c# 2.0 中,您必须声明与 c# 1.1 相同的变量类型,

例如

Type variableName = new Type();

让你上面的代码示例

AttributedAssemblyPartCatalog catalog = new AttributedAssemblyPartCatalog(Assembly.GetExecutingAssembly());

CompositionContainer container = new CompositionContainer(catalog.CreateResolver());

HTH

This is the use of type inference in C# 3.0.

When using the keyword

var

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.

Type variableName = new Type();

Making you above code example

AttributedAssemblyPartCatalog catalog = new AttributedAssemblyPartCatalog(Assembly.GetExecutingAssembly());

CompositionContainer container = new CompositionContainer(catalog.CreateResolver());

HTH

羁〃客ぐ 2024-07-27 04:25:42
AttributedAssemblyPartCatalog catalog = new 
    AttributedAssemblyPartCatalog(Assembly.GetExecutingAssembly());

CompositionContainer container = new 
    CompositionContainer(catalog.CreateResolver());
AttributedAssemblyPartCatalog catalog = new 
    AttributedAssemblyPartCatalog(Assembly.GetExecutingAssembly());

CompositionContainer container = new 
    CompositionContainer(catalog.CreateResolver());
橘亓 2024-07-27 04:25:42

C# 中的变量仍然是强类型的。 var 是隐式类型 - 请参阅 MSDN

在大多数情况下,这仅意味着您必须减少输入,但在某些情况下这是必要的 - 在我链接到的页面上的第二个示例中:

必须使用

var 因为结果
是匿名类型的集合,
并且该类型的名称不是
除编译器外均可访问
本身。

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 must be used because the result
is a collection of anonymous types,
and the name of that type is not
accessible except to the compiler
itself.

宫墨修音 2024-07-27 04:25:42

var 是一个 C# 3.0 关键字,除了从初始化值推断强类型之外什么也不做。

如果没有 var,您可以手动执行编译器在幕后执行的操作; 您在声明中指定变量的类型。

因此;

AttributedAssemblyPartCatalog catalog = new 
    AttributedAssemblyPartCatalog(Assembly.GetExecutingAssembly());

CompositionContainer container = new 
    CompositionContainer(catalog.CreateResolver());

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;

AttributedAssemblyPartCatalog catalog = new 
    AttributedAssemblyPartCatalog(Assembly.GetExecutingAssembly());

CompositionContainer container = new 
    CompositionContainer(catalog.CreateResolver());
对岸观火 2024-07-27 04:25:42

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文