jeudi 30 novembre 2017

MEF Export Provider throws exception

I am receiving the following exception when trying to import a type that uses an Export Provider:

ImportDefinition of type 'System.ComponentModel.Composition.Primitives.ContractBasedImportDefinition' cannot be used in this context. Only import definitions produced by the ReflectionModelServices.CreateImportDefinition are supported.

    protected override IEnumerable<Export> GetExportsCore(ImportDefinition definition, AtomicComposition atomicComposition)
    {
        if (definition.ContractName == typeof(Interface.ILogger).FullName)
        {
            try
            {
                string typeName = null;
                if (ReflectionModelServices.IsImportingParameter(definition))
                {
                    Lazy<System.Reflection.ParameterInfo> paramInfo = ReflectionModelServices.GetImportingParameter(definition);
                    typeName = paramInfo.Value.Member.ReflectedType.FullName;
                }
                else
                {
                    LazyMemberInfo info = ReflectionModelServices.GetImportingMember(definition);
                    typeName = info.GetAccessors()?.FirstOrDefault()?.ReflectedType.FullName;
                }

                return new Export[] { new Export(definition.ContractName, () => { return LogManager.GetLogger(typeName, typeof(PassthroughLogger)); }) };
            }
            catch (ArgumentException) { }
        }
        return Enumerable.Empty<Export>();
    }

The exception is thrown when it reaches

ReflectionModelServices.IsImportingParameter(definition)





Aucun commentaire:

Enregistrer un commentaire