最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】C#函数编译出错:Inconsistent accessibility: parameter type ‘aaa.bbb.ccc’ is less accessible than method ‘aaa.bbb.functionName(aaa.bbb.ccc)

C# crifan 8369浏览 0评论

【问题】

C#的函数:

        //save product info
        public void saveProductInfo(AmazonProductInfo productInfo)
        {
            return;
        }

被别处调用:

        //check whether each product valid or not
        //if valid, extract product info
        //http://www.amazon.com/Silver-Linings-Playbook/dp/B00CL68QVQ/ref=sr_1_2?s=instant-video&ie=UTF8&qid=1368688342&sr=1-2
        private void checkAndExtractForSingleProduct(string productUrl)
        {
            bool isProductValid = false;
            string invalidReason = "";

            //string respHtml = crl.getUrlRespHtml(productUrl);
            string productHtml = crl.getUrlRespHtml_multiTry(productUrl);
            
            string usedAndNewUrl = "";
            isProductValid = checkProductValid(productUrl, productHtml, out invalidReason, out usedAndNewUrl);

            if (isProductValid)
            {
                AmazonProductInfo productInfo = extractProductInfo(productUrl, productHtml, usedAndNewUrl);
                saveProductInfo(productInfo);
            }
        }

涉及到的结构体定义为:

        struct AmazonProductInfo
        {
            public string title;
            public string description;
            //5 bullet
            public string[] bulletArr; // total 5
            //download 5 pics
            public string[] imgFullnameArr; // total 5
            //product keyword, up to 5
            //public string keyword1;
            //public string keyword2;
            //public string keyword3;
            //public string keyword4;
            //public string keyword5;
            //highest price of total (up to 8) sellers
            public float highestPrice;
            public bool isOneSellerIsAmazon;
            public int reviewNumber;
            public bool isBestSeller;
        };

结果编译出错:

Inconsistent accessibility parameter type is less accessible than method

Error    1    Inconsistent accessibility: parameter type ‘ScrapeAmazonProduct.frmScrapeAmazonProduct.AmazonProductInfo’ is less accessible than method ‘ScrapeAmazonProduct.frmScrapeAmazonProduct.saveProductInfo(ScrapeAmazonProduct.frmScrapeAmazonProduct.AmazonProductInfo)’    D:\tmp\tmp_dev_root\freelance\elance\40939187_scrape_amazon\40939187_scrape_amazon\ScrapeAmazonProduct\ScrapeAmazonProduct\frmScrapeAmazonProduct.cs    648

 

【解决过程】

1.注意到,已经把原先的函数加上public了。

但是还是出错。

2.参考:

Inconsistent Accessibility: Parameter type is less accessible than method

去把原先的结构体:

        struct AmazonProductInfo
        {
            public string title;
            public string description;
            //5 bullet
            public string[] bulletArr; // total 5
            //download 5 pics
            public string[] imgFullnameArr; // total 5
            //product keyword, up to 5
            //public string keyword1;
            //public string keyword2;
            //public string keyword3;
            //public string keyword4;
            //public string keyword5;
            //highest price of total (up to 8) sellers
            public float highestPrice;
            public bool isOneSellerIsAmazon;
            public int reviewNumber;
            public bool isBestSeller;
        };

也加上public:

        public struct AmazonProductInfo
        {
            public string title;
            public string description;
            //5 bullet
            public string[] bulletArr; // total 5
            //download 5 pics
            public string[] imgFullnameArr; // total 5
            //product keyword, up to 5
            //public string keyword1;
            //public string keyword2;
            //public string keyword3;
            //public string keyword4;
            //public string keyword5;
            //highest price of total (up to 8) sellers
            public float highestPrice;
            public bool isOneSellerIsAmazon;
            public int reviewNumber;
            public bool isBestSeller;
        };

试试,结果就解决问题了。

3.但是还是很诡异,因为此处,不论是函数:

saveProductInfo

函数其中的参数:

AmazonProductInfo

还是调用该函数的其他函数:

checkAndExtractForSingleProduct

都是在当前的cs文件内,都是属于命名空间:

ScrapeAmazonProduct.frmScrapeAmazonProduct

的。

所以,应该不存在此类错误的。

4.后来把原先的函数saveProductInfo,从public改为private

同时去掉结构体AmazonProductInfo的public:

struct AmazonProductInfo
{
    public string title;
    public string description;
    //5 bullet
    public string[] bulletArr; // total 5
    //download 5 pics
    public string[] imgFullnameArr; // total 5
    //product keyword, up to 5
    //public string keyword1;
    //public string keyword2;
    //public string keyword3;
    //public string keyword4;
    //public string keyword5;
    //highest price of total (up to 8) sellers
    public float highestPrice;
    public bool isOneSellerIsAmazon;
    public int reviewNumber;
    public bool isBestSeller;
};


//save product info
private void saveProductInfo(AmazonProductInfo productInfo)
{
    return;
}

private void checkAndExtractForSingleProduct(string productUrl)
{
    ......
    saveProductInfo(productInfo);
    ......
}

结果也是可以的。

至此,才算明白内部的逻辑。

 

【总结】

下列情况:

struct structA //default here is private
{
    ......
};


//save product info
public void funA(structA structValue)
{
    return;
}

private void funcB(string productUrl)
{
    ......
    funA(structValue);
    ......
}

会出现此处的编译错误提示:

Inconsistent accessibility: parameter type xxx is less accessible than method yyy

的原因:

funcB调用funcA,本来是没问题的,但是,由于funcB是private,调用的funcA是public,并且传递的参数structA是private,所以可能会出现:

structA是属于private的funcB的,而无法被funcA访问到。

解决办法:

要么把funcA改为private,则此时,两个函数都是private,而对应的structA是public,也都可以访问到了:

public struct structA
{
    ......
};


//save product info
private void funA(structA structValue)
{
    return;
}

private void funcB(string productUrl)
{
    ......
    funA(structValue);
    ......
}

 

要么把structA改为public,使得不论是private的funcB还是public的funcA都访问到:

public struct structA
{
    ......
};


//save product info
public void funA(structA structValue)
{
    return;
}

private void funcB(string productUrl)
{
    ......
    funA(structValue);
    ......
}

 

结论:

以后对于函数和结构体等,还是要注意设置合适的权限,即统一的private或public,不要不小心搞混乱了。

转载请注明:在路上 » 【已解决】C#函数编译出错:Inconsistent accessibility: parameter type ‘aaa.bbb.ccc’ is less accessible than method ‘aaa.bbb.functionName(aaa.bbb.ccc)

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.166 seconds, using 22.11MB memory