缺少程序集,但是当我添加它时,表示无法找到元数据文件C#.NET [英] Missing assembly but when I add it it says metadata file could not be found C# .NET

查看:362
本文介绍了缺少程序集,但是当我添加它时,表示无法找到元数据文件C#.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好!



我似乎面临一个小困境,那就是我的应用程序不会编译C#代码我需要它来编译



如果我不添加引用它会说



Hello!

I seem to be facing a small dilemma and that would be that my application wont compile the C# code i need it to compile

If I dont add the reference it says

[The type or namespace name 'Window' could not be found (are you missing a using directive or an assembly reference?)





当我添加



and when I add

PresentationFramework.dll





它说



it says

Metadata file 'PresentationFramework.dll' could not be found





我做了一些研究,似乎



I've done some reasearch and it seems like

The type or namespace name 'Window' needs PresentationFramework.dll





为什么我的应用程序在向我发送此错误时我尝试在我的应用程序内编译(不是来自VS)





我的源代码





Why is my application throwing me this error when I try to compile inside my application (not from VS)


My source code

using Microsoft.CSharp;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Linq;
using System.Windows;


namespace SimpleBuilder
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// store code
    /// select what features
    /// print out textfile with all the code from the features
    /// compile that textfileContent to a exe
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        
        private void fakeMessageOne()
        {
            if(fakeMessageCheckbox.IsChecked == true)
            {
                fakeMessage1 fkmsg = new fakeMessage1();
                fkmsg.fakeMessage();
            }
        }

        private void button_Click(object sender, RoutedEventArgs e)
        {
            
            

            CSharpCodeProvider csc = new CSharpCodeProvider(new Dictionary<string, string>() { { "CompilerVersion", frameworkTextbox.Text } });
            CompilerParameters parameters = new CompilerParameters(new[] { "mscorlib.dll", "System.Core.dll" }, outputTextbox.Text, true);
            parameters.GenerateExecutable = true;
            parameters.ReferencedAssemblies.Add("Microsoft.CSharp.dll");
            parameters.ReferencedAssemblies.Add("System.dll");
            //parameters.ReferencedAssemblies.Add("PresentationFramework.dll"); commented this out since it wasnt working.

            CompilerResults result = csc.CompileAssemblyFromSource(parameters, sourceTextbox.Text);
            if (result.Errors.HasErrors)
            {
                result.Errors.Cast<CompilerError>().ToList().ForEach(error => errorTextbox.Text += error.ErrorText + "\r\n");
            }
            else
            {
                errorTextbox.Text = "--- Build Succeeded! ---";
            }

        }
    }
}





代码im尝试编译





The code im trying to compile

using Microsoft.CSharp;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Linq;
using System.Windows;


namespace SimpleBuilder
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// store code
    /// select what features
    /// print out textfile with all the code from the features
    /// compile that textfileContent to a exe
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void fakeMessageOne()
        {
            Messagebox.Show("Hello World");
        }
    }
}





我尝试了什么:



我已经完成了关于dll的一些重复,但是找不到任何其他的



What I have tried:

I've done some rerearch about the dll but cant find any others

推荐答案

问题是 PresentationFramework.dll 程序集不在编译器搜索路径中的目录中。相反,它位于主框架目录下的一个名为WPF的子目录中:

The problem is that the PresentationFramework.dll assembly isn't in a directory that's in the compiler's search path. Instead, it's in a sub-directory called "WPF" under the main framework directory:
parameters.ReferencedAssemblies.Add("WPF\\PresentationFramework.dll");



您还需要其他一些参考:


You'll also need some other references:

parameters.ReferencedAssemblies.Add("System.Xaml.dll");
parameters.ReferencedAssemblies.Add("WPF\\WindowsBase.dll");
parameters.ReferencedAssemblies.Add("WPF\\PresentationCore.dll");



您还需要在代码中更正 MessageBox 类的大小写进行编译。



随着这些更改,您将发现一个编译器错误:

名称'InitializeComponent'不会存在于当前上下文中



这是因为您的编译单元不包含Visual Studio在您创建时为您生成的任何文物一个新的WPF窗口,包括BAML (已编译的XAML)和部分类的其他部分。



如果查看现有的在Windows资源管理器中的WPF项目中,您将看到一个名为 obj 的文件夹。在此之下,在与配置同名的文件夹中(通常为 Debug Release ,你会看到每个WPF窗口都有三个编译器生成的文件:

  • WindowName.baml - 编译版本的XAML标记;
  • WindowName.g.cs - 定义字段, InitializeComponent 从嵌入资源加载BAML的方法,以及将字段连接到加载的BAML的代码;
  • WindowName.gics - 似乎是 .g.cs 的副本,可能用于Intellisense;

  • You'll also need to correct the case of the MessageBox class in your code to compile.

    With those changes in place, you'll then find a single compiler error:
    The name 'InitializeComponent' does not exist in the current context

    That's because your compilation unit doesn't include any of the artefacts that Visual Studio generates for you when you create a new WPF window, including the BAML (compiled XAML) and the other part of the partial class.

    If you look at an existing WPF project in Windows Explorer, you'll see a folder called obj. Under that, in a folder with the same name as the configuration (typically Debug or Release), you'll see that each WPF window has three compiler-generated files:

    • WindowName.baml - the compiled version of the XAML markup;
    • WindowName.g.cs - defines the fields, the InitializeComponent method to load the BAML from the embedded resources, and code to connect the fields to the loaded BAML;
    • WindowName.g.i.cs - seems to be a duplicate of .g.cs, possibly used for Intellisense;

    • 这篇关于缺少程序集,但是当我添加它时,表示无法找到元数据文件C#.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆