【发布时间】:2014-01-29 23:34:37
【问题描述】:
虽然我在这里找到了一些类似的问题,但我没有找到可以帮助我解决问题的答案。 我有一个包含 2 个项目的解决方案(C#):P1(类库)和 P2(控制台应用程序)。我确实在 P2 中添加了一个引用,并在 P2 中添加了使用语句,并且所有类都是公共的(在两个项目中)。但是,当我在 P2 中编写一个方法时,我需要从 P1 中的一个类中调用一个方法,但实际上做不到。我可以调用 P1 中任何类的所有其他方法,除了这个。可能是什么问题?
这是 P1 中的课程:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace P1
{
public class Song
{
private string name;
private double length;
public double Length { get { return length; } set { length = value; } }
public Song(string name, double length)
{
this.name = name;
this.length = length;
}
public Song ReadSong()
{
Console.WriteLine("Song name: ");
string songName = Console.ReadLine();
bool wrongEntry = true;
double songLength = 0;
while (wrongEntry)
{
Console.WriteLine("Song length: ");
string songLengthStr = Console.ReadLine();
try
{
songLength = Double.Parse(songLengthStr);
wrongEntry = false;
}
catch (FormatException)
{
Console.WriteLine("Error!");
}
}
return new Song(songName, songLength);
}
}
}
这是 P2 中的课程:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using P1;
namespace P2
{
public class Festival
{
private string name;
private List<Artist> listOfArtists;
public Festival(string name)
{
this.name = name;
listOfArtists = new List<Artist>();
}
public Artist ReadArtst()
{
Console.WriteLine("Artist name: ");
string artName = Console.ReadLine();
Console.WriteLine("Date of birth: ");
DateTime dat = DateTime.Parse(Console.ReadLine());
Artist art = new Artist(artName, dat);
Console.WriteLine("How many songs do you want to enter: ");
int number = Int32.Parse(Console.ReadLine());
for (int i = 0; i < number; i++)
{
}
return art;
}
static void Main(string[] args)
{
}
}
}
在 for 循环中的 Festival 类(在第二个项目中)中,我需要调用 ReadSong()(来自第一个项目的 Song 类),但由于某种原因它不能被调用。我还尝试使用我拥有的其他一些方法(public void AddSong(Song newSong),它在 Artist 类中实现,并且有效)。 ReadSong() 是我无法在 for 循环中调用的唯一方法。
【问题讨论】:
-
不看代码很难说。尝试清理和重建。
-
我最好的猜测是在某处涉及
interface。 -
有时这是由依赖项目未引用所需程序集引起的。 P2 项目是否以“客户端框架”为目标?
-
清理和重建没有帮助。我这里没有使用界面。不,它不是针对客户端框架。
-
显示一些代码。没有代码,我们几乎只能猜测。据我们所知,您正在尝试使用错误的方法签名来调用它。没有代码,我们无法知道它是 c# 的一些晦涩的“功能”,还是您的一个简单的错字。