tags:

views:

1815

answers:

7

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?

+1  A: 
AttributedAssemblyPartCatalog catalog = new 
    AttributedAssemblyPartCatalog(Assembly.GetExecutingAssembly());

CompositionContainer container = new 
    CompositionContainer(catalog.CreateResolver());
Binary Worrier
+12  A: 

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.

Anton Gogolev
i tried this bt its saying nowError 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 MEFDemowhere as i have added referance to SystemComponentModel.Fomposition
Mohsan
I'm not sure your apparent condescension is necessary, if the OP "obviously" doesn't understand, is there any need to point that out?
Patrick McDonald
@Patrick, @Mohsan Sorry, never meant to offend anybody.
Anton Gogolev
I removed the "apparent condescension", it's a down-vote magnet to what is otherwise a complete and comprehensive question.
Binary Worrier
A: 

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

Mohsan
AttributedAssemblyPartCatalog has been renamed to AssemblyCatalog in the current version of MEF. See: http://stackoverflow.com/questions/732495/what-happened-to-mef-type-attributedassemblypartcatalog
Nathan Ridley
Take a look at the Faq, you should re-edit your question and add more detail there, do not add an answer unless you're actually answering your own question (which is sometimes a valid thing to do), hope this helps.
Binary Worrier
A: 

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());
Nathan Ridley
+1  A: 

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.

ChrisF
A: 

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

w4ymo
Anonymous types and implicitly typed variables are separate features. Anonymous types aren't particularly useful without "var", but the sample in the question doesn't use anonymous types at all.
Jon Skeet
Post Edited. Thanks for pointing that out Jon I got my wording mixed up.
w4ymo
As an additional help, you can hover the mouse over the "var" keyword to find out what the inferred type is.
Timwi
A: 

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

Daniel Plaisted