簡體   English   中英

遇到“不一致的可訪問性:返回類型比方法更難訪問”

[英]Experiencing "Inconsistent accessibility: return type is less accessible than method"

我在使用 Visual Studio 2010 用 C# 編碼時遇到了這個錯誤。

public List<Employee> employee_getData()
{
    List<Employee> employees;
    employees = new List<Employee>();
    connection = new SqlConnection(connectionStr);
    cmd = new SqlCommand("sp_employeeList", connection);

    try
    {
        connection.Open();
        dr = new SqlDataReader();
        dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            Employee e = new Employee(dr["e_ID"].ToString(), dr["e_Name"].ToString(), DateTime.Parse(dr["e_dayofBirth"].ToString()), dr["e_Regency"].ToString()
            , float.Parse(dr["e_salaryRate"].ToString()), dr["e_workingBranch"].ToString(), dr["e_workingPosition"].ToString(),
            dr["e_username"].ToString(), dr["e_passcode"].ToString());
            employees.Add(e);
        }
    }
    catch (Exception e)
    {
    }
    finally
    {
        connection.Close();
    }

    return employees;
}

它會引發異常,其中包含以下詳細信息:

不一致的可訪問性:返回類型“System.Collections.Generic.List”的可訪問性低於方法“ResortLib.DataAccessObject.DAO_Employee.employee_getData()”

您的方法被標記為public ,但您的類Employee的訪問權限較低。 可能您沒有在類定義中指定任何訪問說明符,因此將其internal 您可以通過在類中指定public來修復該錯誤,例如:

public class Employee 
{
//your class clode

看看這個樣本:

// if you not specify explictly the access modifier
// it is internal for class/struct inside namespace
// or private for inner types
/*internal*/ class A
{
}

public class B
{
    public List<A> GetA() // <- this line give error Inconsistent accessibility...
    {
        return null;
    }
}

因此,為了解決您的問題,您可以將Employee類設為公開,或者將您的方法employee_getData設為內部。 作為旁注注意 C# 中的命名約定,您可以在網上找到很多信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM