C# 我可以从同一解决方案中的另一个项目访问项目的类和方法吗?
我有两个项目在一个解决方案。第一个是类库作为输出,第二个是WPF项目。第一个项目中有一个方法我想从第二个项目中访问,但我不能。我告诉我基金会不存在 我已经尝试使用AddReference菜单将引用添加到第二个项目中。在第二个项目引用树中,出现了第一个项目。 我包括了名称空间:使用firstproject 第一个项目代码:C# 我可以从同一解决方案中的另一个项目访问项目的类和方法吗?,c#,C#,我有两个项目在一个解决方案。第一个是类库作为输出,第二个是WPF项目。第一个项目中有一个方法我想从第二个项目中访问,但我不能。我告诉我基金会不存在 我已经尝试使用AddReference菜单将引用添加到第二个项目中。在第二个项目引用树中,出现了第一个项目。 我包括了名称空间:使用firstproject 第一个项目代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; us
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ClassLibrary2
{
public class Class1
{
public void displayTest()
{
Console.WriteLine("Hello World !");
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ClassLibrary2;
namespace CpowerPos
{
/// <summary>
/// Logique d'interaction pour MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void ButtonConnection(object sender, RoutedEventArgs e)
{
displayTest();
}
}
}
第二个项目代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ClassLibrary2
{
public class Class1
{
public void displayTest()
{
Console.WriteLine("Hello World !");
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ClassLibrary2;
namespace CpowerPos
{
/// <summary>
/// Logique d'interaction pour MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void ButtonConnection(object sender, RoutedEventArgs e)
{
displayTest();
}
}
}
您需要先创建类的实例,然后才能调用它
您可以这样做:
var c1 = new Class1();
c1.displayTest();
或者您可以将其制作为一行:
new Class1().displayTest();
您完成了将类库项目集成到WPF应用程序所需的所有过程 您只是错过了在主窗口中创建Class1的实例 要在MainWindow中实例化Class1,可以使用new运算符
Class1 c1 = new Class1(); //This will create instance of class1
c1.displayTest() //This will call display function
不要错过包含Class1的名称空间。您已经在项目中完成了,即使用ClassLibrary2 项目1
public static class Class1
{
public static void displayTest()
{
Console.WriteLine("Hello World !");
}
}
项目2
private void ButtonConnection(object sender, RoutedEventArgs e)
{
Class1.displayTest();
}
您需要将第一个项目dll的引用添加到第二个项目。右键单击->添加引用->选择第一个项目的dll。然后包含您试图访问的第一个项目中类型为的命名空间。此外,您发布的代码不应该首先编译,因为没有displayTest方法作为类型的成员,它也是一个实例方法。因此,您必须在访问Class1之前创建它的实例
private void ButtonConnection(object sender, RoutedEventArgs e)
{
Class1 c = new Class1();
c.displayTest();
}
您确实启用了从CpowerPos内部访问ClassLibrary2。 问题是Class1和DisplayText都不是静态的,因此就代码而言,它们还不存在 您可以将DisplayText设置为静态,允许您调用“Class1.DisplayText”,或者创建一个实例:
Class1 libraryClass = new Class1();
libraryClass.DisplayText();
方法是类的一部分,所以您需要创建Class1类的实例才能调用displayTest方法
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void ButtonConnection(object sender, RoutedEventArgs e)
{
var myObj = new Class1();
myObj.displayTest();
}
}
或者,您可以使该方法静态并调用,而无需创建实例
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void ButtonConnection(object sender, RoutedEventArgs e)
{
Class1.displayTest();
}
}
c还允许您访问类型的静态成员和嵌套类型,而无需使用类型名限定访问
using static ClassLibrary2.Class1;
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void ButtonConnection(object sender, RoutedEventArgs e)
{
displayTest();
}
}
做以下改变:1。将displayTest设为静态2。创建第一个项目的dll并将其加载到第二个项目。它应该是第二个项目的访问