I have a WPF project that I need to compile, load as a DLL file, and access its components programmatically. I'm currently using the Roslyn Compiler in my function, but I'm open to alternative solutions if they can help resolve my issue. The problem I'm facing is that even though the compilation process completes successfully, I'm unable to access any of the Types defined in my WPF project from the compiled assembly.
What I have tried:
I've attempted to populate a Type array by loading the assembly and calling GetTypes(), but it returns an empty array. I have also ensured that the necessary assembly references are added using MetadataReference.CreateFromFile, including references to assemblies such as System.Windows and PresentationFramework. However, I'm still unable to access the Types defined in my project. I have tried various approaches such as MSBuild and CodeDom, but none of them have yielded the desired result. I would greatly appreciate any insights or suggestions on how to resolve this issue. Are there any additional steps or modifications I should consider in order to access the Types from the compiled WPF project?
I have also checked the emitResult.Diagnostics collection for any errors or warnings, but there are no reported issues during the compilation process. Here's a simplified version of the code I'm using in my function.
public void CompileAndBuildWithRoslyn(string projectFilePath, string outputDirectory)
{
var projectText = File.ReadAllText(projectFilePath);
var projectId = ProjectId.CreateNewId();
var versionStamp = VersionStamp.Create();
var projectInfo = ProjectInfo.Create(projectId, versionStamp, "MyProject", "MyProject", LanguageNames.CSharp)
.WithFilePath(projectFilePath);
var workspace = new AdhocWorkspace();
var project = workspace.AddProject(projectInfo);
var documents = project.Documents.ToList();
foreach (var document in documents)
{
var text = File.ReadAllText(document.FilePath);
var syntaxTree = CSharpSyntaxTree.ParseText(text);
var updatedDocument = document.WithSyntaxRoot(syntaxTree.GetRoot());
var updatedProject = project.AddDocument(updatedDocument.Name, syntaxTree.GetText());
project = updatedDocument.Project;
}
var compilationOptions = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)
.WithOptimizationLevel(OptimizationLevel.Debug);
var metadataReferences = new List<MetadataReference>
{
MetadataReference.CreateFromFile(typeof(object).Assembly.Location),
MetadataReference.CreateFromFile(typeof(System.Windows.MessageBox).Assembly.Location),
};
var compilation = CSharpCompilation.Create(
Path.GetFileNameWithoutExtension(projectFilePath),
documents.Select(doc => doc.GetSyntaxTreeAsync().Result),
metadataReferences,
compilationOptions
);
var outputFile = Path.Combine(outputDirectory, $"{project.AssemblyName}.dll");
EmitResult emitResult = compilation.Emit(outputFile);
if (emitResult.Success)
{
var assembly = Assembly.LoadFile(outputFile);
var types = assembly.GetTypes();
var myClassType = assembly.GetType("MPUserWPFApp.MVVM.ViewModels.Basics.BaseViewModel");
var myClassInstance = Activator.CreateInstance(myClassType);
var methodInfo = myClassType.GetMethod("Dispose");
methodInfo.Invoke(myClassInstance, null);
var myOtherClassType = assembly.GetType("Namespace.MyOtherClass");
var staticProperty = myOtherClassType.GetProperty("SomeStaticProperty");
var propertyValue = staticProperty.GetValue(null);
var customType = assembly.GetType("Namespace.CustomType");
var customInstance = Activator.CreateInstance(customType);
System.Windows.MessageBox.Show("Compilation and build process completed successfully.");
}
else
{
System.Windows.MessageBox.Show("Compilation and build process failed.");
foreach (var diagnostic in emitResult.Diagnostics)
{
System.Diagnostics.Debug.WriteLine(diagnostic.ToString());
}
}
}
private void Compile_Click(object sender, RoutedEventArgs e)
{
string projectFilePath = @"C:\Users\iwass\source\repos\MPAPP\MPUSERAPP\MPUSERAPP.csproj";
string OutPutDir = @"C:\Users\iwass\OneDrive\Bureau";
CompileAndBuildWithRoslyn(projectFilePath, OutPutDir);
}
Thank you in advance for your help!