2010-11-05 41 views
0

至於MEF的全部新手,我在第一次測試MEF時遇到問題。我的問題是代碼如下─無法編譯ImportMany屬性

using System; 
using GlobalInterfaces; 
using Microsoft.VisualStudio.TestTools.UnitTesting; 
using System; 
using System.Collections.Generic; 
using System.Configuration; 
using System.ComponentModel.Composition; 
using System.ComponentModel.Composition.Hosting; 

namespace GlobalInterfacesUnitTest 
{ 
    [TestClass] 
    public class GlobalInterfacesUnitTest 
    { 
     [TestMethod] 
     public void TestMethod1() 
     { 
      [ImportMany(AllowRecomposition = true)] 
      Lazy<IComponentGui, IImportComponentGuiCapabilites>[] Senders {get;set;} 
     } 
    } 
} 

,我不能讓編譯器找到「ImportMany」屬性的問題。我檢查了幾個演示的引用並複製了它們的引用,但仍然有相同的問題。我看不到我忽略了什麼。我正在使用VS2010/Net4.0。

+0

'ImportAttribute'位於System.ComponentModle.Composition的組合體中。按照Andrey的回答,並確保將該程序集添加爲參考 – JaredPar 2010-11-05 17:00:24

回答

4

您不能在方法內定義屬性。將它移出課堂。嘗試:

[TestClass] 
public class GlobalInterfacesUnitTest 
{ 
    [ImportMany(AllowRecomposition = true)] 
    Lazy<IComponentGui, IImportComponentGuiCapabilites>[] Senders {get;set;} 

    [TestMethod] 
    public void TestMethod1() 
    { 

    } 
}