[英]Why am I obtaining this inconsistent accessibility error on the return type of a method?
I am working on a .NET project. 我正在研究.NET项目。 In a DAO class (using ADO.NET but this is not important) I have a method like this: 在DAO类中(使用ADO.NET,但这并不重要),我有这样的方法:
public static List<Inoltro> GetListaInoltriUltimiGiorni(int IdUor, string Protocollista, int NumeroGiorni, DBConnection config)
{
string query = PROT_INOLTRO.INOLTRI_ULTIMI_N_GIORNI_BY_UOR_AND_PROTOCOLLISTA;
List<Inoltro> result = new List<Inoltro>();
using (SqlConnection con = ArxeiaConnection.getARXEIAStringConnection(config.Tenant + "_" + config.Database))
{
using (SqlCommand cmd = new SqlCommand(query, con))
{
try
{
cmd.Parameters.AddWithValue("@IdUor", IdUor);
cmd.Parameters.AddWithValue("@Protocollista", Protocollista);
cmd.Parameters.AddWithValue("@NumeroGiorni", NumeroGiorni);
con.Open();
using (SqlDataReader reader = cmd.ExecuteReader())
{
if (reader.HasRows)
{
// Read advances to the next row.
while (reader.Read())
{
.........................................
.........................................
.........................................
}
}
}
}
catch (Exception ex)
{
con.Close();
throw ex;
}
}
}
return result;
}
As you can see I am creating and returning this object: 如您所见,我正在创建并返回此对象:
List<Inoltro> result = new List<Inoltro>();
The problem is that Visual studio give me the following error on this method signature: 问题是Visual Studio在此方法签名上给我以下错误:
Inconsistent accessibility: return type
List<Inoltro>
is less accessible than method MyClass.GetListaInoltriUltimiGiorni(....) 可访问性不一致:返回类型List<Inoltro>
的访问性比方法MyClass.GetListaInoltriUltimiGiorni(....)
Why? 为什么? What am I missing? 我想念什么? How can I fix it? 我该如何解决?
Your method is public
but Inoltro
is internal
. 您的方法是public
但Inoltro
是internal
。 You cannot have a public
method that exposes an internal
type. 您不能具有公开internal
类型的public
方法。 Either make the method internal
or the type public
. 将方法internal
方法或将类型public
。
While we are looking at your code, a few things come to mind. 在查看您的代码时,会想到一些注意事项。
First, the try catch
is unnecessary, since the using
already does a Close
on the connection if an exception is thrown. 首先, try catch
是不必要的,因为如果引发异常, using
会在连接上执行Close
。 Eliminate the try catch
. 消除try catch
。
Second, for your future reference, never say throw ex;
其次,请不要再throw ex;
供您将来参考throw ex;
in a catch
. catch
。 Say throw;
说throw;
to re-throw an exception. 重新抛出异常。 The difference is subtle. 区别是微妙的。 throw;
re-throws the exact exception that was caught. 重新引发捕获的确切异常。 throw ex;
re-throws the exception but resets the stack trace to the current trace, not the original trace. 重新引发异常,但将堆栈跟踪重置为当前跟踪,而不是原始跟踪。
The only time you want to do throw ex;
您唯一想做的事就是throw ex;
is when you deliberately want to obscure where an exception came from, for instance, because there is a trust boundary between the current code and the caller. 例如,当您故意要掩盖异常的来源时,例如,因为当前代码与调用方之间存在信任边界。 However, in that case it would be better still to throw a new, generic exception. 但是,在那种情况下,最好还是抛出一个新的通用异常。
Third, I don't understand why you have both a while
and an if
to check to see if the set has rows. 第三,我不明白为什么您要花while
和一个if
来检查集合是否有行。 Surely if the set is empty then the Read
will return false, so there was never a need for the if
, right? 当然,如果集合为空,则Read
将返回false,因此永远不需要if
,对吧?
Fourth, this is just a style point; 第四,这只是一个风格点; normally we would format 通常我们会格式化
using (SqlConnection con = ArxeiaConnection.getARXEIAStringConnection(config.Tenant + "_" + config.Database))
{
using (SqlCommand cmd = new SqlCommand(query, con))
{
as 如
using (SqlConnection con = ArxeiaConnection.getARXEIAStringConnection(config.Tenant + "_" + config.Database))
using (SqlCommand cmd = new SqlCommand(query, con))
{
It is common to have a using
whose body is only another using
, and in that scenario the unnecessary braces and indentation do not make the code easier to understand. 通常,只有一个using
的主体只是另一个using
,在这种情况下,不必要的花括号和缩进不会使代码更易于理解。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.