C#获取/设置不可访问性错误
我得到一个错误,如下所示: 可访问性不一致:属性类型“AudioDevices.Tracks.track.Time”的可访问性低于属性“AudioDevices.Tracks.track.length” 我不知道它是什么,也不知道如何修复它。有人能帮我吗 这是我所有的代码,[模板=类库]:C#获取/设置不可访问性错误,c#,get,set,C#,Get,Set,我得到一个错误,如下所示: 可访问性不一致:属性类型“AudioDevices.Tracks.track.Time”的可访问性低于属性“AudioDevices.Tracks.track.length” 我不知道它是什么,也不知道如何修复它。有人能帮我吗 这是我所有的代码,[模板=类库]: namespace AudioDevices.Tracks { public class Track { #region STRUCT private int id;
namespace AudioDevices.Tracks
{
public class Track
{
#region STRUCT
private int id;
private string name;
private string artist;
private string albumSource;
private Time length;
private category style;
public enum category{
Ambient, Blues, Country, Disco, Electro, Hardcore, HardRock, HeavyMetal, Hiphop, Jazz, Jumpstyle,
Klassiek, Latin, Other, Pop, Punk, Reggae, Rock, Soul, Trance, Techno
};
#endregion
#region GET/SET
public int Id{
get { return id; }
set { id = value; }
}
public string Name{
get { return name; }
set { name = value; }
}
public string Artist{
get { return artist; }
set { artist = value; }
}
public string AlbumSource{
get { return albumSource; }
set { albumSource = value; }
}
public Time Length{
set { length = value; }
}
public string DisplayTime
{
get { return length.ToString(); }
}
public category Style
{
get { return style; }
set { style = value; }
}
#endregion
#region TIME CONSTRUCTOR
struct Time
{
int seconds;
int minutes;
int hours;
public Time(int seconds)
{
this.seconds = seconds;
this.minutes = 0;
this.hours = 0;
}
public Time(int seconds, int minutes)
{
this.seconds = seconds;
this.minutes = minutes;
this.hours = 0;
}
public Time(int seconds, int minutes, int hours)
{
this.seconds = seconds;
this.minutes = minutes;
this.hours = hours;
}
public override string ToString()
{
return hours + ":" + minutes + ":" + seconds;
}
}
#endregion
#region TRACK CONSTRUCTOR
public Track(){ }
public Track(int id)
{
this.id = id;
}
public Track(int id, string name)
{
this.id = id;
this.name = name;
}
public Track(int id, string name, string artist)
{
this.id = id;
this.name = name;
this.artist = artist;
}
#endregion
#region GetLength
public string GetLength()
{
return length.ToString();
}
public int GetLengthInSeconds(int seconds, int minutes, int hours){
int SecondsToSeconds = seconds;
int MinutesToSeconds = minutes * 60;
int HoursToSeconds = hours * 3600;
int TotalSeconds = HoursToSeconds + MinutesToSeconds + SecondsToSeconds;
return TotalSeconds;
}
#endregion
}
}您在这里拥有一处公共财产:
public Time Length{
set { length = value; }
}
。。。但该属性的类型是时间
,它是一种私有类型:
(它是私有的,因为它是嵌套类型;如果它被声明为顶级类型,默认情况下它将是内部的,这仍然会有相同的问题。)
公共成员签名不能引用参数类型或返回类型中的任何位置的私有或内部类型。如果调用者在不同的程序集中,那么该成员对调用者来说就没有意义了
因此,解决方法是要么将
Time
设置为公共类型(我建议同时将其提取为顶级类型),要么将Time
设置为私有属性。这可能是因为您使用的是构造时间
尝试更改您的代码,如:
public Time Length{
set { length = new Time(value); }
}
来自
类成员和结构成员的访问级别,包括
嵌套类和结构,默认情况下是私有的
因此,默认情况下,Time
结构是private
关于这一部分
public Time Length
{
set { length = value; }
}
您正在尝试创建public
属性类型为private
struct。你不能那样做。为了修复它
- 将
属性访问修饰符Length
更改为public
private
- 将
struct access修饰符设置为Time
public
长度
(因为它是公共的),但不能看到时间
(因为它是私有的)。基本上,您的原始代码表示:任何人都可以通过传入时间对象来设置长度。什么是时间对象?这是个秘密!::伸出舌头:
public Time Length
{
set { length = value; }
}