tags:

views:

131

answers:

1

I have to register an object instance into a container. I can't use the generic ComposeExportedValue<T> since I don't know what T is at compile time.

The method I need is something like this: container.RegisterInstance(Type someType, object instance)

Any ideas?

Thanks.

A: 

Here's a version of ComposeExportedValue that is not generic:

public static void ComposeExportedValue(this CompositionContainer container, string contractName, object exportedValue) {
    if (container == null)
        throw new ArgumentNullException("container");
    if (exportedValue == null)
        throw new ArgumentNullException("exportedValue");
    CompositionBatch batch = new CompositionBatch();
    var metadata = new Dictionary<string, object> {
        { "ExportTypeIdentity", AttributedModelServices.GetTypeIdentity(exportedValue.GetType()) }
    };
    batch.AddExport(new Export(contractName, metadata, () => exportedValue));
    container.Compose(batch);
}

public static void ComposeExportedValue(this CompositionContainer container, object exportedValue) {
    if (container == null)
        throw new ArgumentNullException("container");
    if (exportedValue == null)
        throw new ArgumentNullException("exportedValue");
    ComposeExportedValue(container, AttributedModelServices.GetContractName(exportedValue.GetType(), exportedValue);
}
Julien Lebosquain
Are you sure this works? I am aware of the container -> composition parameter name mismatch, apart from this the code still doesn't work.
yang
Ok my mistake. Correcting contract names fixed the problem. Thanks a lot!
yang