Question

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

Was it helpful?

Solution

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

OTHER TIPS

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.

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

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

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.

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());

MEF uses LINQ, so it requires .NET 3.5. You won't have any luck trying to use it on .NET 2.0.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top