错误1可访问性不一致: [英] Error 1 Inconsistent accessibility:

查看:85
本文介绍了错误1可访问性不一致:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行下面代码的构建时,它显示以下错误



错误1可访问性不一致:返回类型'DevotionJson.Devotion'不如方法'DevotionJson.MainPage.ReadJsonFile(string)'



分类:

<前lang =cs> public class 投入
{
public string 日期{获取; set ;}
public string 标题{获取; set ;}
public string Verse { get ; set ;}

[JsonProperty( 阅读章节)]
public string ReadChapter {获得; set ;}

[JsonProperty( 读取文本)]
public string ReadText {获得; set ;}

[JsonProperty( 一年内的圣经)]
public string BibleInOneYear { get ; set ;}

public 字符串消息{ get ; set ;}
public string Notes { get ; set ;}
}





Xaml.cs

 私人列表< devotion>奉献; 

public App()
{
InitializeComponent();
// 因为填充费用很昂贵
// 您只想在应用启动时执行一次
devotions = new List< devotion>();
AddDevotions();
}

受保护 覆盖 void OnNavigatedTo(NavigationEventArgs e)
{
int index = DateTime.Now.DayOfYear - 1 ; // 列表从0索引
textblock.Text = devotions [index] .Message; // 或其他一些属性
}

私有 void AddDevotions()
{
for int i = 1 ; i& lt; = 366 ; i ++)
{
string filePath = Assets / Dec + i.ToString()+ JS;
Devotion d = ReadJsonFile(filePath);
devotions.Add(d);
}
}

public Devotion ReadJsonFile( string JsonfilePath)
{
投入d = null ;

使用(StreamReader r = new StreamReader(JsonfilePath))
{
string json = r.ReadToEnd();
d = JsonConvert< devotion> .DeserializeObject(json);
}
return d;
}





请提供解决方案。

提前感谢您的回复

解决方案

错误信息可以自我解释,但也要考虑它的逻辑。



你的方法 ReadJsonFile 是公开的,以及它的声明类。在这种情况下,该方法可以被某些不同类型的代码使用,甚至可以在不同的程序集中使用(限制程序集,使用 internal ;尽量不要给比实际需要更多的访问权限。



什么代码可以真正使用它?只有代码也可以访问类型 Devotion 字符串(怎么回事?)。似乎是这种情况,因为您显示 Devotion 的公共定义。结论是什么?您可能有两个或更多 Devotion 声明,而有问题的代码段则使用其他类型。解?找出它然后将所有代码减少到仅使用一个 Devotion 类型,或者使有问题的类型也公开(或与方法一致)。



此外,当查询者显示不同的代码时,我在此论坛上看到了很多案例,与代码制作麻烦不同。另外,请正确检查。你的问题非常简单。



请注意,相反的情况不会出问题。如果参数或返回类型比方法更容易访问会有什么问题?没什么,一直都在做。



-SA


When i ran the build of the code below it shows the following error

"Error 1 Inconsistent accessibility: return type 'DevotionJson.Devotion' is less accessible than method 'DevotionJson.MainPage.ReadJsonFile(string)'"

Class:

public class Devotion
{
    public string Date {get; set;}
    public string Title {get; set;}
    public string Verse {get; set;}

    [JsonProperty("Read Chapter")]
    public string ReadChapter {get; set;}

    [JsonProperty("Read Text")]
    public string ReadText {get; set;}

    [JsonProperty("Bible In One Year")]
    public string BibleInOneYear {get; set;}

    public string Message {get; set;}
    public string Notes {get; set;}
}



Xaml.cs

private List<devotion> devotions;

public App()
{
   InitializeComponent();
   // as this will be expensive to populate
   // you only want to do it once when the app starts
   devotions = new List<devotion>();
   AddDevotions();
}

protected override void OnNavigatedTo(NavigationEventArgs e)
{
   int index = DateTime.Now.DayOfYear - 1; // list is indexed from 0
   textblock.Text = devotions[index].Message; // or some other property
}  

private void AddDevotions()
{
   for(int i = 1; i &lt;= 366; i++)
   {
      string filePath = "Assets/Dec" + i.ToString() + ".js";
      Devotion d = ReadJsonFile(filePath);
      devotions.Add(d);
   } 
}

public Devotion ReadJsonFile(string JsonfilePath)
{
   Devotion d = null;

   using (StreamReader r = new StreamReader(JsonfilePath))
   { 
      string json = r.ReadToEnd();
      d = JsonConvert<devotion>.DeserializeObject(json);
   }
   return d; 
}



Kindly proffer a solution.
Thank you in advance and reply soon

解决方案

The error message is quite self explainable, but also think of the logic of it.

Your method ReadJsonFile is made public, as well as its declaring class. In this case, the method can be used by some code you have in some different type and even in different assembly (to limit to the assembly, use internal; try not to give more access than it is really needed).

What code can really use it? Only the code which has also access to the types Devotion and string (how else?). It seems to be the case, because you show the public definition of Devotion. Conclusions? It's possible that you have two or more Devotion declarations, and the problematic piece of code uses another type. Solution? Find it out and then either reduce all the code to using just one Devotion type, or make the type in question also public (or consistent with the method).

Also, I saw many cases on this forum when an inquirer shows different code, not the same as the code making trouble. Also, check it up properly. Your problem is extremely easy.

Note that the opposite situations won't make a problem. What would be wrong if parameter or return types were more accessible than method? Nothing, it's done all the time.

—SA


这篇关于错误1可访问性不一致:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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