easier way to get assembly location RRS feed

  • Question

  • using the Roslyn CSharpCompilation.Create method.  Have to pass the assemblies referenced by the code being created.  I call MetadataReference.CreateFromFile for each assembly.  Passed to that method is the Location of the Assembly. 

    For each assembly I first new up an object in that assembly. Then GetType of the object.  Then use the Assembly.Location property of the type to get the Location of the Assembly.

    Is there an easier way?


            string assemblyFileName = "gen" + Guid.NewGuid().ToString().Replace("-", "") + ".dll";
            var mscorlib = MetadataReference.CreateFromFile(typeof(object).Assembly.Location);
            System.IO.FileFormatException xx = new FileFormatException();
            var tx = xx.GetType();
            var s3 = tx.AssemblyQualifiedName;
            var tx4 = Type.GetType(s3);
            var windowsBase = MetadataReference.CreateFromFile(tx.Assembly.Location);
            DragAction da = DragAction.Cancel;
            var tx2 = da.GetType();
            var presentationCore = MetadataReference.CreateFromFile(tx2.Assembly.Location);
            EventTrigger trig = new EventTrigger();
            var tx3 = trig.GetType();
            var presentationFramework = MetadataReference.CreateFromFile(tx3.Assembly.Location);
            CSharpCompilation compilation = CSharpCompilation.Create(assemblyFileName,
                new[] { CSharpSyntaxTree.ParseText(fooSource) },
                  new[] { mscorlib, windowsBase, presentationCore, presentationFramework },
                new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)

    Saturday, August 6, 2016 5:15 AM


  • Hi Steve Richter,

    We could use CurrentDomain.GetAssemblies() to retrieve all Assembly of the project. then you could filter the special assembly as you requirement.

    var assList = AppDomain.CurrentDomain.GetAssemblies().ToList();
                foreach (Assembly a in assList)

    Best regards,

    Cole Wu

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Steve Richter Monday, August 8, 2016 8:58 PM
    Monday, August 8, 2016 7:22 AM