Dynamic Assemblies without pointing to specific dependency versions? RRS feed

  • Question

  • We have a scenario where we create assemblies through code with the AssemblyBuilder class, and embed a XAML file (actually a WF.NET xaml workflow definition).

    Everything worked great until we ran into an upgrade version.  We are working on version 2 of the product and all of a sudden the dependencies for the dynamically built assemblies can no longer be resolved. 

    I believe this is because the assembly was built using (for example) dependency versions, and our next release uses dependency versions ... which no longer apply.

    I think my question is this - is it possible to dynamically build an assembly without it's dependencies pointing to (or looking for) a specific version?  Sort of like how you can change the VS reference of SpecificVersion="False" .... can u do the same thing somehow in code?

    Here's a snippet for how we're compiling the assembly:

     AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName(ns) { Version = new Version(1, 0, 0, 0) },
    AssemblyBuilderAccess.RunAndSave, targetfolder);
    ModuleBuilder mb = ab.DefineDynamicModule(ns + ".dll", ns + ".dll");
    // Part 0: load the XAML
    XamlSchemaContext schemaContext = new XamlSchemaContext();
    Stream stream = new MemoryStream(ASCIIEncoding.Default.GetBytes(wfXamlDef.WorkflowXAMLDefinition));
    object xamlLoadedObject;
    using (var xamlReader = new XamlXmlReader(stream))
                    var builderReader = ActivityXamlServices.CreateBuilderReader(xamlReader, schemaContext);
                    xamlLoadedObject = XamlServices.Load(builderReader);
    ActivityBuilder activityBuilder = (ActivityBuilder)xamlLoadedObject;
    activityBuilder.Name = Eclipsys.Infrastructure.VisualWorkflow.Security.InfrastructureConstants.SavedWorkflowClassName;
    // Part 1: define the class
    TypeBuilder newClass = BuildType(mb, activityBuilder, false, wfXamlDef);
    Type createdType = newClass.CreateType();
    string magicXaml = GenerateXAML(activityBuilder, fakeType);
    MemoryStream stringStream = new MemoryStream(Encoding.Unicode.GetBytes(magicXaml));
    // Part 2: embed the XAML as a manifest
    string xamlResourceName = createdType.FullName + ".xaml";
    mb.DefineManifestResource(xamlResourceName, stringStream, ResourceAttributes.Public);
    // End: save the generated assembly
        ab.Save(ns + ".dll");

    Tuesday, November 6, 2012 10:32 PM


All replies

    • Proposed as answer by Mike Feng Thursday, November 8, 2012 2:13 PM
    • Marked as answer by Mike Feng Wednesday, November 14, 2012 1:39 PM
    Wednesday, November 7, 2012 12:51 AM
  • Sorry, I'm not really clear on how would help in our scenario ... could you clarify?
    Wednesday, November 7, 2012 3:07 PM
  • You could define a code contract of the dynamically loaded assembly and implement the contract in a plugin. Then you find the plugin based on the location specified in the config file, via MEF.

    Visual C++ MVP

    Wednesday, November 7, 2012 3:09 PM