4

For my application as described in this question I want to use MEF to scan the available plugin assemblies and then store all the available import and export information in a serialized format (e.g. a set of strings or a memory stream). This is necessary because I need to transfer the import and export information over an AppDomain boundary without loading the plugin assemblies (essentially I want to delay load the plugins). I found some references, for instance this one or this one but none of the links gave me any idea how to:

  • Extract all the imports and exports from an assembly
  • Serialize all the required import/export information
  • And then later on re-hydrate the serialized information back into imports and exports.

I think I can use the ReflectionModelServices class to create Import/Export definitions but that still leaves the serialization and deserialization parts. Can anybody point me to some examples, documentation or provide me with suggestion as to how to go about these steps?

2 Answers 2

8

This answer to this question was provided by Kevin on the MEF discussion list. It turns out that one can extract all the required information from the MEF ExportDefinition and ImportDefinition data structures with the following code snippets.

The first step is to load the assembly types into a catalog. Then for each part in the catalog iterate over the import and export definitions. Export definitions can only be placed on types, methods, properties and fields (which my code ignores at the moment). So to process the exports the following code can be used.

var exports = new List<Tuple<string, MemberInfo>>();
foreach (var export in part.ExportDefinitions)
{
    var memberInfo = ReflectionModelServices.GetExportingMember(export);
    Tuple<string, MemberInfo> exportDefinition = null;
    switch (memberInfo.MemberType)
    {
        case MemberTypes.Method:
            exportDefinition = new Tuple<string, MemberInfo>(export.ContractName, memberInfo.GetAccessors().First() as MethodInfo);
            break;
        case MemberTypes.NestedType:
        case MemberTypes.TypeInfo:
            exportDefinition = new Tuple<string, MemberInfo>(export.ContractName, memberInfo.GetAccessors().First() as Type);
            break;
        case MemberTypes.Property:
            // this is a bit ugly because we assume that the underlying methods for a property are named as:
            // get_PROPERTYNAME and set_PROPERTYNAME. In this case we assume that exports always
            // have a get method.
            var getMember = memberInfo.GetAccessors().Where(m => m.Name.Contains("get_")).First();
            var name = getMember.Name.Substring("get_".Length);
            var property = getMember.DeclaringType.GetProperty(name);
            exportDefinition = new Tuple<string, MemberInfo>(export.ContractName, property);
            break;

        default:
            throw new NotImplementedException();
    }

    exports.Add(exportDefinition);
}

In order to process the imports, which can only be placed on properties, parameters and fields (which are again ignored) the following code can be used:

public void ExtractImports()
{
    var imports = new List<Tuple<string, string>>();
    foreach (var import in part.ImportDefinitions)
    {
        SerializedImportDefinition importDefinition = !ReflectionModelServices.IsImportingParameter(import)
            ? importDefinition = CreatePropertyImport(import)
            : importDefinition = CreateConstructorParameterImport(import);
    }
}

private Tuple<string, string> CreatePropertyImport(ImportDefinition import)
{
    var memberInfo = ReflectionModelServices.GetImportingMember(import);
    if (memberInfo.MemberType != MemberTypes.Property)
    {
        throw new ArgumentOutOfRangeException("import");
    }

    // this is a bit ugly because we assume that the underlying methods for a property are named as:
    // get_PROPERTYNAME and set_PROPERTYNAME. In this case we assume that imports always
    // have a set method.
    var getMember = memberInfo.GetAccessors().Where(m => m.Name.Contains("set_")).First();
    var name = getMember.Name.Substring("set_".Length);
    var property = getMember.DeclaringType.GetProperty(name);
    return new Tuple<string, string>(import.ContractName, property.ToString());
}

private Tuple<string, string> CreateConstructorParameterImport(ImportDefinition import)
{
    var parameterInfo = ReflectionModelServices.GetImportingParameter(import);
    return new Tuple<string, string>(import.ContractName, parameterInfo.Value.ToString());
}

Note that it seems that imports cannot be provided via method parameters so the code above does not support those.

Once all the exports and imports have been processed it is a simple question of serializing the stored MemberInfo objects into a string format.

5
  • 1
    Very good job. I was looking for something similar a while ago as you can see in some of my questions, and soon I'll revisit this feature. Thanks for sharing and taking the time to answer your own question.
    – Adi Lester
    Oct 24, 2012 at 23:01
  • Where does the memberInfo come from in memberInfo.GetAccessors()?
    – l33t
    Jan 13, 2016 at 12:08
  • @l33t I've updated the code sample to include the part that gets the memberInfo object. I hope that makes it more clear. Thanks for pointing out the missing code lines.
    – Petrik
    Jan 15, 2016 at 0:22
  • Thanks! Your code doesn't handle the Metadata, right? Only the injected imports in the constructor and properties. Is that correct?
    – l33t
    Jan 15, 2016 at 9:38
  • @l33t No it doesn't by default. I'm not quite sure how hard it will be to add that capability. So far I haven't needed it.
    – Petrik
    Jan 19, 2016 at 19:01
0

I've had the same requirement and ended up implementing something very similar to what @Petrik has mentioned in his answer. My LazyAssemblyLoading solution is available on GitHub.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.