この質問に対するこの回答は、MEF ディスカッション リストで Kevin によって提供されました。次のコード スニペットを使用して、MEF のExportDefinitionおよびImportDefinitionデータ構造から必要なすべての情報を抽出できることがわかります。
最初のステップは、アセンブリ タイプをカタログにロードすることです。次に、カタログ内のパーツごとに、インポートとエクスポートの定義を繰り返します。エクスポート定義は、タイプ、メソッド、プロパティ、およびフィールドにのみ配置できます (私のコードは現時点では無視しています)。したがって、エクスポートを処理するには、次のコードを使用できます。
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);
}
プロパティ、パラメーター、およびフィールド (これも無視されます) にのみ配置できるインポートを処理するには、次のコードを使用できます。
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());
}
メソッドパラメータを介してインポートを提供できないように見えるため、上記のコードはそれらをサポートしていないことに注意してください。
すべてのエクスポートとインポートが処理されると、格納されたMemberInfoオブジェクトを文字列形式にシリアル化するという簡単な問題になります。