简体   繁体   English

为什么在方法的返回类型上获得此不一致的可访问性错误?

[英]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 . 您的方法是publicInoltrointernal 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.

相关问题 为什么我收到错误错误 1 ​​不一致的可访问性:返回类型? - Why am I getting the error Error 1 Inconsistent accessibility: return type? 错误 1 ​​不一致的可访问性:返回类型的可访问性低于方法 - Error 1 Inconsistent accessibility: return type is less accessible than method 错误:可访问性不一致:与方法“相比,返回类型的访问性较差 - Error : Inconsistent accessibility: return type is less accessible than method ' 可访问性不一致:返回类型小于方法 - Inconsistent accessibility: return type is less than method 为什么我在这堂课上出现不一致的可访问性? - Why am I getting inconsistent accessibility in this class? 我有这个错误:错误2可访问性不一致:返回类型&#39;johny.Form1.CoOrds&#39;比方法&#39;johny.Form1.toto()更难访问 - I had this error: Error 2 Inconsistent accessibility: return type 'johny.Form1.CoOrds' is less accessible than method 'johny.Form1.toto() 为什么会出现不一致的可访问性错误? - Why do I get Inconsistent Accessibility error? 不一致的可访问性:返回类型比 RESTful 服务中的方法更难访问 - Inconsistent accessibility: return type is less accessible than method in RESTful service 遇到“不一致的可访问性:返回类型比方法更难访问” - Experiencing "Inconsistent accessibility: return type is less accessible than method" C# 不一致的可访问性:返回类型的可访问性低于方法 - C# Inconsistent accessibility: return type is less accessible than method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM