1

次のコードで次のエラーが発生し続けます。

Error: "No exports were found that match the constraint: 

ContractName    MefTestSample.Contracts.ICanDoSomethingImportant"

Program.cs は次のとおりです。

namespace MefTestSample
{
    class Program
    {
        private static CompositionContainer m_Container;


        static void Main(string[] args)
        {
            UseMockedUpTypes();
            ICanDoSomethingImportant cool = m_Container.GetExport<ICanDoSomethingImportant>().Value;
            cool.DoSomethingClever();
            Console.ReadLine();
        }

        private static void UseMockedUpTypes()
        {
            //The commented out section works just by itself.  
            /*
            m_Container =
                new CompositionContainer(
                    new AssemblyCatalog(
                        typeof(MefTestSample.Mocks.ClassesDoNotNecessarly).Assembly));
             */

            //This fails if i dont comment out the [ImportingConstructor] block.
            var assemblyCatalog1 = new AssemblyCatalog(typeof (MefTestSample.Mocks.ClassesDoNotNecessarly).Assembly);
            var myassembly = new AssemblyCatalog(typeof (ServiceLibrary.MoonService).Assembly);
            var aggregateCatalog = new AggregateCatalog(assemblyCatalog1, myassembly);
            m_Container = new CompositionContainer(aggregateCatalog);
        }
    }
}

以下は ClassesDoNotNecessarly のコードです。

namespace MefTestSample.Mocks
{
    [Export(typeof(ICanDoSomethingImportant))]
    public class ClassesDoNotNecessarly : ICanDoSomethingImportant
    {
        //private IServicesContract _isc;
        #region ICanDoSomethingImportant Members

        /* This seems to be causing the problem.
        [ImportingConstructor]
        public ClassesDoNotNecessarly([Import("Moon")]IServicesContract isc)
        {
            string temp = isc.DisplayMessage();
            Console.WriteLine(temp);
        }
        */

        public void DoSomethingClever()
        {
            Console.WriteLine("Hehe, I'm actually procrastinating!");        
        }

        #endregion
    }
}

MoonService は次のとおりです。

namespace ServiceLibrary
{
    [Export(typeof(IServicesContract))]
    public class MoonService : IServicesContract
    {
        public string DisplayMessage()
        {
            return "Moon services were accessed.";
        }
    }
}

問題は何だと思いますか。program.cs はそのままにして、ClassesDoNotNecessarly クラスの [ImportingConstructor] 属性 + コンストラクターをコメントアウトすると、プログラムはそのクラスのテキストを表示します。

[ImportingConstructor] 属性 n コンストラクターのコメントを外すと、この質問の上部に示されているエラーが発生し始めます。

どんなアイデアでも大歓迎です。

4

1 に答える 1

1

コンストラクターで を削除する[Import("Moon")]と、エクスポートには名前が付けられないため、代わりにインポートに名前を付けることはできません。

于 2012-10-16T09:06:43.747 に答える